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

Commit 432963d

Browse files
committed
fix: avoid overwrting user defined mapping
fix some stuff fix
1 parent 8aecb23 commit 432963d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lua/completion.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ local function hasConfirmedCompletion()
110110
end
111111
end
112112

113+
-- make sure we didn't overwrite user defined mapping
114+
local function checkMapping(map, key)
115+
for _, m in ipairs(map) do
116+
if api.nvim_replace_termcodes(m.lhs, true, false, true) == api.nvim_replace_termcodes(key, true, false, true) then
117+
print(api.nvim_replace_termcodes(m.lhs, true, false, true), vim.inspect(m))
118+
return false
119+
end
120+
end
121+
return true
122+
end
113123
------------------------------------------------------------------------
114124
-- autocommands --
115125
------------------------------------------------------------------------
@@ -213,11 +223,18 @@ M.on_attach = function(option)
213223
api.nvim_command("autocmd InsertCharPre <buffer> lua require'completion'.on_InsertCharPre()")
214224
api.nvim_command("autocmd CompleteDone <buffer> lua require'completion'.on_CompleteDone()")
215225
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})
226+
local confirm_key = opt.get_option('confirm_key')
227+
if string.len(confirm_key) ~= 0 then
228+
if checkMapping(api.nvim_buf_get_keymap(0, "i"), confirm_key) and
229+
(manager.checkGlobalMapping or checkMapping(api.nvim_get_keymap("i"), confirm_key)) then
230+
manager.checkGlobalMapping = true
231+
api.nvim_buf_set_keymap(0, 'i', confirm_key,
232+
'pumvisible() ? complete_info()["selected"] != "-1" ? "\\<Plug>(completion_confirm_completion)" :'..
233+
' "\\<c-e>\\<CR>" : "\\<CR>"',
234+
{silent=false, noremap=false, expr=true})
235+
else
236+
api.nvim_err_writeln("completion-nvim: mapping conflict, please consider changing g:completion_confirm_key")
237+
end
221238
end
222239
-- overwrite vsnip_integ autocmd since we handle it on ourself in confirmCompletion
223240
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)