nvim/lua/plugins/cmp.lua
2024-06-26 23:15:42 +02:00

209 lines
6.6 KiB
Lua

return {
'hrsh7th/nvim-cmp',
branch = 'main',
event = { 'InsertEnter', 'CmdlineEnter' },
dependencies = {
{ 'onsails/lspkind-nvim' },
{ 'hrsh7th/cmp-buffer' },
{ 'hrsh7th/cmp-nvim-lsp' },
{ 'L3MON4D3/LuaSnip' },
{ 'saadparwaiz1/cmp_luasnip' },
{ 'hrsh7th/cmp-nvim-lua' },
{ 'octaltree/cmp-look' },
{ 'hrsh7th/cmp-path' },
{ 'hrsh7th/cmp-calc' },
{ 'f3fora/cmp-spell' },
{ 'hrsh7th/cmp-emoji' },
{ 'hrsh7th/cmp-cmdline' },
{ 'dmitmel/cmp-cmdline-history' },
{ 'ray-x/cmp-treesitter' },
{ 'hrsh7th/cmp-nvim-lsp-signature-help' },
{ 'chrisgrieser/cmp_yanky' },
{ 'p00f/clangd_extensions.nvim' },
{
'windwp/nvim-autopairs',
config = true,
},
},
config = function()
local cmp = require('cmp')
local luasnip = require('luasnip')
local t = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
cmp.setup({
preselect = cmp.PreselectMode.None,
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' }
),
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
sources = {
{ name = 'luasnip', priority = 8 },
{
name = 'nvim_lsp',
option = {
markdown_oxide = {
keyword_pattern = [[\(\k\| \|\/\|#\)\+]],
},
},
priority = 7,
},
{ name = 'nvim_lsp_signature_help', priority = 7 },
-- { name = 'treesitter', priority = 6 },
{
name = 'cmp_yanky',
option = {
-- only suggest items which match the current filetype
onlyCurrentFiletype = false,
-- only suggest items with a minimum length
minLength = 3,
},
},
{
name = 'buffer',
priority = 3,
option = {
get_bufnrs = function()
local bufs = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
bufs[vim.api.nvim_win_get_buf(win)] = true
end
return vim.tbl_keys(bufs)
end,
},
},
-- { name = 'nvim_lua' },
-- { name = 'look' },
{ name = 'path' },
-- { name = 'cmp_tabnine' },
-- { name = 'calc' },
-- { name = 'spell' },
-- { name = 'emoji' },
},
enabled = function()
return vim.api.nvim_get_option_value('buftype', { buf = 0 }) ~= 'prompt'
or vim.api.nvim_get_option_value('filetype', { buf = 0 }) == 'dap-repl'
or vim.api.nvim_get_option_value('filetype', { buf = 0 }) == 'dapui_watches'
or vim.api.nvim_get_option_value('filetype', { buf = 0 }) == 'dapui_hover'
end,
-- completion = {
-- completeopt = 'menu,menuone,noinsert, noselect',
-- border = 'rounded',
-- },
window = {
completion = cmp.config.window.bordered({
col_offset = -3,
side_padding = 0,
winhighlight = 'Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None',
}),
documentation = cmp.config.window.bordered({
winhighlight = 'Normal:Normal,FloatBorder:FloatBorder,CursorLine:Visual,Search:None',
}),
},
formatting = {
fields = { 'kind', 'abbr', 'menu' },
format = function(entry, vim_item)
local kind = require('lspkind').cmp_format({
mode = 'symbol_text',
maxwidth = 50,
})(entry, vim_item)
local strings = vim.split(kind.kind, '%s', { trimempty = true })
kind.kind = ' ' .. strings[1] .. ' '
-- kind.menu = ' (' .. strings[2] .. ')'
return kind
end,
},
-- experimental = { native_menu = true }
})
-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ 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 = cmp.config.sources({
{ name = 'cmdline' },
{ name = 'cmdline_history' },
{ name = 'path' },
{ name = 'buffer' },
}),
matching = { disallow_symbol_nonprefix_matching = false },
})
require('cmp').setup.filetype({ 'dap-repl', 'dapui_watches', 'dapui_hover' }, {
sources = {
{ name = 'dap' },
},
})
-- Autopairs
--require("nvim-autopairs.completion.cmp").setup({
-- map_cr = true,
-- map_complete = true,
-- auto_select = true
--})
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done({ map_char = { tex = '' } }))
end,
}