Skip to content

Commit a720117

Browse files
committed
fix: maintain autocomplete pos when scrolling/resizing
1 parent 6ee55d4 commit a720117

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lua/blink/cmp/windows/autocomplete.lua

+14-11
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ function autocomplete.setup()
4848
end,
4949
})
5050

51-
vim.api.nvim_create_autocmd('CursorMovedI', {
51+
vim.api.nvim_create_autocmd({ 'CursorMovedI', 'WinScrolled', 'WinResized' }, {
5252
callback = function()
5353
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)
57-
autocomplete.event_targets.on_position_update()
54+
autocomplete.update_position(autocomplete.context)
5855
end,
5956
})
6057

@@ -65,21 +62,18 @@ end
6562

6663
function autocomplete.open_with_items(context, items)
6764
autocomplete.items = items
68-
autocomplete.context = context
6965
autocomplete.draw()
7066

7167
autocomplete.win:open()
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)
74-
autocomplete.event_targets.on_position_update()
68+
69+
autocomplete.context = context
70+
autocomplete.update_position(context)
7571

7672
-- todo: some logic to maintain the selection if the user moved the cursor?
7773
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { 1, 0 })
7874
autocomplete.event_targets.on_select(autocomplete.get_selected_item())
7975
end
8076

81-
function autocomplete.listen_on_position_update(callback) autocomplete.event_targets.on_position_update = callback end
82-
8377
function autocomplete.open()
8478
if autocomplete.win:is_open() then return end
8579
autocomplete.win:open()
@@ -92,6 +86,15 @@ function autocomplete.close()
9286
end
9387
function autocomplete.listen_on_close(callback) autocomplete.event_targets.on_close = callback end
9488

89+
function autocomplete.update_position(context)
90+
-- todo: should point to the window of the context?
91+
local cursor_column = vim.api.nvim_win_get_cursor(0)[2]
92+
autocomplete.win:update_position('cursor', context.bounds.start_col - cursor_column - 1)
93+
autocomplete.event_targets.on_position_update()
94+
end
95+
96+
function autocomplete.listen_on_position_update(callback) autocomplete.event_targets.on_position_update = callback end
97+
9598
---------- Selection ----------
9699

97100
function autocomplete.select_next()

0 commit comments

Comments
 (0)