Organize keymap implementation (#454)

* Organize keymap implementation

* Pass tests

* Improve

* Restore tests
This commit is contained in:
hrsh7th
2021-11-02 11:59:26 +09:00
committed by GitHub
parent 1c33887afb
commit 017d3abc57
2 changed files with 82 additions and 127 deletions

View File

@@ -1,6 +1,4 @@
local misc = require('cmp.utils.misc') local misc = require('cmp.utils.misc')
local str = require('cmp.utils.str')
local cache = require('cmp.utils.cache')
local api = require('cmp.utils.api') local api = require('cmp.utils.api')
local keymap = {} local keymap = {}
@@ -12,21 +10,6 @@ keymap.t = function(keys)
return vim.api.nvim_replace_termcodes(keys, true, true, true) return vim.api.nvim_replace_termcodes(keys, true, true, true)
end end
---Escape keymap with <LT>
keymap.escape = function(keys)
local i = 1
while i <= #keys do
if string.sub(keys, i, i) == '<' then
if not vim.tbl_contains({ '<lt>', '<Lt>', '<lT>', '<LT>' }, string.sub(keys, i, i + 3)) then
keys = string.sub(keys, 1, i - 1) .. '<LT>' .. string.sub(keys, i + 1)
i = i + 3
end
end
i = i + 1
end
return keys
end
---Normalize key sequence. ---Normalize key sequence.
---@param keys string ---@param keys string
---@return string ---@return string
@@ -109,56 +92,30 @@ keymap.equals = function(a, b)
end end
---Register keypress handler. ---Register keypress handler.
keymap.listen = setmetatable({ keymap.listen = function(mode, lhs, callback)
cache = cache.new(), lhs = keymap.normalize(keymap.to_keymap(lhs))
}, {
__call = function(self, mode, keys_or_chars, callback)
local keys = keymap.normalize(keymap.to_keymap(keys_or_chars))
local existing = keymap.get_mapping(mode, keys)
if not existing then
return
end
local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or -1
self.cache:set({ 'id', mode, keys, bufnr }, misc.id('cmp.utils.keymap.listen'))
local fallback = keymap.evacuate(mode, keys, bufnr) local existing = keymap.get_mapping(mode, lhs)
if bufnr == -1 then if string.match(existing.rhs, vim.pesc('v:lua.cmp.utils.keymap')) then
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 })), { return
expr = false,
noremap = true,
silent = true,
})
else
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,
noremap = true,
silent = true,
})
end
self.cache:set({ 'definition', self.cache:get({ 'id', mode, keys, bufnr }) }, {
mode = mode,
keys = keys,
bufnr = bufnr,
callback = callback,
fallback = fallback,
existing = existing,
})
end,
})
misc.set(_G, { 'cmp', 'utils', 'keymap', 'listen', 'run' }, function(id)
local definition = keymap.listen.cache:get({ 'definition', id })
if definition.mode == 'c' and vim.fn.getcmdtype() == '=' then
return vim.api.nvim_feedkeys(keymap.t(definition.fallback.keys), definition.fallback.mode, true)
end end
definition.callback(
definition.keys, local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or -1
misc.once(function() local fallback = keymap.evacuate(bufnr, mode, lhs)
vim.api.nvim_feedkeys(keymap.t(definition.fallback.keys), definition.fallback.mode, true) keymap.set_map(bufnr, mode, lhs, function()
end) if mode == 'c' and vim.fn.getcmdtype() == '=' then
) return vim.api.nvim_feedkeys(keymap.t(fallback.keys), fallback.mode, true)
return keymap.t('<Ignore>') end
end)
callback(lhs, misc.once(function()
vim.api.nvim_feedkeys(keymap.t(fallback.keys), fallback.mode, true)
end))
end, {
expr = false,
noremap = true,
silent = true,
})
end
---Get mapping ---Get mapping
---@param mode string ---@param mode string
@@ -169,9 +126,6 @@ keymap.get_mapping = 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 { return {
lhs = map.lhs, lhs = map.lhs,
rhs = map.rhs, rhs = map.rhs,
@@ -187,9 +141,6 @@ keymap.get_mapping = function(mode, lhs)
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 { return {
lhs = map.lhs, lhs = map.lhs,
rhs = map.rhs, rhs = map.rhs,
@@ -202,6 +153,7 @@ keymap.get_mapping = function(mode, lhs)
} }
end end
end end
return { return {
lhs = lhs, lhs = lhs,
rhs = lhs, rhs = lhs,
@@ -215,21 +167,23 @@ keymap.get_mapping = function(mode, lhs)
end end
---Evacuate existing key mapping ---Evacuate existing key mapping
---@param mode string
---@param keys string
---@param bufnr number ---@param bufnr number
---@param mode string
---@param lhs string
---@return { keys: string, mode: string } ---@return { keys: string, mode: string }
keymap.evacuate = function(mode, keys, bufnr) keymap.evacuate = function(bufnr, mode, lhs)
local map = keymap.get_mapping(mode, keys) local map = keymap.get_mapping(mode, lhs)
if not map then if not map then
return { keys = keys, mode = 'itn' } return { keys = lhs, 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", "%s")', mode, str.escape(keymap.escape(keys), { '"' }), bufnr, str.escape(keymap.escape(rhs), { '"' })) rhs = function()
return keymap.t(keymap.recursive(bufnr, mode, lhs, vim.api.nvim_eval(map.rhs)))
end
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
@@ -238,7 +192,7 @@ keymap.evacuate = function(mode, keys, bufnr)
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, keys, bufnr, rhs) rhs = keymap.recursive(bufnr, mode, lhs, 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,54 +201,61 @@ keymap.evacuate = function(mode, keys, bufnr)
return { keys = rhs, mode = 'it' .. (map.noremap and 'n' or '') } return { keys = rhs, mode = 'it' .. (map.noremap and 'n' or '') }
end end
local fallback = ('<Plug>(cmp-utils-keymap-evacuate-rhs:%s)'):format(map.lhs) local fallback = ('<Plug>(cmp.utils.keymap.evacuate:%s)'):format(map.lhs)
if bufnr == -1 then keymap.set_map(bufnr, mode, fallback, rhs, {
vim.api.nvim_set_keymap(mode, fallback, rhs, { expr = map.expr,
expr = map.expr, noremap = map.noremap,
noremap = map.noremap, script = map.script,
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...)
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, keys, bufnr, rhs)
return keymap.t(keymap.recursive(mode, keys, bufnr, vim.api.nvim_eval(rhs)))
end)
---Solve recursive mapping ---Solve recursive mapping
---@param mode string
---@param keys string
---@param bufnr number ---@param bufnr number
---@param mode string
---@param lhs string
---@param rhs string ---@param rhs string
---@return string ---@return string
keymap.recursive = function(mode, keys, bufnr, rhs) keymap.recursive = function(bufnr, mode, lhs, rhs)
rhs = keymap.normalize(rhs) rhs = keymap.normalize(rhs)
local fallback_lhs = ('<Plug>(cmp-utils-keymap-listen-lhs:%s)'):format(keys)
local new_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.normalize(keys)), fallback_lhs) local recursive_lhs = ('<Plug>(cmp.utils.keymap.recursive:%s)'):format(lhs)
if not keymap.equals(new_rhs, rhs) then local recursive_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.normalize(lhs)), recursive_lhs)
if bufnr == '-1' then if not keymap.equals(recursive_rhs, rhs) then
vim.api.nvim_set_keymap(mode, fallback_lhs, keys, { keymap.set_map(bufnr, mode, recursive_lhs, lhs, {
expr = false, expr = false,
noremap = true, noremap = true,
silent = 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 recursive_rhs
end end
---Set keymapping
keymap.set_map = setmetatable({
callbacks = {}
}, {
__call = function(self, bufnr, mode, lhs, rhs, opts)
if type(rhs) == 'function' then
local id = misc.id('cmp.utils.keymap.set_map')
self.callbacks[id] = rhs
if opts.expr then
rhs = ('v:lua.cmp.utils.keymap.set_map(%s)'):format(id)
else
rhs = ('<Cmd>call v:lua.cmp.utils.keymap.set_map(%s)<CR>'):format(id)
end
end
if bufnr == -1 then
vim.api.nvim_set_keymap(mode, lhs, rhs, opts)
else
vim.api.nvim_buf_set_keymap(bufnr, mode, lhs, rhs, opts)
end
end
})
misc.set(_G, { 'cmp', 'utils', 'keymap', 'set_map' }, function(id)
return keymap.set_map.callbacks[id]()
end)
return keymap return keymap

View File

@@ -11,12 +11,6 @@ describe('keymap', function()
assert.are.equal(keymap.to_keymap('|'), '<Bar>') assert.are.equal(keymap.to_keymap('|'), '<Bar>')
end) end)
it('escape', function()
assert.are.equal(keymap.escape('<C-d>'), '<LT>C-d>')
assert.are.equal(keymap.escape('<C-d><C-f>'), '<LT>C-d><LT>C-f>')
assert.are.equal(keymap.escape('<LT>C-d>'), '<LT>C-d>')
end)
describe('evacuate', function() describe('evacuate', function()
before_each(spec.before) before_each(spec.before)
@@ -25,7 +19,7 @@ describe('keymap', function()
expr = true, expr = true,
noremap = false, noremap = false,
}) })
local fallback = keymap.evacuate('i', '(') local fallback = keymap.evacuate(0, 'i', '(')
vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true) vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true)
assert.are.same({ '(' }, vim.api.nvim_buf_get_lines(0, 0, -1, true)) assert.are.same({ '(' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end) end)
@@ -39,7 +33,7 @@ describe('keymap', function()
expr = false, expr = false,
noremap = false, noremap = false,
}) })
local fallback = keymap.evacuate('i', '(') local fallback = keymap.evacuate(0, 'i', '(')
vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true) vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true)
assert.are.same({ '()' }, vim.api.nvim_buf_get_lines(0, 0, -1, true)) assert.are.same({ '()' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end) end)
@@ -52,7 +46,7 @@ describe('keymap', function()
expr = true, expr = true,
noremap = false, noremap = false,
}) })
local fallback = keymap.evacuate('i', '<Tab>') local fallback = keymap.evacuate(0, 'i', '<Tab>')
vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true) vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true)
assert.are.same({ 'foobar' }, vim.api.nvim_buf_get_lines(0, 0, -1, true)) assert.are.same({ 'foobar' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end) end)
@@ -61,7 +55,7 @@ describe('keymap', function()
expr = true, expr = true,
noremap = false, noremap = false,
}) })
local fallback = keymap.evacuate('i', '<Tab>') local fallback = keymap.evacuate(0, 'i', '<Tab>')
vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true) vim.api.nvim_feedkeys('i' .. keymap.t(fallback.keys), fallback.mode .. 'x', true)
assert.are.same({ '\taiueo' }, vim.api.nvim_buf_get_lines(0, 0, -1, true)) assert.are.same({ '\taiueo' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end) end)