From e8cb695b0bcf53ea51837421d24ff01cadb4f4e5 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Mon, 1 Nov 2021 13:58:41 +0900 Subject: [PATCH] Fix #436 --- lua/cmp/config/compare.lua | 4 ++-- lua/cmp/config/default.lua | 4 ++-- lua/cmp/core.lua | 2 +- lua/cmp/utils/keymap.lua | 46 ++++++++++++++++++++++++++------------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/lua/cmp/config/compare.lua b/lua/cmp/config/compare.lua index 51bdcfe..d5657cb 100644 --- a/lua/cmp/config/compare.lua +++ b/lua/cmp/config/compare.lua @@ -35,7 +35,7 @@ compare.recently_used = setmetatable({ records = {}, add_entry = function(self, e) self.records[e.completion_item.label] = vim.loop.now() - end + end, }, { __call = function(self, entry1, entry2) local t1 = self.records[entry1.completion_item.label] or -1 @@ -43,7 +43,7 @@ compare.recently_used = setmetatable({ if t1 ~= t2 then return t1 > t2 end - end + end, }) -- kind diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 28bf42d..c0d5f5a 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -91,7 +91,7 @@ return function() else fallback() end - end + end, }), [''] = mapping({ c = function(fallback) @@ -105,7 +105,7 @@ return function() else fallback() end - end + end, }), [''] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }), [''] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }), diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index f5db889..9e29464 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -202,7 +202,7 @@ core.autoindent = function(self, trigger_event, callback) if trigger_event ~= types.cmp.TriggerEvent.TextChanged then return callback() end - if not api.is_insert_mode() then + if not api.is_insert_mode() then return callback() end diff --git a/lua/cmp/utils/keymap.lua b/lua/cmp/utils/keymap.lua index cebb27d..c618988 100644 --- a/lua/cmp/utils/keymap.lua +++ b/lua/cmp/utils/keymap.lua @@ -215,14 +215,31 @@ keymap.listen = setmetatable({ local bufnr = vim.api.nvim_get_current_buf() local existing = keymap.find_map_by_lhs(mode, keys) - local done = true - done = done and string.match(existing.rhs, vim.pesc('v:lua.cmp.utils.keymap.listen.run')) - done = done and self.cache:get({ 'id', mode, bufnr, keys }) ~= nil - if done then - return + local cur_definition = self.cache:get({ 'definition', self.cache:get({ 'id', mode, bufnr, keys }) or '' }) + if cur_definition then + local same = true + same = same and existing + same = same and cur_definition.existing.lhs == existing.lhs + same = same and cur_definition.existing.rhs == existing.rhs + same = same and cur_definition.existing.expr == existing.expr + same = same and cur_definition.existing.noremap == existing.noremap + same = same and cur_definition.existing.script == existing.script + if not existing or same then + return + end end self.cache:set({ 'id', mode, bufnr, keys }, misc.id('cmp.utils.keymap.listen')) + existing = existing or { + lhs = keys, + rhs = keys, + expr = 0, + script = 0, + noremap = 1, + nowait = 0, + silent = 1, + } + local fallback = keymap.evacuate(mode, keys) vim.api.nvim_buf_set_keymap(0, mode, keys, ('call v:lua.cmp.utils.keymap.listen.run(%s)'):format(self.cache:get({ 'id', mode, bufnr, keys })), { expr = false, @@ -260,6 +277,9 @@ end) ---@return { keys: string, mode: string } keymap.evacuate = function(mode, lhs) local map = keymap.find_map_by_lhs(mode, lhs) + if not map then + return { keys = lhs, mode = 'itn' } + end -- Keep existing mapping as mapping. We escape fisrt recursive key sequence. See `:help recursive_mapping`) local rhs = map.rhs @@ -322,23 +342,21 @@ end keymap.find_map_by_lhs = function(mode, lhs) for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do if keymap.equals(map.lhs, lhs) then + if string.match(map.rhs, vim.pesc('v:lua.cmp.utils.keymap.listen.run')) then + return nil + end return map end end + for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do if keymap.equals(map.lhs, lhs) then + if string.match(map.rhs, vim.pesc('v:lua.cmp.utils.keymap.listen.run')) then + return nil + end return map end end - return { - lhs = lhs, - rhs = lhs, - expr = 0, - script = 0, - noremap = 1, - nowait = 0, - silent = 1, - } end keymap.spec = function()