This commit is contained in:
hrsh7th
2021-10-26 23:37:11 +09:00
parent 32e22e64c6
commit b5899f05c5
2 changed files with 34 additions and 38 deletions

View File

@@ -5,15 +5,6 @@ local api = require('cmp.utils.api')
local keymap = {}
---The mapping of vim notation and chars.
keymap._table = {
['<CR>'] = { '\n', '\r', '\r\n' },
['<Tab>'] = { '\t' },
['<BSlash>'] = { '\\' },
['<Bar>'] = { '|' },
['<Space>'] = { ' ' },
}
---Shortcut for nvim_replace_termcodes
---@param keys string
---@return string
@@ -36,42 +27,40 @@ keymap.escape = function(keys)
return keys
end
---Return upper case key sequence.
---Normalize key sequence.
---@param keys string
---@return string
keymap.to_upper = function(keys)
local result = {}
local ctrl = false
for i = 1, #keys do
local c = string.sub(keys, i, i)
if c == '<' then
table.insert(result, c)
ctrl = true
elseif ctrl and c ~= '>' then
table.insert(result, string.upper(c))
elseif ctrl and c == '>' then
table.insert(result, c)
ctrl = false
else
table.insert(result, c)
keymap.normalize = function(keys)
vim.api.nvim_set_keymap('t', '<Plug>(cmp.utils.keymap.normalize)', keys, {})
for _, map in ipairs(vim.api.nvim_get_keymap('t')) do
if map.lhs == '<Plug>(cmp.utils.keymap.normalize)' then
return map.rhs
end
end
return table.concat(result, '')
return keys
end
---Return vim notation keymapping (simple conversion).
---@param s string
---@return string
keymap.to_keymap = function(s)
keymap.to_keymap = setmetatable({
['<CR>'] = { '\n', '\r', '\r\n' },
['<Tab>'] = { '\t' },
['<BSlash>'] = { '\\' },
['<Bar>'] = { '|' },
['<Space>'] = { ' ' },
}, {
__call = function(self, s)
return string.gsub(s, '.', function(c)
for key, chars in pairs(keymap._table) do
for key, chars in pairs(self) do
if vim.tbl_contains(chars, c) then
return key
end
end
return c
end)
end
end
})
---Mode safe break undo
keymap.undobreak = function()
@@ -222,7 +211,7 @@ keymap.listen = setmetatable({
cache = cache.new(),
}, {
__call = function(self, mode, keys_or_chars, callback)
local keys = keymap.to_upper(keymap.to_keymap(keys_or_chars))
local keys = keymap.normalize(keymap.to_keymap(keys_or_chars))
local bufnr = vim.api.nvim_get_current_buf()
local existing = keymap.find_map_by_lhs(mode, keys)
@@ -303,9 +292,9 @@ end)
---@param rhs string
---@return string
keymap.recursive = function(mode, lhs, rhs)
rhs = keymap.to_upper(rhs)
rhs = keymap.normalize(rhs)
local fallback_lhs = ('<Plug>(cmp-utils-keymap-listen-lhs:%s)'):format(lhs)
local new_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.to_upper(lhs)), fallback_lhs)
local new_rhs = string.gsub(rhs, '^' .. vim.pesc(keymap.normalize(lhs)), fallback_lhs)
if not keymap.equals(new_rhs, rhs) then
vim.api.nvim_buf_set_keymap(0, mode, fallback_lhs, lhs, {
expr = false,

View File

@@ -92,5 +92,12 @@ describe('keymap', function()
vim.api.nvim_feedkeys(keymap.t('iaiueo<CR>a<C-n><C-n>'), 'tx', true)
assert.are.same({ 'aiueo', 'aiueo' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end)
it('#414', function()
keymap.listen('i', '<M-j>', function()
vim.api.nvim_feedkeys(keymap.t('<C-n>'), 'int', true)
end)
vim.api.nvim_feedkeys(keymap.t('iaiueo<CR>a<M-j><M-j>'), 'tx', true)
assert.are.same({ 'aiueo', 'aiueo' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end)
end)
end)