dotfiles/.config/nvim/lua/plugins/cmp.lua
2023-06-02 10:57:53 +02:00

88 lines
2.1 KiB
Lua

vim.opt.completeopt = {'menu', 'menuone', 'noselect', 'preview'}
vim.opt.shortmess = vim.opt.shortmess + { c = true}
vim.api.nvim_set_option('updatetime', 1000)
require('luasnip.loaders.from_vscode').lazy_load()
local cmp = require('cmp')
local luasnip = require('luasnip')
local select_opts = {behavior = cmp.SelectBehavior.Select}
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end
},
sources = {
{name = 'path'},
{name = 'nvim_lsp'},
{name = 'nvim_lsp_signature_help'},
{name = 'buffer', keyword_length = 2},
{name = 'luasnip', keyword_length = 2},
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered()
},
formatting = {
fields = {'menu', 'abbr', 'kind'},
format = function(entry, item)
local menu_icon = {
nvim_lsp = 'λ',
nvim_lsp_signature_help = 'λ“',
luasnip = '',
buffer = 'Ω',
path = '»',
}
item.menu = menu_icon[entry.source.name]
return item
end,
},
mapping = {
['<C-p>'] = cmp.mapping.select_prev_item(select_opts),
['<C-o>'] = cmp.mapping.select_next_item(select_opts),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({select = false}),
['<C-d>'] = cmp.mapping(function(fallback)
if luasnip.jumpable(1) then
luasnip.jump(1)
else
fallback()
end
end, {'i', 's'}),
['<C-b>'] = cmp.mapping(function(fallback)
if luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, {'i', 's'}),
['<Tab>'] = cmp.mapping(function(fallback)
local col = vim.fn.col('.') - 1
if cmp.visible() then
cmp.select_next_item(select_opts)
elseif col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
fallback()
else
cmp.complete()
end
end, {'i', 's'}),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item(select_opts)
else
fallback()
end
end, {'i', 's'}),
},
})