nvim/lua/completion.lua

62 lines
1.7 KiB
Lua
Raw Normal View History

local cmp = require'cmp'
local snippy = require('snippy')
cmp.setup({
snippet = {
expand = function(args)
snippy.expand_snippet(args.body)
end
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
-- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
['<CR>'] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif snippy.can_expand_or_advance() then
snippy.expand_or_advance()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif snippy.can_jump(-1) then
snippy.previous()
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'snippy' },
{ name = 'buffer' },
}),
formatting = {
fields = {
cmp.ItemField.Abbr,
cmp.ItemField.Kind,
cmp.ItemField.Menu,
},
format = function(entry, vim_item)
vim_item.abbr = string.sub(vim_item.abbr, 1, 20)
vim_item.kind = string.sub(vim_item.kind, 1, 10)
vim_item.menu = string.sub(vim_item.menu, 1, 10)
return vim_item
end
}
})