Revert expr mapping handling

This commit is contained in:
hrsh7th
2022-01-15 20:45:21 +09:00
parent 76d92fca64
commit 2ed0cdab36
2 changed files with 46 additions and 67 deletions

View File

@@ -1,4 +1,3 @@
local cache = require('cmp.utils.cache')
local misc = require('cmp.utils.misc')
local api = require('cmp.utils.api')
@@ -102,16 +101,15 @@ keymap.listen = function(mode, lhs, callback)
end
local bufnr = existing.buffer and vim.api.nvim_get_current_buf() or -1
local fallback = keymap.evacuate(bufnr, mode, existing)
keymap.set_map(bufnr, mode, lhs, function()
if mode == 'c' and vim.fn.getcmdtype() == '=' then
return vim.api.nvim_feedkeys(keymap.t(fallback), 'it', true)
return keymap.feed_map(existing)
end
callback(
lhs,
misc.once(function()
return vim.api.nvim_feedkeys(keymap.t(fallback), 'it', true)
keymap.feed_map(existing)
end)
)
end, {
@@ -121,59 +119,6 @@ keymap.listen = function(mode, lhs, callback)
})
end
---Evacuate existing keymapping.
---@param bufnr number
---@param mode string
---@param existing table
---@return string
keymap.evacuate = setmetatable({
cache = cache.new(),
}, {
__call = function(self, bufnr, mode, existing)
local fallback = self.cache:ensure({ bufnr, mode, existing.lhs }, function()
return string.format('<Plug>(cmp.u.k.evacuate:%s)', misc.id('cmp.utils.keymap.evacuate'))
end)
local callback = not existing.expr and existing.callback
keymap.set_map(bufnr, mode, fallback, function()
-- Make resolved key sequence.
local lhs = keymap.t(existing.lhs)
local rhs
if existing.callback then
rhs = existing.callback()
elseif existing.expr then
rhs = vim.api.nvim_eval(keymap.t(existing.rhs))
else
rhs = keymap.t(existing.rhs)
end
-- Resolve recursive mapping. See `:help recursive_mapping`.
if not existing.noremap then
if string.find(rhs, lhs, 1, true) == 1 then
rhs = string.gsub(rhs, '^' .. vim.pesc(lhs), string.format(keymap.t([[<C-r>=v:lua.vim.json.decode(%s)<CR>]]), vim.fn.string(vim.json.encode(lhs))))
end
end
return rhs
end, callback and {
expr = false,
callback = callback,
noremap = existing.noremap,
script = existing.script,
silent = mode ~= 'c',
nowait = existing.nowait,
} or {
expr = true,
noremap = existing.noremap,
script = existing.script,
silent = mode ~= 'c',
nowait = existing.nowait,
})
return fallback
end,
})
---Get map
---@param mode string
---@param lhs string
@@ -220,12 +165,40 @@ keymap.get_map = function(mode, lhs)
callback = nil,
noremap = true,
script = false,
silent = true,
silent = false,
nowait = false,
buffer = false,
}
end
---Feed mapping object.
---@param map table
keymap.feed_map = function(map)
local lhs = keymap.t(map.lhs)
local rhs
if map.callback and not map.expr then
return map.callback()
elseif map.callback and map.expr then
rhs = map.callback()
elseif map.expr then
rhs = vim.api.nvim_eval(keymap.t(map.rhs))
else
rhs = keymap.t(map.rhs)
end
if map.noremap then
vim.api.nvim_feedkeys(rhs, 'itn', true)
else
if string.find(rhs, lhs, 1, true) == 1 then
rhs = string.gsub(rhs, '^' .. vim.pesc(lhs), '')
vim.api.nvim_feedkeys(rhs, 'itm', true)
vim.api.nvim_feedkeys(lhs, 'itn', true)
else
vim.api.nvim_feedkeys(rhs, 'itm', true)
end
end
end
---Set keymapping
keymap.set_map = setmetatable({
callbacks = {},