nvim: refactor completion key mappings

This commit is contained in:
Fernando Schauenburg 2024-07-17 23:20:50 +02:00
parent 7952eeed2f
commit cbe4a440f7

View file

@ -1,10 +1,10 @@
local icons = require("fschauen.util.icons")
local make_keymap = function(cmp)
local select = { behavior = cmp.SelectBehavior.Select }
local if_visible = function(yes, no)
no = no or function(fallback) fallback() end
no = no or function(fallback)
fallback()
end
return function(fallback)
if cmp.visible() then
yes(fallback)
@ -14,42 +14,35 @@ local make_keymap = function(cmp)
end
end
-- Mappings that should work in both command line and Insert mode.
local common = {
-- stylua: ignore start
["<c-n>"] = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()),
["<c-p>"] = if_visible(cmp.mapping.select_prev_item(select), cmp.mapping.complete()),
local select = { behavior = cmp.SelectBehavior.Select }
local next_or_complete = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete())
local prev_or_complete = if_visible(cmp.mapping.select_prev_item(select), cmp.mapping.complete())
["<c-j>"] = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()),
["<c-k>"] = if_visible(cmp.mapping.select_prev_item(select)),
["<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 },
-- stylua: ignore end
}
-- I want <tab> to start completion on the command line, but not in Insert.
local keymap = {
["<tab>"] = {
i = if_visible(cmp.mapping.confirm { select = true }),
c = if_visible(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 }
local cmp_map = function(rhs, modes)
if modes == nil then
modes = { "i", "c" }
elseif type(modes) ~= "table" then
modes = { modes }
end
return cmp.mapping(rhs, modes)
end
return cmp.mapping.preset.insert(keymap)
-- Mappings that should work in both command line and Insert mode.
return {
-- stylua: ignore start
["<c-n>"] = cmp_map(next_or_complete),
["<c-p>"] = cmp_map(prev_or_complete),
["<c-j>"] = cmp_map(next_or_complete),
["<c-k>"] = cmp_map(prev_or_complete),
["<s-down>"] = cmp_map(cmp.mapping.scroll_docs( 3)),
["<s-up>"] = cmp_map(cmp.mapping.scroll_docs(-3)),
["<c-e>"] = cmp_map(cmp.mapping.abort()),
["<c-y>"] = cmp_map(cmp.mapping.confirm { select = true }),
-- stylua: ignore end
}
end
return {