nvim/lua/custom/plugins/cmp.lua
Jeremie Fraeys bfb1a63776
refactor: overhaul LSP and completion setup
- Major refactor of lsp-config.lua with simplified configuration
- Streamline cmp.lua for better performance
- Remove unused LSP configurations
2026-03-23 20:32:51 -04:00

132 lines
3.8 KiB
Lua
Executable file

return {
'hrsh7th/nvim-cmp',
event = { 'InsertEnter', 'CmdlineEnter' },
dependencies = {
'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path',
'hrsh7th/cmp-cmdline',
'petertriho/cmp-git',
'onsails/lspkind-nvim',
'L3MON4D3/LuaSnip',
'saadparwaiz1/cmp_luasnip',
},
config = function()
local cmp = require('cmp')
local lspkind = require('lspkind')
-- NO luasnip require here
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- lazy, only on snippet expand
end,
},
mapping = cmp.mapping.preset.insert({
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<CR>'] = cmp.mapping.confirm({ select = false }),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
local luasnip = require('luasnip') -- lazy, only on keypress
if luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
local luasnip = require('luasnip') -- lazy, only on keypress
if luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end
end, { 'i', 's' }),
}),
sources = {
{ name = 'nvim_lsp', priority = 1000 },
{ name = 'luasnip', priority = 900 },
{ name = 'path', priority = 750 },
{
name = 'buffer',
priority = 500,
keyword_length = 5,
max_item_count = 50,
option = {
get_bufnrs = function()
return { vim.api.nvim_get_current_buf() } -- only index current buffer, not all open buffers
end,
},
},
},
formatting = {
fields = { 'abbr', 'kind', 'menu' },
expandable_indicator = false,
format = lspkind.cmp_format({
mode = 'symbol_text',
maxwidth = 50,
ellipsis_char = '...',
menu = {
nvim_lsp = '[LSP]',
luasnip = '[Snip]',
buffer = '[Buffer]',
path = '[Path]',
},
}),
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
performance = {
confirm_resolve_timeout = 100,
max_view_entries = 20,
debounce = 60,
throttle = 30,
fetching_timeout = 200,
},
})
cmp.setup.filetype('gitcommit', { -- only once
sources = {
{ name = 'git', priority = 900 },
{ name = 'buffer', keyword_length = 5 },
},
})
local cmdline_mapping = cmp.mapping.preset.cmdline({
['<C-n>'] = { c = cmp.mapping.select_next_item() },
['<C-p>'] = { c = cmp.mapping.select_prev_item() },
['<Tab>'] = { c = cmp.mapping.select_next_item() },
['<S-Tab>'] = { c = cmp.mapping.select_prev_item() },
})
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmdline_mapping,
sources = { { name = 'buffer', keyword_length = 5 } },
})
cmp.setup.cmdline(':', {
mapping = cmdline_mapping,
sources = {
{ name = 'path' },
{ name = 'cmdline' },
},
window = {
documentation = cmp.config.window.bordered({
winhighlight = 'Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None',
border = 'rounded',
}),
},
})
end,
}