theonepath.nvim/lua/plugins/configs/completion/cmp.lua
2024-10-02 17:27:37 +01:00

141 lines
2.8 KiB
Lua

local M = {}
local luasnip = require('luasnip')
local cmp = require('cmp')
local compare = require('cmp.config.compare')
local lspkind = require('lspkind')
-- Global config table
M.cmp = {
preselect = cmp.PreselectMode.None,
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
window = {
completion = {
scrollbar = false,
},
},
mapping = {
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { 'i', 's' }),
['<CR>'] = cmp.mapping {
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm { behavior = cmp.ConfirmBehavior.Replace, select = false }
else
fallback()
end
end,
s = cmp.mapping.confirm { select = false },
c = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Insert,
select = false,
},
},
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-d>'] = cmp.mapping(function(fallback)
if cmp.visible() then
if cmp.visible_docs() then
cmp.close_docs()
else
cmp.open_docs()
end
end
fallback()
end),
['<C-e>'] = cmp.mapping.abort(),
},
sources = cmp.config.sources {
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
{ name = 'lazydev', group_index = 0 },
{ name = 'path' },
{ name = 'buffer', keyword_length = 5 },
{ name = 'conjure' },
{ name = 'emoji', priority = -20 },
},
formatting = {
format = lspkind.cmp_format {
mode = 'symbol_text',
menu = {
buffer = '[BUF]',
nvim_lsp = '[LSP]',
path = '[PTH]',
luasnip = '[SNP]',
emoji = '[EMJ]',
},
maxwidth = 50,
ellipsis_char = '',
},
},
experimental = {
ghost_text = true,
},
sorting = {
priority_weight = 0.8,
comparators = {
compare.score,
compare.exact,
compare.offset,
compare.sort_text,
compare.scopes,
compare.recently_used,
compare.order,
compare.kind,
compare.length,
},
},
performance = {
fetching_timeout = 30,
},
}
--- Configure completion for commandline
M.cmd = function()
-- completion for searches
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' },
},
})
cmp.setup.cmdline({ '@' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'cmdline' },
},
})
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' },
}, {
{ name = 'cmdline' },
}),
})
end
return M