From 5bb1039198f06319e7a1086ea233e8530fd57a40 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 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 --- 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