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 = {} 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 ---Shortcut for nvim_replace_termcodes
---@param keys string ---@param keys string
---@return string ---@return string
@@ -36,42 +27,40 @@ keymap.escape = function(keys)
return keys return keys
end end
---Return upper case key sequence. ---Normalize key sequence.
---@param keys string ---@param keys string
---@return string ---@return string
keymap.to_upper = function(keys) keymap.normalize = function(keys)
local result = {} vim.api.nvim_set_keymap('t', '<Plug>(cmp.utils.keymap.normalize)', keys, {})
local ctrl = false for _, map in ipairs(vim.api.nvim_get_keymap('t')) do
for i = 1, #keys do if map.lhs == '<Plug>(cmp.utils.keymap.normalize)' then
local c = string.sub(keys, i, i) return map.rhs
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)
end end
end end
return table.concat(result, '') return keys
end end
---Return vim notation keymapping (simple conversion). ---Return vim notation keymapping (simple conversion).
---@param s string ---@param s string
---@return string ---@return string
keymap.to_keymap = function(s) keymap.to_keymap = setmetatable({
return string.gsub(s, '.', function(c) ['<CR>'] = { '\n', '\r', '\r\n' },
for key, chars in pairs(keymap._table) do ['<Tab>'] = { '\t' },
if vim.tbl_contains(chars, c) then ['<BSlash>'] = { '\\' },
return key ['<Bar>'] = { '|' },
['<Space>'] = { ' ' },
}, {
__call = function(self, s)
return string.gsub(s, '.', function(c)
for key, chars in pairs(self) do
if vim.tbl_contains(chars, c) then
return key
end
end end
end return c
return c end)
end) end
end })
---Mode safe break undo ---Mode safe break undo
keymap.undobreak = function() keymap.undobreak = function()
@@ -222,7 +211,7 @@ keymap.listen = setmetatable({
cache = cache.new(), cache = cache.new(),
}, { }, {
__call = function(self, mode, keys_or_chars, callback) __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 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)
@@ -303,9 +292,9 @@ end)
---@param rhs string ---@param rhs string
---@return string ---@return string
keymap.recursive = function(mode, lhs, rhs) 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 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 if not keymap.equals(new_rhs, rhs) then
vim.api.nvim_buf_set_keymap(0, mode, fallback_lhs, lhs, { vim.api.nvim_buf_set_keymap(0, mode, fallback_lhs, lhs, {
expr = false, 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) 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)) assert.are.same({ 'aiueo', 'aiueo' }, vim.api.nvim_buf_get_lines(0, 0, -1, true))
end) 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)
end) end)