From 7aa3f71932c419d716290e132cacbafbaf5bea1c Mon Sep 17 00:00:00 2001 From: mcauley-penney <59481467+mcauley-penney@users.noreply.github.com> Date: Sat, 30 Mar 2024 23:46:16 -0700 Subject: [PATCH] feat: add option for view to follow cursor (#1727) * feat: add option for custom entry view to follow cursor Creates an option to allow the custom entries view to follow the user's cursor as they type. To enable, set ```lua require("cmp").setup({ view = { entries = { follow_cursor = true } } }) ``` Original source at https://github.com/lvimuser/nvim-cmp/commit/7569056388417d887baf2e959e18a767bcfe84f1 Closes #1660 Co-authored-by: lvimuser <109605931+lvimuser@users.noreply.github.com> * doc: add view.follow_cursor option to docs --------- Co-authored-by: lvimuser <109605931+lvimuser@users.noreply.github.com> --- doc/cmp.txt | 7 +++++++ lua/cmp/config/default.lua | 1 + lua/cmp/types/cmp.lua | 1 + lua/cmp/view/custom_entries_view.lua | 7 +++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index e642f4c..23d6d54 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -686,6 +686,13 @@ view.docs.auto_open~ Specify whether to show the docs_view when selecting an item. + *cmp-config.view.follow_cursor* +view.follow_cursor* + `boolean` + + Specify whether the pmenu should follow the current position of the cursor + as the user types. `false` by default. + *cmp-config.window.{completion,documentation}.border* window.{completion,documentation}.border~ `string | string[] | nil` diff --git a/lua/cmp/config/default.lua b/lua/cmp/config/default.lua index 592ca28..65c0fda 100644 --- a/lua/cmp/config/default.lua +++ b/lua/cmp/config/default.lua @@ -97,6 +97,7 @@ return function() entries = { name = 'custom', selection_order = 'top_down', + follow_cursor = false, }, docs = { auto_open = true, diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 8040eee..e42dcc4 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -184,6 +184,7 @@ cmp.ItemField = { ---@class cmp.CustomEntriesViewConfig ---@field name 'custom' ---@field selection_order 'top_down'|'near_cursor' +---@field follow_cursor boolean ---@class cmp.NativeEntriesViewConfig ---@field name 'native' diff --git a/lua/cmp/view/custom_entries_view.lua b/lua/cmp/view/custom_entries_view.lua index 9d703c9..03c48d7 100644 --- a/lua/cmp/view/custom_entries_view.lua +++ b/lua/cmp/view/custom_entries_view.lua @@ -161,9 +161,12 @@ custom_entries_view.open = function(self, offset, entries) height = height ~= 0 and height or #self.entries height = math.min(height, #self.entries) + local delta = 0 + if not config.get().view.entries.follow_cursor then + local cursor_before_line = api.get_cursor_before_line() + delta = vim.fn.strdisplaywidth(cursor_before_line:sub(self.offset)) + end local pos = api.get_screen_cursor() - local cursor_before_line = api.get_cursor_before_line() - local delta = vim.fn.strdisplaywidth(cursor_before_line:sub(self.offset)) local row, col = pos[1], pos[2] - delta - 1 local border_info = window.get_border_info({ style = completion })