vim: improve completion, add cmdline completion on demand

This commit is contained in:
Fernando Schauenburg 2023-07-12 00:25:54 +02:00
parent 51cb1426ac
commit 353827431f

View file

@ -1,77 +1,107 @@
local config = function() local config = function()
local cmp = require('cmp') local cmp = require('cmp')
local mapping = { local map = setmetatable({
['<c-n>'] = { complete_or_select_next = function()
i = function() if cmp.visible() then
if cmp.visible() then cmp.select_next_item { behavior = cmp.SelectBehavior.Select }
cmp.select_next_item { behavior = cmp.SelectBehavior.Select } else
else cmp.complete()
cmp.complete() end
end end,
end,
},
['<c-p>'] = { complete_or_select_previous = function()
i = function() if cmp.visible() then
if cmp.visible() then cmp.select_prev_item { behavior = cmp.SelectBehavior.Select }
cmp.select_prev_item { behavior = cmp.SelectBehavior.Select } else
else cmp.complete()
cmp.complete() end
end end,
end,
},
['<Down>'] = { i = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select } }, tab_completion = function(fallback)
['<Up>'] = { i = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select } }, if cmp.visible() then
cmp.complete_common_string()
elseif vim.fn.mode() == 'c' then
cmp.complete()
else
fallback()
end
end,
}, {
-- Make `func` available in [i]nsert and [c]mdline modes.
__call = function(_, func) return { i = func, c = func } end
})
['<c-f>'] = { i = cmp.mapping.scroll_docs(4) }, local keymap = {
['<c-b>'] = { i = cmp.mapping.scroll_docs(-4) }, ['<c-n>'] = map(map.complete_or_select_next),
['<c-p>'] = map(map.complete_or_select_previous),
['<c-e>'] = { i = cmp.mapping.abort() }, ['<down>'] = map(cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }),
['<c-y>'] = { i = cmp.mapping.confirm { select = true } }, ['<up>'] = map(cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }),
['<Tab>'] = {
i = function(fallback) ['<c-f>'] = map(cmp.mapping.scroll_docs(4)),
if cmp.visible() then ['<s-down>'] = map(cmp.mapping.scroll_docs(4)),
cmp.confirm { select = true } ['<c-b>'] = map(cmp.mapping.scroll_docs(-4)),
else ['<s-up>'] = map(cmp.mapping.scroll_docs(-4)),
fallback()
end ['<c-e>'] = map(cmp.mapping.abort()),
end, ['<c-y>'] = map(cmp.mapping.confirm { select = true }),
}, ['<tab>'] = map(map.tab_completion),
} }
cmp.setup { cmp.setup {
mapping = mapping, mapping = keymap,
snippet = {
expand = function(_ --[[args]])
-- require('luasnip').lsp_expand(args.body) TODO enable
end,
},
formatting = {
format = require('lspkind').cmp_format {
mode = 'symbol_text',
menu = {
buffer = "[buf]",
nvim_lsp = "[LSP]",
nvim_lua = "[lua]",
path = "[path]",
},
},
},
sources = cmp.config.sources({ sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'nvim_lua' }, { name = 'nvim_lua' },
{ name = 'nvim_lsp' },
-- { name = "luasnip" }, TODO enable
}, { }, {
{ name = 'path' }, { name = 'path' },
{ name = 'buffer', keyword_length = 5 }, { name = 'buffer', keyword_length = 5 },
}), }),
formatting = (function() window = {
local ok, lspkind = pcall(require, 'lspkind') completion = cmp.config.window.bordered(),
if not ok then return {} end documentation = cmp.config.window.bordered(),
return { },
format = lspkind.cmp_format {
mode = 'symbol_text',
menu = {
buffer = "[buf]",
nvim_lsp = "[LSP]",
nvim_lua = "[lua]",
path = "[path]",
},
},
}
end)(),
experimental = { experimental = {
ghost_text = true, ghost_text = true,
}, },
} }
cmp.setup.cmdline(':', {
mapping = keymap,
completion = {
autocomplete = false,
},
sources = cmp.config.sources({
{ name = 'path' }
}, {
{ name = 'cmdline' }
}),
})
end end
return { return {
@ -83,6 +113,7 @@ return {
'hrsh7th/cmp-nvim-lua', 'hrsh7th/cmp-nvim-lua',
'hrsh7th/cmp-path', 'hrsh7th/cmp-path',
'hrsh7th/cmp-buffer', 'hrsh7th/cmp-buffer',
'hrsh7th/cmp-cmdline',
'onsails/lspkind-nvim', 'onsails/lspkind-nvim',
}, },
} }