feat(nvim-cmp): Add tab complete when there is no ambiguity

- from <https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#confirm-candidate-on-tab-immediately-when-theres-only-one-completion-entry>
This commit is contained in:
Fabrice Mouhartem 2024-10-18 18:17:34 +02:00
parent 0a73bd4259
commit 6252962dbf
Signed by: fmouhart
GPG Key ID: 2C5033B228CFE4E7

View File

@ -9,6 +9,12 @@ luasnip.config.setup {
enable_autosnippets = true,
}
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
cmp.setup {
snippet = {
expand = function(args)
@ -32,9 +38,18 @@ cmp.setup {
['<C-e>'] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close() }),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
if #cmp.get_entries() == 1 then
cmp.confirm({select = true})
else
cmp.select_next_item()
end
elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
if #cmp.get_entries() == 1 then
cmp.confirm({select = true})
end
else
fallback()
end