Skip to content

Commit 4b3fd8f

Browse files
committed
fix: window direction and autocomplete closing on position update
closes #240
1 parent ca94ee0 commit 4b3fd8f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lua/blink/cmp/windows/autocomplete.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ function autocomplete.open_with_items(context, items)
141141
vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)
142142

143143
if not autocomplete.auto_show then return end
144-
autocomplete.win:open()
145144

145+
autocomplete.win:open()
146146
autocomplete.update_position(context)
147+
148+
-- it's possible for the window to close after updating the position
149+
-- if there was nowhere to place the window
150+
if not autocomplete.win:is_open() then return end
151+
147152
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')
148153

149154
-- todo: some logic to maintain the selection if the user moved the cursor?
@@ -154,9 +159,11 @@ end
154159

155160
function autocomplete.open()
156161
if autocomplete.win:is_open() then return end
157-
vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)
162+
158163
autocomplete.win:open()
159164
autocomplete.set_has_selected(autocmp_config.selection == 'preselect')
165+
166+
vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)
160167
end
161168

162169
function autocomplete.close()

lua/blink/cmp/windows/lib/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function win:get_vertical_direction_and_height(direction_priority)
227227
local direction_priority_by_space = vim.fn.sort(vim.deepcopy(direction_priority), function(a, b)
228228
local distance_a = math.min(max_height, get_distance(a))
229229
local distance_b = math.min(max_height, get_distance(b))
230-
return distance_a < distance_b and -1 or distance_a > distance_b and 1 or 0
230+
return (distance_a < distance_b) and 1 or (distance_a > distance_b) and -1 or 0
231231
end)
232232

233233
local direction = direction_priority_by_space[1]
@@ -282,7 +282,7 @@ function win:get_direction_with_window_constraints(anchor_win, direction_priorit
282282
local constraints_b = direction_constraints[b]
283283
local distance_a = math.min(max_height, constraints_a.vertical, constraints_a.horizontal)
284284
local distance_b = math.min(max_height, constraints_b.vertical, constraints_b.horizontal)
285-
return distance_a < distance_b and -1 or distance_a > distance_b and 1 or 0
285+
return distance_a < distance_b and 1 or distance_a > distance_b and -1 or 0
286286
end)
287287

288288
local border_size = self:get_border_size()

0 commit comments

Comments
 (0)