From 6252962dbff6805a10b2209b901829b2fdeccb2f Mon Sep 17 00:00:00 2001 From: Fabrice Mouhartem Date: Fri, 18 Oct 2024 18:17:34 +0200 Subject: [PATCH] feat(nvim-cmp): Add tab complete when there is no ambiguity - from --- lua/complete.lua | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lua/complete.lua b/lua/complete.lua index 9f4b608..d75d192 100644 --- a/lua/complete.lua +++ b/lua/complete.lua @@ -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 { [''] = cmp.mapping({ i = cmp.mapping.abort(), c = cmp.mapping.close() }), [''] = cmp.mapping(function(fallback) 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 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