Fix keymap bug

This commit is contained in:
hrsh7th
2021-11-02 02:06:34 +09:00
parent b7f79a19c6
commit 1c33887afb

View File

@@ -118,27 +118,27 @@ keymap.listen = setmetatable({
if not existing then if not existing then
return return
end end
local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or '*' local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or -1
self.cache:set({ 'id', mode, bufnr, keys }, misc.id('cmp.utils.keymap.listen')) self.cache:set({ 'id', mode, keys, bufnr }, misc.id('cmp.utils.keymap.listen'))
local fallback = keymap.evacuate(mode, keys) local fallback = keymap.evacuate(mode, keys, bufnr)
if existing.buffer then if bufnr == -1 then
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_set_keymap(mode, keys, ('<Cmd>call v:lua.cmp.utils.keymap.listen.run(%s)<CR>'):format(self.cache:get({ 'id', mode, keys, bufnr })), {
expr = false, expr = false,
noremap = true, noremap = true,
silent = true, silent = true,
}) })
else else
vim.api.nvim_set_keymap(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, keys, bufnr })), {
expr = false, expr = false,
noremap = true, noremap = true,
silent = true, silent = true,
}) })
end end
self.cache:set({ 'definition', self.cache:get({ 'id', mode, bufnr, keys }) }, { self.cache:set({ 'definition', self.cache:get({ 'id', mode, keys, bufnr }) }, {
keys = keys,
mode = mode, mode = mode,
keys = keys,
bufnr = bufnr, bufnr = bufnr,
callback = callback, callback = callback,
fallback = fallback, fallback = fallback,
@@ -216,19 +216,20 @@ end
---Evacuate existing key mapping ---Evacuate existing key mapping
---@param mode string ---@param mode string
---@param lhs string ---@param keys string
---@param bufnr number
---@return { keys: string, mode: string } ---@return { keys: string, mode: string }
keymap.evacuate = function(mode, lhs) keymap.evacuate = function(mode, keys, bufnr)
local map = keymap.get_mapping(mode, lhs) local map = keymap.get_mapping(mode, keys)
if not map then if not map then
return { keys = lhs, mode = 'itn' } return { keys = keys, mode = 'itn' }
end 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
if not map.noremap and map.expr then if not map.noremap and map.expr then
-- remap & expr mapping should evacuate as <Plug> mapping with solving recursive mapping. -- remap & expr mapping should evacuate as <Plug> mapping with solving recursive mapping.
rhs = string.format('v:lua.cmp.utils.keymap.evacuate.expr("%s", "%s", "%s")', mode, str.escape(keymap.escape(lhs), { '"' }), str.escape(keymap.escape(rhs), { '"' })) rhs = string.format('v:lua.cmp.utils.keymap.evacuate.expr("%s", "%s", "%s", "%s")', mode, str.escape(keymap.escape(keys), { '"' }), bufnr, str.escape(keymap.escape(rhs), { '"' }))
elseif map.noremap and map.expr then elseif map.noremap and map.expr then
-- noremap & expr mapping should always evacuate as <Plug> mapping. -- noremap & expr mapping should always evacuate as <Plug> mapping.
rhs = rhs rhs = rhs
@@ -237,7 +238,7 @@ keymap.evacuate = function(mode, lhs)
rhs = rhs rhs = rhs
elseif not map.noremap then elseif not map.noremap then
-- remap & non-expr mapping should be checked if recursive or not. -- remap & non-expr mapping should be checked if recursive or not.
rhs = keymap.recursive(mode, lhs, rhs) rhs = keymap.recursive(mode, keys, bufnr, rhs)
if rhs == map.rhs or map.noremap then if rhs == map.rhs or map.noremap then
return { keys = rhs, mode = 'it' .. (map.noremap and 'n' or '') } return { keys = rhs, mode = 'it' .. (map.noremap and 'n' or '') }
end end
@@ -247,33 +248,51 @@ keymap.evacuate = function(mode, lhs)
end end
local fallback = ('<Plug>(cmp-utils-keymap-evacuate-rhs:%s)'):format(map.lhs) local fallback = ('<Plug>(cmp-utils-keymap-evacuate-rhs:%s)'):format(map.lhs)
vim.api.nvim_buf_set_keymap(0, mode, fallback, rhs, { if bufnr == -1 then
expr = map.expr, vim.api.nvim_set_keymap(mode, fallback, rhs, {
noremap = map.noremap, expr = map.expr,
script = map.script, noremap = map.noremap,
silent = mode ~= 'c', -- I can't understand but it solves the #427 (wilder.nvim's mapping does not work if silent=true in cmdline mode...) script = map.script,
}) silent = mode ~= 'c', -- I can't understand but it solves the #427 (wilder.nvim's mapping does not work if silent=true in cmdline mode...)
})
else
vim.api.nvim_buf_set_keymap(bufnr, mode, fallback, rhs, {
expr = map.expr,
noremap = map.noremap,
script = map.script,
silent = mode ~= 'c', -- I can't understand but it solves the #427 (wilder.nvim's mapping does not work if silent=true in cmdline mode...)
})
end
return { keys = fallback, mode = 'it' } return { keys = fallback, mode = 'it' }
end end
misc.set(_G, { 'cmp', 'utils', 'keymap', 'evacuate', 'expr' }, function(mode, lhs, rhs) misc.set(_G, { 'cmp', 'utils', 'keymap', 'evacuate', 'expr' }, function(mode, keys, bufnr, rhs)
return keymap.t(keymap.recursive(mode, lhs, vim.api.nvim_eval(rhs))) return keymap.t(keymap.recursive(mode, keys, bufnr, vim.api.nvim_eval(rhs)))
end) end)
---Solve recursive mapping ---Solve recursive mapping
---@param mode string ---@param mode string
---@param lhs string ---@param keys string
---@param bufnr number
---@param rhs string ---@param rhs string
---@return string ---@return string
keymap.recursive = function(mode, lhs, rhs) keymap.recursive = function(mode, keys, bufnr, rhs)
rhs = keymap.normalize(rhs) rhs = keymap.normalize(rhs)
local fallback_lhs = ('<Plug>(cmp-utils-keymap-listen-lhs:%s)'):format(lhs) local fallback_lhs = ('<Plug>(cmp-utils-keymap-listen-lhs:%s)'):format(keys)
local new_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.normalize(lhs)), fallback_lhs) local new_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.normalize(keys)), fallback_lhs)
if not keymap.equals(new_rhs, rhs) then if not keymap.equals(new_rhs, rhs) then
vim.api.nvim_buf_set_keymap(0, mode, fallback_lhs, lhs, { if bufnr == '-1' then
expr = false, vim.api.nvim_set_keymap(mode, fallback_lhs, keys, {
noremap = true, expr = false,
silent = true, noremap = true,
}) silent = true,
})
else
vim.api.nvim_buf_set_keymap(0, mode, fallback_lhs, keys, {
expr = false,
noremap = true,
silent = true,
})
end
end end
return new_rhs return new_rhs
end end