Skip to content

Commit 6ee55d4

Browse files
committed
feat: lock position to context start
1 parent 6b78c89 commit 6ee55d4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lua/blink/cmp/windows/autocomplete.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
local config = require('blink.cmp.config')
44
local autocomplete = {
55
items = {},
6+
context = nil,
67
event_targets = {
78
on_position_update = function() end,
89
on_select = function() end,
@@ -49,7 +50,10 @@ function autocomplete.setup()
4950

5051
vim.api.nvim_create_autocmd('CursorMovedI', {
5152
callback = function()
52-
autocomplete.win:update_position('cursor')
53+
if autocomplete.context == nil then return end
54+
55+
local cursor_column = vim.api.nvim_win_get_cursor(0)[2]
56+
autocomplete.win:update_position('cursor', autocomplete.context.bounds.start_col - cursor_column - 1)
5357
autocomplete.event_targets.on_position_update()
5458
end,
5559
})
@@ -59,12 +63,14 @@ end
5963

6064
---------- Visibility ----------
6165

62-
function autocomplete.open_with_items(items)
66+
function autocomplete.open_with_items(context, items)
6367
autocomplete.items = items
68+
autocomplete.context = context
6469
autocomplete.draw()
6570

6671
autocomplete.win:open()
67-
autocomplete.win:update_position('cursor')
72+
local cursor_column = vim.api.nvim_win_get_cursor(0)[2]
73+
autocomplete.win:update_position('cursor', autocomplete.context.bounds.start_col - cursor_column - 1)
6874
autocomplete.event_targets.on_position_update()
6975

7076
-- todo: some logic to maintain the selection if the user moved the cursor?

lua/blink/cmp/windows/lib.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ function win:close()
6868
end
6969
end
7070

71-
-- todo: dynamic width
72-
function win:update_position(relative_to)
71+
-- todo: move this code to the individual windows
72+
function win:update_position(relative_to, offset)
7373
if not self:is_open() then return end
7474
local winnr = self:get_win()
7575
local config = self.config
@@ -93,9 +93,9 @@ function win:update_position(relative_to)
9393
local is_space_below = screen_height - cursor_row > height
9494

9595
if is_space_below then
96-
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = 1, col = 0 })
96+
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = 1, col = offset })
9797
else
98-
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = -height, col = 0 })
98+
vim.api.nvim_win_set_config(winnr, { relative = 'cursor', row = -height, col = offset })
9999
end
100100

101101
-- relative to window

0 commit comments

Comments
 (0)