This commit is contained in:
hrsh7th
2021-11-01 13:58:41 +09:00
parent baa39271b2
commit e8cb695b0b
4 changed files with 37 additions and 19 deletions

View File

@@ -35,7 +35,7 @@ compare.recently_used = setmetatable({
records = {}, records = {},
add_entry = function(self, e) add_entry = function(self, e)
self.records[e.completion_item.label] = vim.loop.now() self.records[e.completion_item.label] = vim.loop.now()
end end,
}, { }, {
__call = function(self, entry1, entry2) __call = function(self, entry1, entry2)
local t1 = self.records[entry1.completion_item.label] or -1 local t1 = self.records[entry1.completion_item.label] or -1
@@ -43,7 +43,7 @@ compare.recently_used = setmetatable({
if t1 ~= t2 then if t1 ~= t2 then
return t1 > t2 return t1 > t2
end end
end end,
}) })
-- kind -- kind

View File

@@ -91,7 +91,7 @@ return function()
else else
fallback() fallback()
end end
end end,
}), }),
['<S-Tab>'] = mapping({ ['<S-Tab>'] = mapping({
c = function(fallback) c = function(fallback)
@@ -105,7 +105,7 @@ return function()
else else
fallback() fallback()
end end
end end,
}), }),
['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }), ['<C-n>'] = mapping(mapping.select_next_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),
['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }), ['<C-p>'] = mapping(mapping.select_prev_item({ behavior = types.cmp.SelectBehavior.Insert }), { 'i', 'c' }),

View File

@@ -215,14 +215,31 @@ keymap.listen = setmetatable({
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local existing = keymap.find_map_by_lhs(mode, keys) local existing = keymap.find_map_by_lhs(mode, keys)
local done = true local cur_definition = self.cache:get({ 'definition', self.cache:get({ 'id', mode, bufnr, keys }) or '' })
done = done and string.match(existing.rhs, vim.pesc('v:lua.cmp.utils.keymap.listen.run')) if cur_definition then
done = done and self.cache:get({ 'id', mode, bufnr, keys }) ~= nil local same = true
if done then 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 return
end end
end
self.cache:set({ 'id', mode, bufnr, keys }, misc.id('cmp.utils.keymap.listen')) 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) local fallback = keymap.evacuate(mode, keys)
vim.api.nvim_buf_set_keymap(0, mode, keys, ('<Cmd>call v:lua.cmp.utils.keymap.listen.run(%s)<CR>'):format(self.cache:get({ 'id', mode, bufnr, keys })), { vim.api.nvim_buf_set_keymap(0, mode, keys, ('<Cmd>call v:lua.cmp.utils.keymap.listen.run(%s)<CR>'):format(self.cache:get({ 'id', mode, bufnr, keys })), {
expr = false, expr = false,
@@ -260,6 +277,9 @@ end)
---@return { keys: string, mode: string } ---@return { keys: string, mode: string }
keymap.evacuate = function(mode, lhs) keymap.evacuate = function(mode, lhs)
local map = keymap.find_map_by_lhs(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 <Plug> mapping. We escape fisrt recursive key sequence. See `:help recursive_mapping`) -- Keep existing mapping as <Plug> mapping. We escape fisrt recursive key sequence. See `:help recursive_mapping`)
local rhs = map.rhs local rhs = map.rhs
@@ -322,23 +342,21 @@ end
keymap.find_map_by_lhs = function(mode, lhs) keymap.find_map_by_lhs = function(mode, lhs)
for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do for _, map in ipairs(vim.api.nvim_buf_get_keymap(0, mode)) do
if keymap.equals(map.lhs, lhs) then 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 return map
end end
end end
for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do for _, map in ipairs(vim.api.nvim_get_keymap(mode)) do
if keymap.equals(map.lhs, lhs) then 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 return map
end end
end end
return {
lhs = lhs,
rhs = lhs,
expr = 0,
script = 0,
noremap = 1,
nowait = 0,
silent = 1,
}
end end
keymap.spec = function() keymap.spec = function()