Refactor keymap

This commit is contained in:
hrsh7th
2021-09-13 16:43:23 +09:00
parent af73e87fd8
commit f3af9e2ee6
2 changed files with 51 additions and 45 deletions

View File

@@ -93,25 +93,11 @@ keymap.listen = setmetatable({
keys = keymap.to_keymap(keys) keys = keymap.to_keymap(keys)
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
if keymap.listen.cache:get({ mode, bufnr, keys }) then if self.cache:get({ mode, bufnr, keys }) then
return return
end end
local existing = { local fallback = keymap.evacuate(mode, keys)
lhs = keys,
rhs = keys,
expr = 0,
nowait = 0,
noremap = 1,
}
for _, map in ipairs(keymap._getmaps(mode)) do
if map.lhs == keys then
existing = map
break
end
end
local fallback = keymap._evacuate(mode, existing)
vim.api.nvim_buf_set_keymap(0, mode, keys, ('<Cmd>call v:lua.cmp.utils.keymap.listen.run("%s", "%s")<CR>'):format(mode, str.escape(keymap.escape(keys), { '"' })), { vim.api.nvim_buf_set_keymap(0, mode, keys, ('<Cmd>call v:lua.cmp.utils.keymap.listen.run("%s", "%s")<CR>'):format(mode, str.escape(keymap.escape(keys), { '"' })), {
expr = false, expr = false,
noremap = true, noremap = true,
@@ -120,7 +106,6 @@ keymap.listen = setmetatable({
}) })
self.cache:set({ mode, bufnr, keys }, { self.cache:set({ mode, bufnr, keys }, {
mode = mode, mode = mode,
existing = existing,
callback = callback, callback = callback,
fallback = fallback, fallback = fallback,
}) })
@@ -138,9 +123,10 @@ end)
---Evacuate existing key mapping ---Evacuate existing key mapping
---@param mode string ---@param mode string
---@param map table ---@param lhs string
---@return string ---@return string
keymap._evacuate = function(mode, map) keymap.evacuate = function(mode, lhs)
local map = keymap.find_map_by_lhs(mode, lhs)
-- 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 map.noremap == 0 then if map.noremap == 0 then
@@ -165,18 +151,30 @@ keymap._evacuate = function(mode, map)
return fallback return fallback
end end
---Get all available key mappings. ---Get specific key mapping
---@param mode string ---@param mode string
---@return table[] ---@param lhs string
keymap._getmaps = function(mode) ---@return table
local maps = {} 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
table.insert(maps, map) if map.lhs == lhs then
return map
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
table.insert(maps, map) if map.lhs == lhs then
return map
end
end end
return maps return {
lhs = lhs,
rhs = lhs,
expr = 0,
script = 0,
noremap = 1,
nowait = 0,
silent = 1,
}
end end
return keymap return keymap

View File

@@ -17,26 +17,34 @@ describe('keymap', function()
assert.are.equal(keymap.escape('<LT>C-d>'), '<LT>C-d>') assert.are.equal(keymap.escape('<LT>C-d>'), '<LT>C-d>')
end) end)
it('_evacuate', function() describe('evacuate', function()
local s = function(cmd, keys, buf)
spec.before() before_each(spec.before)
vim.cmd(cmd)
local existing = vim.tbl_filter(function(map) it('expr & register', function()
return map.lhs == keys vim.api.nvim_buf_set_keymap(0, 'i', '(', [['<C-r>="("<CR>']], {
end, keymap._getmaps('i'))[1] or { expr = true,
lhs = keys, noremap = false,
rhs = keys, })
expr = 0, local fallback = keymap.evacuate('i', '(')
nowait = 0,
noremap = 1,
}
local fallback = keymap._evacuate('i', existing)
vim.api.nvim_feedkeys('i' .. keymap.t(fallback), 'x', true) vim.api.nvim_feedkeys('i' .. keymap.t(fallback), 'x', true)
assert.are.same(vim.api.nvim_buf_get_lines(0, 0, -1, true), buf) assert.are.same({ '(' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end end)
s([[]], '(', { '(' })
s([[imap <expr> ( '<C-r>="("<CR>']], '(', { '(' }) it('recursive & <Plug> (tpope/vim-endwise)', function()
s([[imap ( (]], '(', { '(' }) vim.api.nvim_buf_set_keymap(0, 'i', '<Plug>(paren-close)', [[)<Left>]], {
expr = false,
noremap = true,
})
vim.api.nvim_buf_set_keymap(0, 'i', '(', [[(<Plug>(paren-close)]], {
expr = false,
noremap = false,
})
local fallback = keymap.evacuate('i', '(')
vim.api.nvim_feedkeys('i' .. keymap.t(fallback), 'x', true)
assert.are.same({ '()' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end)
end) end)
end) end)