Skip to content

Commit 29fe017

Browse files
committed
feat: add auto_show property for menu
Closes #402
1 parent 8c1fdc9 commit 29fe017

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ MiniDeps.add({
363363
-- Which directions to show the window,
364364
-- falling back to the next direction when there's not enough space
365365
direction_priority = { 's', 'n' },
366+
367+
-- Whether to automatically show the window when new completion items are available
368+
auto_show = true,
369+
366370
-- Controls how the completion items are rendered on the popup window
367371
draw = {
368372
-- Aligns the keyword you've typed to a component in the menu

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ local menu = {
3131
}),
3232
items = {},
3333
context = nil,
34+
auto_show = config.auto_show,
3435
open_emitter = require('blink.cmp.lib.event_emitter').new('completion_menu_open', 'BlinkCmpCompletionMenuOpen'),
3536
close_emitter = require('blink.cmp.lib.event_emitter').new('completion_menu_close', 'BlinkCmpCompletionMenuClose'),
3637
position_update_emitter = require('blink.cmp.lib.event_emitter').new(
@@ -43,8 +44,6 @@ vim.api.nvim_create_autocmd({ 'CursorMovedI', 'WinScrolled', 'WinResized' }, {
4344
callback = function() menu.update_position() end,
4445
})
4546

46-
--- @param context blink.cmp.Context
47-
--- @param items blink.cmp.CompletionItem[]
4847
function menu.open_with_items(context, items)
4948
menu.context = context
5049
menu.items = items
@@ -53,12 +52,7 @@ function menu.open_with_items(context, items)
5352
if not menu.renderer then menu.renderer = require('blink.cmp.completion.windows.render').new(config.draw) end
5453
menu.renderer:draw(menu.win:get_buf(), items)
5554

56-
menu.open()
57-
menu.update_position()
58-
59-
-- it's possible for the window to close after updating the position
60-
-- if there was nowhere to place the window
61-
if not menu.win:is_open() then return end
55+
if menu.auto_show then menu.open() end
6256
end
6357

6458
function menu.open()
@@ -68,11 +62,13 @@ function menu.open()
6862
if menu.selected_item_idx ~= nil then
6963
vim.api.nvim_win_set_cursor(menu.win:get_win(), { menu.selected_item_idx, 0 })
7064
end
65+
menu.update_position()
7166

7267
menu.open_emitter:emit()
7368
end
7469

7570
function menu.close()
71+
menu.auto_show = config.auto_show
7672
if not menu.win:is_open() then return end
7773

7874
menu.win:close()

lua/blink/cmp/config/completion/menu.lua

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
--- @field winblend number
1010
--- @field winhighlight string
1111
--- @field scrolloff number Keep the cursor X lines away from the top/bottom of the window
12+
--- @field auto_show boolean Whether to automatically show the window when new completion items are available
1213
--- @field draw blink.cmp.Draw Controls how the completion items are rendered on the popup window
1314

1415
--- @class (exact) blink.cmp.CompletionMenuOrderConfig
@@ -36,6 +37,9 @@ local window = {
3637
-- TODO: implement
3738
order = { n = 'bottom_up', s = 'top_down' },
3839

40+
-- Whether to automatically show the window when new completion items are available
41+
auto_show = true,
42+
3943
-- Controls how the completion items are rendered on the popup window
4044
draw = {
4145
-- Aligns the keyword you've typed to a component in the menu

lua/blink/cmp/init.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ end
2222
function cmp.show()
2323
if require('blink.cmp.completion.windows.menu').win:is_open() then return end
2424

25-
vim.schedule(function() require('blink.cmp.completion.trigger').show({ force = true }) end)
25+
vim.schedule(function()
26+
require('blink.cmp.completion.windows.menu').auto_show = true
27+
require('blink.cmp.completion.trigger').show({ force = true })
28+
end)
2629
return true
2730
end
2831

0 commit comments

Comments
 (0)