From d9a0c946cd764c5705f3f22cb70272033e6d8f7f Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sun, 23 Jan 2022 14:16:44 +0100 Subject: [PATCH] update cmp config with jump to completion fields --- .config/nvim/lua/pluginconf/nvim_cmp.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/.config/nvim/lua/pluginconf/nvim_cmp.lua b/.config/nvim/lua/pluginconf/nvim_cmp.lua index 477b339..f1c2399 100644 --- a/.config/nvim/lua/pluginconf/nvim_cmp.lua +++ b/.config/nvim/lua/pluginconf/nvim_cmp.lua @@ -1,4 +1,10 @@ local cmp = require "cmp" +local luasnip = require "luasnip" + +local function has_words_before() + 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 = { @@ -11,7 +17,17 @@ cmp.setup { [""] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), - [""] = cmp.mapping(cmp.mapping.select_next_item()), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), [""] = cmp.mapping(cmp.mapping.select_prev_item()), [""] = cmp.mapping.confirm { select = true }, },