From cbe4a440f7ac0abe5cf5d2175d789e2854d072c1 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Wed, 17 Jul 2024 23:20:50 +0200 Subject: [PATCH] nvim: refactor completion key mappings --- .../nvim/lua/fschauen/plugins/completion.lua | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/config/nvim/lua/fschauen/plugins/completion.lua b/config/nvim/lua/fschauen/plugins/completion.lua index 598724a..f8a68a8 100644 --- a/config/nvim/lua/fschauen/plugins/completion.lua +++ b/config/nvim/lua/fschauen/plugins/completion.lua @@ -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 - [""] = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()), - [""] = 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()) - [""] = if_visible(cmp.mapping.select_next_item(select), cmp.mapping.complete()), - [""] = if_visible(cmp.mapping.select_prev_item(select)), - - [""] = cmp.mapping.select_next_item(select), - [""] = cmp.mapping.select_prev_item(select), - - [""] = cmp.mapping.scroll_docs( 3), - [""] = cmp.mapping.scroll_docs( 3), - [""] = cmp.mapping.scroll_docs(-3), - [""] = cmp.mapping.scroll_docs(-3), - - [""] = cmp.mapping.abort(), - [""] = cmp.mapping.confirm { select = true }, - -- stylua: ignore end - } - - -- I want to start completion on the command line, but not in Insert. - local keymap = { - [""] = { - 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 + [""] = cmp_map(next_or_complete), + [""] = cmp_map(prev_or_complete), + + [""] = cmp_map(next_or_complete), + [""] = cmp_map(prev_or_complete), + + [""] = cmp_map(cmp.mapping.scroll_docs( 3)), + [""] = cmp_map(cmp.mapping.scroll_docs(-3)), + + [""] = cmp_map(cmp.mapping.abort()), + [""] = cmp_map(cmp.mapping.confirm { select = true }), + -- stylua: ignore end + } end return {