nvim: refactor completion key mappings
This commit is contained in:
parent
7952eeed2f
commit
cbe4a440f7
1 changed files with 29 additions and 36 deletions
|
@ -1,10 +1,10 @@
|
||||||
local icons = require("fschauen.util.icons")
|
local icons = require("fschauen.util.icons")
|
||||||
|
|
||||||
local make_keymap = function(cmp)
|
local make_keymap = function(cmp)
|
||||||
local select = { behavior = cmp.SelectBehavior.Select }
|
|
||||||
|
|
||||||
local if_visible = function(yes, no)
|
local if_visible = function(yes, no)
|
||||||
no = no or function(fallback) fallback() end
|
no = no or function(fallback)
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
return function(fallback)
|
return function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
yes(fallback)
|
yes(fallback)
|
||||||
|
@ -14,42 +14,35 @@ local make_keymap = function(cmp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Mappings that should work in both command line and Insert mode.
|
local select = { behavior = cmp.SelectBehavior.Select }
|
||||||
local common = {
|
local next_or_complete = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete())
|
||||||
-- stylua: ignore start
|
local prev_or_complete = if_visible(cmp.mapping.select_prev_item(select), cmp.mapping.complete())
|
||||||
["<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()),
|
|
||||||
|
|
||||||
["<c-j>"] = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()),
|
local cmp_map = function(rhs, modes)
|
||||||
["<c-k>"] = if_visible(cmp.mapping.select_prev_item(select)),
|
if modes == nil then
|
||||||
|
modes = { "i", "c" }
|
||||||
["<down>"] = cmp.mapping.select_next_item(select),
|
elseif type(modes) ~= "table" then
|
||||||
["<up>"] = cmp.mapping.select_prev_item(select),
|
modes = { modes }
|
||||||
|
end
|
||||||
["<c-f>"] = cmp.mapping.scroll_docs( 3),
|
return cmp.mapping(rhs, modes)
|
||||||
["<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 }
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Add table
Reference in a new issue