vim/nvim-cmp: clean up config
This commit is contained in:
parent
3116dd13e6
commit
026b4e8ddc
1 changed files with 28 additions and 41 deletions
|
@ -1,54 +1,48 @@
|
||||||
local config = function()
|
local config = function()
|
||||||
local cmp = require('cmp')
|
local cmp = require('cmp')
|
||||||
|
local map = cmp.mapping
|
||||||
|
|
||||||
local partial = require('user.util').partial
|
|
||||||
local flip = require('user.util').flip
|
local flip = require('user.util').flip
|
||||||
|
local partial = require('user.util').partial
|
||||||
|
|
||||||
-- assign('i', { key = func, ... }) == { key = { i = func }, ... }
|
-- assign('i', { key = func, ... }) == { key = { i = func }, ... }
|
||||||
-- assign({'i', 'c'}, { key = func, ... }) == { key = { i = func, c = 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 }
|
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
|
end
|
||||||
|
|
||||||
local invoke_fallback = function(fallback) fallback() end
|
local cond = function(condition, yes, no)
|
||||||
|
|
||||||
local when = function(condition)
|
|
||||||
return function(opts)
|
|
||||||
local yes = opts.yes or invoke_fallback
|
|
||||||
local no = opts.no or invoke_fallback
|
|
||||||
return function(fallback)
|
return function(fallback)
|
||||||
if condition() then yes(fallback) else no(fallback) end
|
if condition() then yes(fallback) else no(fallback) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
local keymap = {
|
local keymap = {
|
||||||
['<c-n>'] = when(cmp.visible) {
|
['<c-n>'] = cond(cmp.visible,
|
||||||
yes = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select },
|
map.select_next_item { behavior = cmp.SelectBehavior.Select },
|
||||||
no = cmp.mapping.complete(),
|
map.complete()),
|
||||||
},
|
['<c-p>'] = cond(cmp.visible,
|
||||||
|
map.select_prev_item { behavior = cmp.SelectBehavior.Select },
|
||||||
|
map.complete()),
|
||||||
|
|
||||||
['<c-p>'] = when(cmp.visible) {
|
['<down>'] = map.select_next_item { behavior = cmp.SelectBehavior.Select },
|
||||||
yes = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select },
|
['<up>'] = map.select_prev_item { behavior = cmp.SelectBehavior.Select },
|
||||||
no = cmp.mapping.complete(),
|
|
||||||
},
|
|
||||||
|
|
||||||
['<down>'] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select },
|
['<c-f>'] = map.scroll_docs( 3),
|
||||||
['<up>'] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select },
|
['<s-down>'] = map.scroll_docs( 3),
|
||||||
|
['<c-b>'] = map.scroll_docs(-3),
|
||||||
|
['<s-up>'] = map.scroll_docs(-3),
|
||||||
|
|
||||||
['<c-f>'] = cmp.mapping.scroll_docs(4),
|
['<c-e>'] = map.abort(),
|
||||||
['<s-down>'] = cmp.mapping.scroll_docs(4),
|
['<c-y>'] = map.confirm { select = true },
|
||||||
['<c-b>'] = cmp.mapping.scroll_docs(-4),
|
['<tab>'] = cond(cmp.visible,
|
||||||
['<s-up>'] = cmp.mapping.scroll_docs(-4),
|
map.confirm { select = true },
|
||||||
|
function(fallback) fallback() end),
|
||||||
['<c-e>'] = cmp.mapping.abort(),
|
|
||||||
['<c-y>'] = cmp.mapping.confirm { select = true },
|
|
||||||
['<tab>'] = when(cmp.visible) { yes = cmp.mapping.confirm { select = true } },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp.setup {
|
cmp.setup {
|
||||||
mapping = assign('i', keymap),
|
mapping = assign_keymap('i', keymap),
|
||||||
|
|
||||||
enabled = function()
|
enabled = function()
|
||||||
local c = require 'cmp.config.context'
|
local c = require 'cmp.config.context'
|
||||||
|
@ -123,17 +117,10 @@ local config = function()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cmp.setup.cmdline(':', {
|
cmp.setup.cmdline(':', {
|
||||||
mapping = assign(
|
mapping = assign_keymap('c', vim.tbl_extend('force', keymap, {
|
||||||
'c',
|
['<tab>'] = cond(cmp.visible, map.confirm {select = true }, map.complete()),
|
||||||
vim.tbl_extend('force', keymap, {
|
})),
|
||||||
['<tab>'] = when(cmp.visible) {
|
|
||||||
yes = cmp.mapping.confirm { select = true },
|
|
||||||
no = cmp.mapping.complete(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
),
|
|
||||||
|
|
||||||
completion = {
|
completion = {
|
||||||
autocomplete = false,
|
autocomplete = false,
|
||||||
|
|
Loading…
Add table
Reference in a new issue