fix autocompletion

add keybinds for scrolling docs
can now cycle up/down completions with tab/shift-tab
C-f / C-b jump forward/back in function args
completions for search in wildmenu
completion for cmd works now
This commit is contained in:
2024-11-25 12:19:39 -05:00
parent 351b7b0ce8
commit 1cd5c8fb0d
+116 -10
View File
@@ -5,6 +5,7 @@ return {
'hrsh7th/cmp-buffer', 'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path', 'hrsh7th/cmp-path',
'hrsh7th/cmp-cmdline', 'hrsh7th/cmp-cmdline',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-nvim-lsp', 'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-nvim-lsp-document-symbol', 'hrsh7th/cmp-nvim-lsp-document-symbol',
'hrsh7th/cmp-nvim-lsp-signature-help', 'hrsh7th/cmp-nvim-lsp-signature-help',
@@ -20,41 +21,106 @@ return {
config = function() config = function()
local cmp = require('cmp') local cmp = require('cmp')
local luasnip = require('luasnip') local snippet = require('luasnip')
local lspkind = require('lspkind') local lspkind = require('lspkind')
local select_opts = { behavior = cmp.SelectBehavior.Select }
require('luasnip.loaders.from_vscode').lazy_load() require('luasnip.loaders.from_vscode').lazy_load()
cmp.setup({ cmp.setup({
enabled = function()
-- disable completion in comments
local context = require('cmp.config.context')
if vim.api.nvim_get_mode().mode == 'c' then
return true
else
return not context.in_treesitter_capture('comment') and not context.in_syntax_group('Comment')
end
end,
view = { view = {
entries = 'custom', entries = 'custom',
selection_order = 'near_cursor', selection_order = 'near_cursor',
}, },
window = {
documentation = cmp.config.window.bordered(),
},
completion = { completion = {
completeopt = 'menu,menuone,preview,noselect', completeopt = 'menu,menuone,preview,noselect',
}, },
snippet = { snippet = {
expand = function(args) expand = function(args)
luasnip.lsp_expand(args.body) snippet.lsp_expand(args.body)
end, end,
}, },
mapping = cmp.mapping.preset.insert({ mapping = cmp.mapping.preset.insert({
['<C-k>'] = cmp.mapping.select_prev_item(), ['<C-k>'] = cmp.mapping.select_prev_item(),
['<C-j>'] = cmp.mapping.select_next_item(), ['<C-j>'] = cmp.mapping.select_next_item(),
['<C-b>'] = cmp.mapping.scroll_docs(-4), ['<C-u>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4), ['<C-d>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(), -- ['<C-Space>'] = cmp.mapping.complete(),
-- ┌
-- │ if completion menu is visible, move prev/next with
-- │ tab/s-tab
-- │ if we're not attached to anything, then insert a tab
-- │ if we're inside a word, trigger the completion menu
-- └
['<Tab>'] = cmp.mapping(function(fallback)
local col = vim.fn.col('.') - 1
if cmp.visible() then
cmp.select_next_item(select_opts)
elseif col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
fallback()
else
cmp.complete()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item(select_opts)
else
fallback()
end
end, { 'i', 's' }),
['<C-e>'] = cmp.mapping.abort(), ['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = false }), ['<CR>'] = cmp.mapping.confirm({ select = false }),
['<C-f>'] = cmp.mapping(function(fallback)
if snippet.jumpable(1) then
snippet.jump(1)
else
fallback()
end
end, { 'i', 's' }),
['<C-b>'] = cmp.mapping(function(fallback)
if snippet.jumpable(-1) then
snippet.jump(-1)
else
fallback()
end
end, { 'i', 's' }),
}), }),
sources = cmp.config.sources({ sources = cmp.config.sources({
{ name = 'nvim_lsp' }, { name = 'nvim_lsp', priority = 1 },
{ name = 'luasnip' }, { name = 'luasnip', priority = 2 },
{ name = 'buffer' }, { name = 'buffer', keyword_length = 3 },
{ name = 'path' }, { name = 'path' },
}), }),
formatting = { formatting = {
format = lspkind.cmp_format({ -- fields = { 'abbr', 'kind', 'menu' },
expandable_indicator = true,
format = function(entry, vim_item)
if vim.tbl_contains({ 'path' }, entry.source.name) then
local icon, hl_group = require('nvim-web-devicons').get_icon(entry.completion_item().label)
if icon then
vim_item.kind = icon
vim_item.kind_hl_group = hl_group
return vim_item
end
end
return lspkind.cmp_format({
with_text = true,
mode = 'symbol_text', mode = 'symbol_text',
maxwidth = 50, maxwidth = 50,
ellipsis_char = '...', ellipsis_char = '...',
@@ -66,8 +132,10 @@ return {
luasnip = '[LuaSnip]', luasnip = '[LuaSnip]',
buffer = '[Buffer]', buffer = '[Buffer]',
path = '[Path]', path = '[Path]',
latex_symbols = '[Latex]',
}, },
}), })(entry, vim_item)
end,
}, },
-- VSCode-like, kind specific highlights -- VSCode-like, kind specific highlights
vim.api.nvim_set_hl(0, 'CmpItemAbbrDeprecated', { bg = 'NONE', strikethrough = true, fg = '#4c566a' }), vim.api.nvim_set_hl(0, 'CmpItemAbbrDeprecated', { bg = 'NONE', strikethrough = true, fg = '#4c566a' }),
@@ -82,5 +150,43 @@ return {
vim.api.nvim_set_hl(0, 'CmpItemKindProperty', { link = 'CmpItemKindKeyword' }), vim.api.nvim_set_hl(0, 'CmpItemKindProperty', { link = 'CmpItemKindKeyword' }),
vim.api.nvim_set_hl(0, 'CmpItemKindUnit', { link = 'CmpItemKindKeyword' }), vim.api.nvim_set_hl(0, 'CmpItemKindUnit', { link = 'CmpItemKindKeyword' }),
}) })
cmp.setup.cmdline({ '/', '?' }, {
mapping = cmp.mapping.preset.cmdline(),
view = {
entries = {
name = 'wildmenu',
separator = '|',
},
},
sources = {
{ name = 'buffer' },
},
})
cmp.setup.cmdline(':', {
enabled = function()
local disabled = {
-- list of commands to disable completions
wq = true,
wqa = true,
}
local cmd = vim.fn.getcmdline():match('%S+')
return not disabled[cmd] or cmp.close()
end,
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = 'path' },
{ name = 'cmdline' },
}),
matching = {
disallow_fullfuzzy_matching = false,
disallow_fuzzy_matching = false,
disallow_partial_fuzzy_matching = false,
disallow_partial_matching = false,
disallow_prefix_unmatching = false,
disallow_symbol_nonprefix_matching = false,
},
})
end, end,
} }