nvim/lua/plugins/cmp.lua
2025-04-09 22:22:12 +02:00

148 lines
4.6 KiB
Lua

return {
'hrsh7th/nvim-cmp',
branch = 'main',
lazy = false,
dependencies = {
'neovim/nvim-lspconfig',
'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path',
'hrsh7th/cmp-cmdline',
'L3MON4D3/LuaSnip',
'saadparwaiz1/cmp_luasnip',
'onsails/lspkind.nvim',
'zbirenbaum/copilot-cmp',
'hrsh7th/cmp-path',
'dmitmel/cmp-cmdline-history',
'hrsh7th/cmp-nvim-lsp-signature-help',
},
config = function()
local t = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
local cmp = require('cmp')
local lspkind = require('lspkind')
local luasnip = require('luasnip')
lspkind.init({
symbol_map = {
Copilot = '',
},
})
require('copilot_cmp').setup()
cmp.setup({
preselect = cmp.PreselectMode.None,
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end,
},
mapping = {
['<Up>'] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 'c', 's' }),
['<Down>'] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 'c', 's' }),
['<C-p>'] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 'c', 's' }),
['<C-n>'] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 'c', 's' }),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c', 's' }),
['<C-e>'] = cmp.mapping(cmp.mapping.close(), { 'i', 'c', 's' }),
['<CR>'] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = false })
else
fallback()
end
end,
c = cmp.mapping.confirm({ select = false }),
s = cmp.mapping.confirm({ select = false }),
}),
['<Tab>'] = cmp.mapping(function(fallback)
if luasnip.expand_or_jumpable() then
vim.api.nvim_feedkeys(t('<Plug>luasnip-expand-or-jump'), '', true)
else
fallback()
end
end, {
'i',
's',
}),
['<S-Tab>'] = cmp.mapping(function(fallback)
if luasnip.jumpable(-1) then
vim.api.nvim_feedkeys(t('<Plug>luasnip-jump-prev'), '', true)
else
fallback()
end
end, { 'i', 's' }),
['<c-x>'] = cmp.mapping(
cmp.mapping.complete({
config = {
sources = {
{
name = 'cmdline_history',
},
},
},
}),
{ 'c' }
),
},
sources = cmp.config.sources({
{ name = 'copilot' },
{ name = 'nvim_lsp' },
{ name = 'nvim_lsp_signature_help' },
{ name = 'luasnip' },
{ name = 'buffer' },
{ name = 'path' },
}),
window = {
documentation = {
winhighlight = 'Normal:CmpDocumentation',
},
completion = {
winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None',
col_offset = -3,
side_padding = 0,
},
},
formatting = {
fields = { 'kind', 'abbr', 'menu' },
expandable_indicator = false,
format = function(entry, vim_item)
local kind = require('lspkind').cmp_format({ mode = 'symbol_text', maxwidth = 50, show_labelDetails = true })(entry, vim_item)
local strings = vim.split(kind.kind, '%s', { trimempty = true })
kind.kind = ' ' .. (strings[1] or '') .. ' '
kind.menu = ' (' .. (strings[2] or '') .. ')'
return kind
end,
},
})
-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'buffer' },
{ name = 'cmdline_history' },
{ name = 'path' },
{ name = 'buffer' },
},
})
-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = 'cmdline', priority = 200 },
{ name = 'path', priority = 3 },
{ name = 'cmdline_history', priority = 3 },
{ name = 'buffer', priority = 3 },
},
matching = { disallow_symbol_nonprefix_matching = false },
})
end,
}