Skip to content

Commit e1e1a60

Browse files
committed
feat(menu): prefer north direction if ghost_text is multiline
1 parent 4a380c1 commit e1e1a60

File tree

1 file changed

+19
-2
lines changed
  • lua/blink/cmp/completion/windows

1 file changed

+19
-2
lines changed

lua/blink/cmp/completion/windows/menu.lua

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
--- @field renderer blink.cmp.Renderer
55
--- @field selected_item_idx? number
66
--- @field context blink.cmp.Context?
7+
--- @field north boolean
78
--- @field open_emitter blink.cmp.EventEmitter<{}>
89
--- @field close_emitter blink.cmp.EventEmitter<{}>
910
--- @field position_update_emitter blink.cmp.EventEmitter<{}>
@@ -49,6 +50,7 @@ vim.api.nvim_create_autocmd({ 'CursorMovedI', 'WinScrolled', 'WinResized' }, {
4950
function menu.open_with_items(context, items)
5051
menu.context = context
5152
menu.items = items
53+
menu.north = false
5254
menu.selected_item_idx = menu.selected_item_idx ~= nil and math.min(menu.selected_item_idx, #items) or nil
5355

5456
if not menu.renderer then menu.renderer = require('blink.cmp.completion.windows.render').new(config.draw) end
@@ -76,7 +78,7 @@ end
7678
function menu.close()
7779
menu.auto_show = config.auto_show
7880
if not menu.win:is_open() then return end
79-
81+
menu.selected_item_idx = nil
8082
menu.win:close()
8183
menu.close_emitter:emit()
8284
end
@@ -97,15 +99,30 @@ function menu.update_position()
9799

98100
win:update_size()
99101

102+
local item = menu.items[menu.selected_item_idx]
103+
local prefer_north = false
104+
-- If there are multiline text and user turns on ghost text, we should prefer opening upwards.
105+
-- Once we go up, we don't go down again even if the text is only one line, to reduce visual interference
106+
-- when keeps holding down <c-p>/<c-n>.
107+
if
108+
require('blink.cmp.config').completion.ghost_text.enabled and menu.north
109+
or (item ~= nil and item.insertText ~= nil and string.find(item.insertText, '\n') ~= nil)
110+
then
111+
prefer_north = true
112+
end
113+
100114
local border_size = win:get_border_size()
101-
local pos = win:get_vertical_direction_and_height(config.direction_priority)
115+
local pos = win:get_vertical_direction_and_height(prefer_north and { 'n', 's' } or config.direction_priority)
102116

103117
-- couldn't find anywhere to place the window
104118
if not pos then
105119
win:close()
106120
return
107121
end
108122

123+
-- record it, reset to false at next time when item changes
124+
if prefer_north and pos.direction == 'n' then menu.north = true end
125+
109126
local alignment_start_col = menu.renderer:get_alignment_start_col()
110127

111128
-- place the window at the start col of the current text we're fuzzy matching against

0 commit comments

Comments
 (0)