Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option for view to follow cursor #1727

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/cmp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,13 @@ view.docs.auto_open~

Specify whether to show the docs_view when selecting an item.

*cmp-config.view.follow_cursor*
view.follow_cursor*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, reading the rest of the final change it seems the help tags here are wrong, they should use view.entries.follow_cursor since the option is under entries 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, thanks! I'll submit a fix today

`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`
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ return function()
entries = {
name = 'custom',
selection_order = 'top_down',
follow_cursor = false,
},
docs = {
auto_open = true,
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/types/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,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'
Expand Down
7 changes: 5 additions & 2 deletions lua/cmp/view/custom_entries_view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,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 })
Expand Down
Loading