mirror of
https://github.com/hrsh7th/nvim-cmp
synced 2024-09-16 20:54:03 +02:00
Refactor a bit Fix #968
This commit is contained in:
parent
dc83a156e0
commit
18626477a6
3 changed files with 59 additions and 59 deletions
|
@ -108,7 +108,6 @@ cmp.close = cmp.sync(function()
|
|||
if cmp.core.view:visible() then
|
||||
local release = cmp.core:suspend()
|
||||
cmp.core.view:close()
|
||||
cmp.core:reset()
|
||||
vim.schedule(release)
|
||||
return true
|
||||
else
|
||||
|
@ -131,15 +130,13 @@ end)
|
|||
---Select next item if possible
|
||||
cmp.select_next_item = cmp.sync(function(option)
|
||||
option = option or {}
|
||||
option.behavior = option.behavior or cmp.SelectBehavior.Insert
|
||||
|
||||
if cmp.core.view:visible() then
|
||||
local release = cmp.core:suspend()
|
||||
cmp.core.view:select_next_item(option)
|
||||
vim.schedule(release)
|
||||
return true
|
||||
elseif vim.fn.pumvisible() == 1 then
|
||||
-- Special handling for native pum. Required to facilitate key mapping processing.
|
||||
if (option.behavior or cmp.SelectBehavior.Insert) == cmp.SelectBehavior.Insert then
|
||||
if option.behavior == cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-n>'), 'in')
|
||||
else
|
||||
feedkeys.call(keymap.t('<Down>'), 'in')
|
||||
|
@ -152,15 +149,13 @@ end)
|
|||
---Select prev item if possible
|
||||
cmp.select_prev_item = cmp.sync(function(option)
|
||||
option = option or {}
|
||||
option.behavior = option.behavior or cmp.SelectBehavior.Insert
|
||||
|
||||
if cmp.core.view:visible() then
|
||||
local release = cmp.core:suspend()
|
||||
cmp.core.view:select_prev_item(option)
|
||||
vim.schedule(release)
|
||||
return true
|
||||
elseif vim.fn.pumvisible() == 1 then
|
||||
-- Special handling for native pum. Required to facilitate key mapping processing.
|
||||
if (option.behavior or cmp.SelectBehavior.Insert) == cmp.SelectBehavior.Insert then
|
||||
if option.behavior == cmp.SelectBehavior.Insert then
|
||||
feedkeys.call(keymap.t('<C-p>'), 'in')
|
||||
else
|
||||
feedkeys.call(keymap.t('<Up>'), 'in')
|
||||
|
@ -183,25 +178,35 @@ end)
|
|||
---Confirm completion
|
||||
cmp.confirm = cmp.sync(function(option, callback)
|
||||
option = option or {}
|
||||
callback = callback or function() end
|
||||
option.select = option.select or false
|
||||
option.behavior = option.behavior or cmp.ConfirmBehavior.Insert
|
||||
callback = callback or (function() end)
|
||||
|
||||
local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil)
|
||||
if e then
|
||||
cmp.core:confirm(e, {
|
||||
behavior = option.behavior,
|
||||
}, function()
|
||||
callback()
|
||||
cmp.core:complete(cmp.core:get_context({ reason = cmp.ContextReason.TriggerOnly }))
|
||||
end)
|
||||
return true
|
||||
else
|
||||
-- Special handling for native puma. Required to facilitate key mapping processing.
|
||||
if vim.fn.complete_info({ 'selected' }).selected ~= -1 then
|
||||
feedkeys.call(keymap.t('<C-y>'), 'in')
|
||||
if cmp.core.view:visible() then
|
||||
local e = cmp.core.view:get_selected_entry()
|
||||
if not e and option.select then
|
||||
e = cmp.core.view:get_first_entry()
|
||||
end
|
||||
if e then
|
||||
cmp.core:confirm(e, {
|
||||
behavior = option.behavior,
|
||||
}, function()
|
||||
callback()
|
||||
cmp.core:complete(cmp.core:get_context({ reason = cmp.ContextReason.TriggerOnly }))
|
||||
end)
|
||||
return true
|
||||
end
|
||||
elseif vim.fn.pumvisible() == 1 then
|
||||
local index = vim.fn.complete_info({ 'selected' }).selected
|
||||
if index == -1 and option.select then
|
||||
index = 0
|
||||
end
|
||||
if index ~= -1 then
|
||||
vim.api.nvim_select_popupmenu_item(index, true, true, {})
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
return false
|
||||
end)
|
||||
|
||||
---Show status
|
||||
|
|
|
@ -6,7 +6,7 @@ local lsp = {}
|
|||
|
||||
lsp.Position = {
|
||||
---Convert lsp.Position to vim.Position
|
||||
---@param buf integer|string
|
||||
---@param buf integer
|
||||
---@param position lsp.Position
|
||||
---@return vim.Position
|
||||
to_vim = function(buf, position)
|
||||
|
@ -26,7 +26,7 @@ lsp.Position = {
|
|||
}
|
||||
end,
|
||||
---Convert vim.Position to lsp.Position
|
||||
---@param buf integer|string
|
||||
---@param buf integer
|
||||
---@param position vim.Position
|
||||
---@return lsp.Position
|
||||
to_lsp = function(buf, position)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue