From 27ffaab737ae27a10a05eb11473f51ac35b21701 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Sun, 24 Oct 2021 14:22:39 +0200 Subject: feat: make cmp keyword_length easier to configure (#1840) --- lua/lvim/core/cmp.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lua/lvim/core/cmp.lua') diff --git a/lua/lvim/core/cmp.lua b/lua/lvim/core/cmp.lua index b1cb1431..f35ead9a 100644 --- a/lua/lvim/core/cmp.lua +++ b/lua/lvim/core/cmp.lua @@ -126,6 +126,12 @@ M.config = function() behavior = cmp.ConfirmBehavior.Replace, select = false, }, + completion = { + ---@usage vim's `completeopt` setting. Warning: Be careful when changing this value. + completeopt = "menu,menuone,noinsert", + ---@usage The minimum length of a word to complete on. + keyword_length = 1, + }, experimental = { ghost_text = true, native_menu = false, @@ -241,7 +247,7 @@ M.config = function() }), [""] = cmp.mapping.complete(), - [""] = cmp.mapping.close(), + [""] = cmp.mapping.abort(), [""] = cmp.mapping(function(fallback) if cmp.visible() and cmp.confirm(lvim.builtin.cmp.confirm_opts) then return -- cgit v1.2.3 From 177916d63d7e1c748c48436ef93b30f0019eacac Mon Sep 17 00:00:00 2001 From: Chase Colman <5411+chase@users.noreply.github.com> Date: Tue, 26 Oct 2021 00:54:27 +0800 Subject: feat(cmp): expose lunarvim's cmp helper methods (#1844) Co-authored-by: xeluxee <88047141+xeluxee@users.noreply.github.com> --- lua/lvim/core/cmp.lua | 60 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 13 deletions(-) (limited to 'lua/lvim/core/cmp.lua') diff --git a/lua/lvim/core/cmp.lua b/lua/lvim/core/cmp.lua index f35ead9a..57fb70c4 100644 --- a/lua/lvim/core/cmp.lua +++ b/lua/lvim/core/cmp.lua @@ -1,14 +1,29 @@ local M = {} +M.methods = {} +---checks if the character preceding the cursor is a space character +---@return boolean true if it is a space character, false otherwise local check_backspace = function() local col = vim.fn.col "." - 1 return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" end +M.methods.check_backspace = check_backspace local function T(str) return vim.api.nvim_replace_termcodes(str, true, true, true) end +---wraps vim.fn.feedkeys while replacing key codes with escape codes +---Ex: feedkeys("", "n") becomes feedkeys("^M", "n") +---@param key string +---@param mode string +local function feedkeys(key, mode) + vim.fn.feedkeys(T(key), mode) +end +M.methods.feedkeys = feedkeys + +---checks if emmet_ls is available and active in the buffer +---@return boolean true if available, false otherwise local is_emmet_active = function() local clients = vim.lsp.buf_get_clients() @@ -19,16 +34,17 @@ local is_emmet_active = function() end return false end +M.methods.is_emmet_active = is_emmet_active -M.config = function() - local status_cmp_ok, cmp = pcall(require, "cmp") - if not status_cmp_ok then - return - end - local status_luasnip_ok, luasnip = pcall(require, "luasnip") - if not status_luasnip_ok then +---when inside a snippet, seeks to the nearest luasnip field if possible, and checks if it is jumpable +---@param dir number 1 for forward, -1 for backward; defaults to 1 +---@return boolean true if a jumpable luasnip field is found while inside a snippet +local function jumpable(dir) + local luasnip_ok, luasnip = pcall(require, "luasnip") + if not luasnip_ok then return end + local win_get_cursor = vim.api.nvim_win_get_cursor local get_current_buf = vim.api.nvim_get_current_buf @@ -121,6 +137,24 @@ M.config = function() return false end + if dir == -1 then + return inside_snippet() and luasnip.jumpable(-1) + else + return inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() + end +end +M.methods.jumpable = jumpable + +M.config = function() + local status_cmp_ok, cmp = pcall(require, "cmp") + if not status_cmp_ok then + return + end + local status_luasnip_ok, luasnip = pcall(require, "luasnip") + if not status_luasnip_ok then + return + end + lvim.builtin.cmp = { confirm_opts = { behavior = cmp.ConfirmBehavior.Replace, @@ -215,19 +249,19 @@ M.config = function() [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), -- TODO: potentially fix emmet nonsense - [""] = cmp.mapping(function() + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif luasnip.expandable() then luasnip.expand() - elseif inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() then + elseif jumpable() then luasnip.jump(1) elseif check_backspace() then - vim.fn.feedkeys(T "", "n") + fallback() elseif is_emmet_active() then return vim.fn["cmp#complete"]() else - vim.fn.feedkeys(T "", "n") + fallback() end end, { "i", @@ -236,7 +270,7 @@ M.config = function() [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() - elseif inside_snippet() and luasnip.jumpable(-1) then + elseif jumpable(-1) then luasnip.jump(-1) else fallback() @@ -253,7 +287,7 @@ M.config = function() return end - if inside_snippet() and seek_luasnip_cursor_node() and luasnip.jumpable() then + if jumpable() then if not luasnip.jump(1) then fallback() end -- cgit v1.2.3 From 377cab434c79aa537820ba21bbcc44261304ed09 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Fri, 29 Oct 2021 19:33:00 +0200 Subject: feat: update now syncs the core plugins (#1865) --- lua/lvim/core/cmp.lua | 2 -- 1 file changed, 2 deletions(-) (limited to 'lua/lvim/core/cmp.lua') diff --git a/lua/lvim/core/cmp.lua b/lua/lvim/core/cmp.lua index 57fb70c4..68c695cb 100644 --- a/lua/lvim/core/cmp.lua +++ b/lua/lvim/core/cmp.lua @@ -161,8 +161,6 @@ M.config = function() select = false, }, completion = { - ---@usage vim's `completeopt` setting. Warning: Be careful when changing this value. - completeopt = "menu,menuone,noinsert", ---@usage The minimum length of a word to complete on. keyword_length = 1, }, -- cgit v1.2.3