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

Commit

Permalink
fix: avoid overwrting user defined mapping
Browse files Browse the repository at this point in the history
fix some stuff

fix

not show error on old mapping
  • Loading branch information
haorenW1025 committed Jul 28, 2020
1 parent 8aecb23 commit e5f3a3b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
10 changes: 8 additions & 2 deletions autoload/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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("\<c-e>", "n", v:true)
let key = g:completion_confirm_key
call nvim_feedkeys(key, "n", v:true)
endif
else
call nvim_feedkeys("\<c-g>\<c-g>", "n", v:true)
let key = g:completion_confirm_key
Expand Down
30 changes: 25 additions & 5 deletions lua/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 --
------------------------------------------------------------------------
Expand Down Expand Up @@ -213,11 +225,19 @@ M.on_attach = function(option)
api.nvim_command("autocmd InsertCharPre <buffer> lua require'completion'.on_InsertCharPre()")
api.nvim_command("autocmd CompleteDone <buffer> 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" ? "\\<Plug>(completion_confirm_completion)" :'..
' "\\<c-e>\\<CR>" : "\\<CR>"',
{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, '<Plug>(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
Expand Down
1 change: 1 addition & 0 deletions lua/completion/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5f3a3b

Please sign in to comment.