Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit e5f3a3b

Browse files
committed
fix: avoid overwrting user defined mapping
fix some stuff fix not show error on old mapping
1 parent 8aecb23 commit e5f3a3b

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

autoload/completion.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ function! completion#completion_confirm() abort
55
endfunction
66

77
function! completion#wrap_completion() abort
8-
if pumvisible() != 0 && complete_info()["selected"] != "-1"
9-
call completion#completion_confirm()
8+
if pumvisible() != 0
9+
if complete_info()["selected"] != "-1"
10+
call completion#completion_confirm()
11+
else
12+
call nvim_feedkeys("\<c-e>", "n", v:true)
13+
let key = g:completion_confirm_key
14+
call nvim_feedkeys(key, "n", v:true)
15+
endif
1016
else
1117
call nvim_feedkeys("\<c-g>\<c-g>", "n", v:true)
1218
let key = g:completion_confirm_key

lua/completion.lua

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ M.triggerCompletion = function()
3333
source.triggerCompletion(true, manager)
3434
end
3535

36+
3637
M.completionToggle = function()
3738
local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'})
3839
if enable == nil then
@@ -110,6 +111,17 @@ local function hasConfirmedCompletion()
110111
end
111112
end
112113

114+
-- make sure we didn't overwrite user defined mapping
115+
local function checkMapping(map, key)
116+
for _, m in ipairs(map) do
117+
if api.nvim_replace_termcodes(m.lhs, true, false, true) == api.nvim_replace_termcodes(key, true, false, true) then
118+
if not m.rhs:find("completion_confirm_completion") then
119+
return false
120+
end
121+
end
122+
end
123+
return true
124+
end
113125
------------------------------------------------------------------------
114126
-- autocommands --
115127
------------------------------------------------------------------------
@@ -213,11 +225,19 @@ M.on_attach = function(option)
213225
api.nvim_command("autocmd InsertCharPre <buffer> lua require'completion'.on_InsertCharPre()")
214226
api.nvim_command("autocmd CompleteDone <buffer> lua require'completion'.on_CompleteDone()")
215227
api.nvim_command("augroup end")
216-
if string.len(opt.get_option('confirm_key')) ~= 0 then
217-
api.nvim_buf_set_keymap(0, 'i', opt.get_option('confirm_key'),
218-
'pumvisible() ? complete_info()["selected"] != "-1" ? "\\<Plug>(completion_confirm_completion)" :'..
219-
' "\\<c-e>\\<CR>" : "\\<CR>"',
220-
{silent=false, noremap=false, expr=true})
228+
local confirm_key = opt.get_option('confirm_key')
229+
if string.len(confirm_key) ~= 0 then
230+
if checkMapping(api.nvim_buf_get_keymap(0, "i"), confirm_key) and
231+
(manager.checkGlobalMapping or checkMapping(api.nvim_get_keymap("i"), confirm_key)) then
232+
manager.checkGlobalMapping = true
233+
api.nvim_buf_set_keymap(0, 'i', confirm_key, '<Plug>(completion_confirm_completion)',
234+
{silent=false, noremap=false})
235+
else
236+
api.nvim_err_writeln(string.format(
237+
"completion-nvim: mapping conflict! you've already mapped your confirm key to something else in insert mode. "..
238+
" Consider changing g:completion_confirm_key"
239+
))
240+
end
221241
end
222242
-- overwrite vsnip_integ autocmd since we handle it on ourself in confirmCompletion
223243
if vim.fn.exists("#vsnip_integ") then

lua/completion/manager.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ manager = {
1919
confirmedCompletion = false, -- flag for manual confirmation of completion
2020
forceCompletion = false, -- flag for forced manual completion/source change
2121
chainIndex = 1, -- current index in loaded chain
22+
checkGlobalMapping = false -- indication of global mapping is checked or not
2223
}
2324

2425
-- reset manager

0 commit comments

Comments
 (0)