This commit is contained in:
hrsh7th
2021-08-17 00:53:38 +09:00
parent e4deb0142a
commit d56bf1d506
5 changed files with 49 additions and 28 deletions

View File

@@ -2,55 +2,66 @@ local types = require('cmp.types')
local mapping = {}
mapping.mode = function(modes, action)
return setmetatable({
modes = modes or { 'i' },
action = action,
}, {
__call = function(_, ...)
action(...)
end,
})
end
mapping.complete = function()
return function(core)
return mapping.mode({ 'i' }, function(core)
core.complete(core.get_context({ reason = types.cmp.ContextReason.Manual }))
end
end)
end
mapping.close = function()
return function(core, fallback)
return mapping.mode({ 'i' }, function(core, fallback)
if vim.fn.pumvisible() == 1 then
core.reset()
else
fallback()
end
end
end)
end
mapping.scroll = function(delta)
return function(core, fallback)
return mapping.mode({ 'i' }, function(core, fallback)
if core.menu.float:is_visible() then
core.menu.float:scroll(delta)
else
fallback()
end
end
end)
end
mapping.next_item = function()
return function(_, fallback)
return mapping.mode({ 'i' }, function(_, fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-n>', true, true, true), 'n')
else
fallback()
end
end
end)
end
mapping.prev_item = function()
return function(_, fallback)
return mapping.mode({ 'i' }, function(_, fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<C-p>', true, true, true), 'n')
else
fallback()
end
end
end)
end
mapping.confirm = function(option)
option = option or {}
return function(core, fallback)
return mapping.mode({ 'i' }, function(core, fallback)
local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
if e then
core.confirm(e, {
@@ -61,7 +72,7 @@ mapping.confirm = function(option)
else
fallback()
end
end
end)
end
return mapping