From 026b4e8ddce0b73b5e6c1e49cfabc81b47803e3f Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 16 Jul 2023 18:13:51 +0200 Subject: [PATCH] vim/nvim-cmp: clean up config --- config/nvim/lua/user/plugins/completion.lua | 69 +++++++++------------ 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/config/nvim/lua/user/plugins/completion.lua b/config/nvim/lua/user/plugins/completion.lua index 3b53946..52859c9 100644 --- a/config/nvim/lua/user/plugins/completion.lua +++ b/config/nvim/lua/user/plugins/completion.lua @@ -1,54 +1,48 @@ local config = function() local cmp = require('cmp') + local map = cmp.mapping - local partial = require('user.util').partial local flip = require('user.util').flip + local partial = require('user.util').partial -- assign('i', { key = func, ... }) == { key = { i = func }, ... } -- assign({'i', 'c'}, { key = func, ... }) == { key = { i = func, c = func }, ...} - local assign = function(modes, tbl) + local assign_keymap = function(modes, keymap) modes = type(modes) == 'table' and modes or { modes } - return vim.tbl_map(partial(flip(cmp.mapping), modes), tbl) + return vim.tbl_map(partial(flip(map), modes), keymap) end - local invoke_fallback = function(fallback) fallback() end - - local when = function(condition) - return function(opts) - local yes = opts.yes or invoke_fallback - local no = opts.no or invoke_fallback - return function(fallback) - if condition() then yes(fallback) else no(fallback) end - end + local cond = function(condition, yes, no) + return function(fallback) + if condition() then yes(fallback) else no(fallback) end end end local keymap = { - [''] = when(cmp.visible) { - yes = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }, - no = cmp.mapping.complete(), - }, + [''] = cond(cmp.visible, + map.select_next_item { behavior = cmp.SelectBehavior.Select }, + map.complete()), + [''] = cond(cmp.visible, + map.select_prev_item { behavior = cmp.SelectBehavior.Select }, + map.complete()), - [''] = when(cmp.visible) { - yes = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }, - no = cmp.mapping.complete(), - }, + [''] = map.select_next_item { behavior = cmp.SelectBehavior.Select }, + [''] = map.select_prev_item { behavior = cmp.SelectBehavior.Select }, - [''] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }, - [''] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }, + [''] = map.scroll_docs( 3), + [''] = map.scroll_docs( 3), + [''] = map.scroll_docs(-3), + [''] = map.scroll_docs(-3), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(-4), - - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm { select = true }, - [''] = when(cmp.visible) { yes = cmp.mapping.confirm { select = true } }, + [''] = map.abort(), + [''] = map.confirm { select = true }, + [''] = cond(cmp.visible, + map.confirm { select = true }, + function(fallback) fallback() end), } cmp.setup { - mapping = assign('i', keymap), + mapping = assign_keymap('i', keymap), enabled = function() local c = require 'cmp.config.context' @@ -123,17 +117,10 @@ local config = function() }, } - cmp.setup.cmdline(':', { - mapping = assign( - 'c', - vim.tbl_extend('force', keymap, { - [''] = when(cmp.visible) { - yes = cmp.mapping.confirm { select = true }, - no = cmp.mapping.complete(), - } - }) - ), + mapping = assign_keymap('c', vim.tbl_extend('force', keymap, { + [''] = cond(cmp.visible, map.confirm {select = true }, map.complete()), + })), completion = { autocomplete = false,