neovim/lua/code-completion.lua

68 lines
2.9 KiB
Lua

-- completeopt is used to manage code suggestions
-- menuone: show popup even when there is only one suggestion
-- noinsert: Only insert text when selection is confirmed
-- noselect: force us to select one from the suggestions
vim.opt.completeopt = { 'menuone', 'noselect', 'noinsert', 'preview' }
-- shortmess is used to avoid excessive messages
vim.opt.shortmess = vim.opt.shortmess + { c = true }
local cmp = require('cmp')
local lspkind = require('lspkind')
cmp.setup({
enabled = true,
preselect = cmp.PreselectMode.None,
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end,
},
formatting = {
format = lspkind.cmp_format({
mode = 'symbol', -- show only symbol annotations
maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
-- can also be a function to dynamically calculate max width such as
-- maxwidth = function() return math.floor(0.45 * vim.o.columns) end,
ellipsis_char = '...', -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first)
show_labelDetails = true, -- show labelDetails in menu. Disabled by default
-- The function below will be called before any actual modifications from lspkind
-- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
before = function(entry, vim_item)
return vim_item
end
})
},
mapping = {
-- Shift+TAB to go to the Previous Suggested item
['<Up>'] = cmp.mapping.select_prev_item(),
['<S-Tab>'] = cmp.mapping.select_prev_item(),
-- Tab to go to the next suggestion
['<Down>'] = cmp.mapping.select_next_item(),
['<Tab>'] = cmp.mapping.select_next_item(),
-- CTRL+SHIFT+f to scroll backwards in description
['<C-S-f>'] = cmp.mapping.scroll_docs(-4),
-- CTRL+F to scroll forwards in the description
['<C-f>'] = cmp.mapping.scroll_docs(4),
-- CTRL+SPACE to bring up completion at current Cursor location
['<C-Space>'] = cmp.mapping.complete(),
-- CTRL+e to exit suggestion and close it
['<C-e>'] = cmp.mapping.close(),
-- CR (enter or return) to CONFIRM the currently selection suggestion
-- We set the ConfirmBehavior to insert the Selected suggestion
['<CR>'] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Insert,
select = false, -- This prevents the selection of a suggestion by default
})
},
-- sources are the installed sources that can be used for code suggestions
sources = {
{ name = 'path' },
{ name = 'nvim_lsp', keyword_length = 3 },
{ name = 'nvim_lsp_signature_help' },
{ name = 'nvim_lua', keyword_length = 2 },
{ name = 'buffer', keyword_length = 2 },
{ name = 'vsnip', keyword_length = 2 },
}
})