Refactor a bit

This commit is contained in:
hrsh7th
2022-08-20 19:23:05 +09:00
parent 9417f48a99
commit 49c4050e24
3 changed files with 59 additions and 55 deletions

View File

@@ -102,8 +102,7 @@ keymap.listen = function(mode, lhs, callback)
lhs = keymap.normalize(keymap.to_keymap(lhs))
local existing = keymap.get_map(mode, lhs)
local id = string.match(existing.rhs, 'v:lua%.cmp%.utils%.keymap%.set_map%((%d+)%)')
if id and keymap.set_map.callbacks[tonumber(id, 10)] then
if existing.desc == 'cmp.utils.keymap.set_map' then
return
end
@@ -128,8 +127,8 @@ end
keymap.fallback = function(bufnr, mode, map)
return function()
if map.expr then
local fallback_expr = string.format('<Plug>(cmp.u.k.fallback_expr:%s)', map.lhs)
keymap.set_map(bufnr, mode, fallback_expr, function()
local fallback_lhs = string.format('<Plug>(cmp.u.k.fallback_expr:%s)', map.lhs)
keymap.set_map(bufnr, mode, fallback_lhs, function()
return keymap.solve(bufnr, mode, map).keys
end, {
expr = true,
@@ -138,12 +137,12 @@ keymap.fallback = function(bufnr, mode, map)
nowait = map.nowait,
silent = map.silent and mode ~= 'c',
})
vim.api.nvim_feedkeys(keymap.t(fallback_expr), 'im', true)
elseif not map.callback then
vim.api.nvim_feedkeys(keymap.t(fallback_lhs), 'im', true)
elseif map.callback then
map.callback()
else
local solved = keymap.solve(bufnr, mode, map)
vim.api.nvim_feedkeys(solved.keys, solved.mode, true)
else
map.callback()
end
end
end
@@ -151,7 +150,14 @@ end
---Solve
keymap.solve = function(bufnr, mode, map)
local lhs = keymap.t(map.lhs)
local rhs = map.expr and (map.callback and map.callback() or vim.api.nvim_eval(keymap.t(map.rhs))) or keymap.t(map.rhs)
local rhs = keymap.t(map.rhs)
if map.expr then
if map.callback then
rhs = map.callback()
else
rhs = vim.api.nvim_eval(keymap.t(map.rhs))
end
end
if map.noremap then
return { keys = rhs, mode = 'in' }
@@ -161,7 +167,7 @@ keymap.solve = function(bufnr, mode, map)
local recursive = string.format('<SNR>0_(cmp.u.k.recursive:%s)', lhs)
keymap.set_map(bufnr, mode, recursive, lhs, {
noremap = true,
script = map.script,
script = true,
nowait = map.nowait,
silent = map.silent and mode ~= 'c',
})
@@ -223,29 +229,18 @@ keymap.get_map = function(mode, lhs)
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
keymap.set_map = function(bufnr, mode, lhs, rhs, opts)
if type(rhs) == 'function' then
opts.callback = rhs
rhs = ''
end
opts.desc = 'cmp.utils.keymap.set_map'
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]() or ''
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
return keymap