From e5f3a3bf1910862a2d659b56470c93c7e79765ae Mon Sep 17 00:00:00 2001 From: haorenW1025 Date: Wed, 22 Jul 2020 17:00:25 +0800 Subject: [PATCH] fix: avoid overwrting user defined mapping fix some stuff fix not show error on old mapping --- autoload/completion.vim | 10 ++++++++-- lua/completion.lua | 30 +++++++++++++++++++++++++----- lua/completion/manager.lua | 1 + 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/autoload/completion.vim b/autoload/completion.vim index e307aa2..8b5685d 100644 --- a/autoload/completion.vim +++ b/autoload/completion.vim @@ -5,8 +5,14 @@ function! completion#completion_confirm() abort endfunction function! completion#wrap_completion() abort - if pumvisible() != 0 && complete_info()["selected"] != "-1" - call completion#completion_confirm() + if pumvisible() != 0 + if complete_info()["selected"] != "-1" + call completion#completion_confirm() + else + call nvim_feedkeys("\", "n", v:true) + let key = g:completion_confirm_key + call nvim_feedkeys(key, "n", v:true) + endif else call nvim_feedkeys("\\", "n", v:true) let key = g:completion_confirm_key diff --git a/lua/completion.lua b/lua/completion.lua index ed93ee1..e2ce017 100644 --- a/lua/completion.lua +++ b/lua/completion.lua @@ -33,6 +33,7 @@ M.triggerCompletion = function() source.triggerCompletion(true, manager) end + M.completionToggle = function() local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'}) if enable == nil then @@ -110,6 +111,17 @@ local function hasConfirmedCompletion() end end +-- make sure we didn't overwrite user defined mapping +local function checkMapping(map, key) + for _, m in ipairs(map) do + if api.nvim_replace_termcodes(m.lhs, true, false, true) == api.nvim_replace_termcodes(key, true, false, true) then + if not m.rhs:find("completion_confirm_completion") then + return false + end + end + end + return true +end ------------------------------------------------------------------------ -- autocommands -- ------------------------------------------------------------------------ @@ -213,11 +225,19 @@ M.on_attach = function(option) api.nvim_command("autocmd InsertCharPre lua require'completion'.on_InsertCharPre()") api.nvim_command("autocmd CompleteDone lua require'completion'.on_CompleteDone()") api.nvim_command("augroup end") - if string.len(opt.get_option('confirm_key')) ~= 0 then - api.nvim_buf_set_keymap(0, 'i', opt.get_option('confirm_key'), - 'pumvisible() ? complete_info()["selected"] != "-1" ? "\\(completion_confirm_completion)" :'.. - ' "\\\\" : "\\"', - {silent=false, noremap=false, expr=true}) + local confirm_key = opt.get_option('confirm_key') + if string.len(confirm_key) ~= 0 then + if checkMapping(api.nvim_buf_get_keymap(0, "i"), confirm_key) and + (manager.checkGlobalMapping or checkMapping(api.nvim_get_keymap("i"), confirm_key)) then + manager.checkGlobalMapping = true + api.nvim_buf_set_keymap(0, 'i', confirm_key, '(completion_confirm_completion)', + {silent=false, noremap=false}) + else + api.nvim_err_writeln(string.format( + "completion-nvim: mapping conflict! you've already mapped your confirm key to something else in insert mode. ".. + " Consider changing g:completion_confirm_key" + )) + end end -- overwrite vsnip_integ autocmd since we handle it on ourself in confirmCompletion if vim.fn.exists("#vsnip_integ") then diff --git a/lua/completion/manager.lua b/lua/completion/manager.lua index 6e9b920..408812e 100644 --- a/lua/completion/manager.lua +++ b/lua/completion/manager.lua @@ -19,6 +19,7 @@ manager = { confirmedCompletion = false, -- flag for manual confirmation of completion forceCompletion = false, -- flag for forced manual completion/source change chainIndex = 1, -- current index in loaded chain + checkGlobalMapping = false -- indication of global mapping is checked or not } -- reset manager