Files
code/basic/nvim/plugconf/cmp.lua
2023-06-10 15:18:53 +02:00

88 lines
2.1 KiB
Lua

local cmp = require 'cmp'
local context = require 'cmp.config.context'
vim.o.completeopt = nil
-- Plugin helper
local feedkey = function(key, mode)
key = vim.api.nvim_replace_termcodes(key, true, true, true)
vim.api.nvim_feedkeys(key, mode, true)
end
-- General setup
cmp.setup {
snippet = {
expand = function(args) vim.fn['vsnip#anonymous'](args.body) end,
},
window = {
documentation = { border = 'rounded' },
},
formatting = {
fields = { 'menu', 'abbr', 'kind' },
format = function(entry, item)
item.menu = ({
nvim_lsp = 'λ',
vsnip = '',
buffer = 'Ω',
path = '🖫',
})[entry.source.name]
return item
end,
},
preselect = cmp.PreselectMode.None,
mapping = {
-- Confirm, jump or select with tab
['<tab>'] = cmp.mapping(function(fallback)
if cmp.get_selected_entry() then
cmp.confirm()
elseif vim.fn['vsnip#jumpable'](1) == 1 then
feedkey('<plug>(vsnip-jump-next)', '')
elseif vim.fn['vsnip#expandable']() == 1 then
feedkey('<plug>(vsnip-expand)', '')
elseif cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, { 'i', 's' }),
-- Jump back with shift tab
['<s-tab>'] = cmp.mapping(function(fallback)
if vim.fn['vsnip#jumpable'](-1) == 1 then
feedkey('<plug>(vsnip-jump-prev)', '')
else
fallback()
end
end, { 'i', 's' }),
-- Navigate options with ctrl-n and ctrl-p
['<c-n>'] = cmp.mapping(function(fallback)
if not cmp.visible() then
cmp.complete()
else
cmp.select_next_item()
end
end),
['<c-p>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end),
},
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'vsnip' },
{ name = 'nvim_lua' },
}, {
{ name = 'buffer', keyword_length = 5 },
{ name = 'path' },
}),
completion = {
keyword_length = 3
},
-- Disable in comments
enabled = function()
return not context.in_syntax_group('Comment')
end,
}