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