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 7569056388
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>
This commit is contained in:
@@ -686,6 +686,13 @@ view.docs.auto_open~
|
|||||||
|
|
||||||
Specify whether to show the docs_view when selecting an item.
|
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*
|
*cmp-config.window.{completion,documentation}.border*
|
||||||
window.{completion,documentation}.border~
|
window.{completion,documentation}.border~
|
||||||
`string | string[] | nil`
|
`string | string[] | nil`
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ return function()
|
|||||||
entries = {
|
entries = {
|
||||||
name = 'custom',
|
name = 'custom',
|
||||||
selection_order = 'top_down',
|
selection_order = 'top_down',
|
||||||
|
follow_cursor = false,
|
||||||
},
|
},
|
||||||
docs = {
|
docs = {
|
||||||
auto_open = true,
|
auto_open = true,
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ cmp.ItemField = {
|
|||||||
---@class cmp.CustomEntriesViewConfig
|
---@class cmp.CustomEntriesViewConfig
|
||||||
---@field name 'custom'
|
---@field name 'custom'
|
||||||
---@field selection_order 'top_down'|'near_cursor'
|
---@field selection_order 'top_down'|'near_cursor'
|
||||||
|
---@field follow_cursor boolean
|
||||||
|
|
||||||
---@class cmp.NativeEntriesViewConfig
|
---@class cmp.NativeEntriesViewConfig
|
||||||
---@field name 'native'
|
---@field name 'native'
|
||||||
|
|||||||
@@ -161,9 +161,12 @@ custom_entries_view.open = function(self, offset, entries)
|
|||||||
height = height ~= 0 and height or #self.entries
|
height = height ~= 0 and height or #self.entries
|
||||||
height = math.min(height, #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 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 row, col = pos[1], pos[2] - delta - 1
|
||||||
|
|
||||||
local border_info = window.get_border_info({ style = completion })
|
local border_info = window.get_border_info({ style = completion })
|
||||||
|
|||||||
Reference in New Issue
Block a user