vim: refactor completion to make it more maintainable

This commit is contained in:
Fernando Schauenburg 2024-02-12 00:35:44 +01:00
parent e7ccffd50c
commit fedd230111

View file

@ -12,59 +12,59 @@ M.dependencies = {
'saadparwaiz1/cmp_luasnip', 'saadparwaiz1/cmp_luasnip',
} }
M.event = 'InsertEnter' M.event = {
'CmdlineEnter',
'InsertEnter',
}
local repeat_mapping = function(value, keys) local make_keymap = function(cmp)
local tbl = {} local select = { behavior = cmp.SelectBehavior.Select }
for _, k in ipairs(keys) do tbl[k] = value end
return tbl
end
local transform_keymap = function(mappings, modes) local either = function(yes, no)
modes = modes or 'n'
modes = type(modes) == 'table' and modes or { modes }
local tbl = {}
for lhs, rhs in pairs(mappings) do
tbl[lhs] = repeat_mapping(rhs, modes)
end
return tbl
end
local cond = function(condition, yes, no)
return function(fallback) return function(fallback)
if condition() then yes(fallback) else no(fallback) end if cmp.visible() then yes(fallback) else no(fallback) end
end end
end end
-- Mappings that should work in both command line and Insert mode.
local common = {
['<c-n>'] = either(cmp.mapping.select_next_item(select), cmp.mapping.complete()),
['<c-p>'] = either(cmp.mapping.select_prev_item(select), cmp.mapping.complete()),
['<down>'] = cmp.mapping.select_next_item(select),
['<up>'] = cmp.mapping.select_prev_item(select),
['<c-f>'] = cmp.mapping.scroll_docs( 3),
['<s-down>'] = cmp.mapping.scroll_docs( 3),
['<c-b>'] = cmp.mapping.scroll_docs(-3),
['<s-up>'] = cmp.mapping.scroll_docs(-3),
['<c-e>'] = cmp.mapping.abort(),
['<c-y>'] = cmp.mapping.confirm { select = true },
}
-- I want <tab> to start completion on the command line, but not in Insert.
local keymap = {
['<tab>'] = {
i = either(cmp.mapping.confirm { select = true }, function(fallback) fallback() end),
c = either(cmp.mapping.confirm { select = true }, cmp.mapping.complete()),
}
}
-- Turn { lhs = rhs } into { lhs = { i = rhs, c = rhs } }.
for lhs, rhs in pairs(common) do
keymap[lhs] = { i = rhs, c = rhs }
end
return cmp.mapping.preset.insert(keymap)
end
M.config = function() M.config = function()
local cmp = require 'cmp' local cmp = require 'cmp'
local map = cmp.mapping local keymap = make_keymap(cmp)
local keymap = {
['<c-n>'] = cond(cmp.visible,
map.select_next_item { behavior = cmp.SelectBehavior.Select },
map.complete()),
['<c-p>'] = cond(cmp.visible,
map.select_prev_item { behavior = cmp.SelectBehavior.Select },
map.complete()),
['<down>'] = map.select_next_item { behavior = cmp.SelectBehavior.Select },
['<up>'] = map.select_prev_item { behavior = cmp.SelectBehavior.Select },
['<c-f>'] = map.scroll_docs( 3),
['<s-down>'] = map.scroll_docs( 3),
['<c-b>'] = map.scroll_docs(-3),
['<s-up>'] = map.scroll_docs(-3),
['<c-e>'] = map.abort(),
['<c-y>'] = map.confirm { select = true },
['<tab>'] = cond(cmp.visible,
map.confirm { select = true },
function(fallback) fallback() end),
}
cmp.setup { cmp.setup {
mapping = transform_keymap(keymap, 'i'), mapping = keymap,
enabled = function() enabled = function()
local c = require 'cmp.config.context' local c = require 'cmp.config.context'
@ -113,11 +113,7 @@ M.config = function()
} }
cmp.setup.cmdline(':', { cmp.setup.cmdline(':', {
mapping = transform_keymap( mapping = keymap,
vim.tbl_extend('force', keymap, {
['<tab>'] = cond(cmp.visible, map.confirm {select = true }, map.complete()),
}),
'c'),
completion = { completion = {
autocomplete = false, autocomplete = false,