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
- BREAKING CHANGE: tab behaviour has changed
This commit is contained in:
Fabrice Mouhartem 2024-10-18 18:17:34 +02:00
parent 0a73bd4259
commit 5bb1039198
Signed by: fmouhart
GPG Key ID: 2C5033B228CFE4E7

View File

@ -9,6 +9,12 @@ luasnip.config.setup {
enable_autosnippets = true, 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 { cmp.setup {
snippet = { snippet = {
expand = function(args) expand = function(args)
@ -32,9 +38,18 @@ cmp.setup {
['<C-e>'] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close() }), ['<C-e>'] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close() }),
['<Tab>'] = cmp.mapping(function(fallback) ['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_next_item() if #cmp.get_entries() == 1 then
cmp.confirm({select = true})
else
cmp.select_next_item()
end
elseif luasnip.expand_or_locally_jumpable() then elseif luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump() luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
if #cmp.get_entries() == 1 then
cmp.confirm({select = true})
end
else else
fallback() fallback()
end end