diff --git a/README.md b/README.md index e5cde8f4..b15e0332 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,10 @@ MiniDeps.add({ -- Which directions to show the window, -- falling back to the next direction when there's not enough space direction_priority = { 's', 'n' }, + + -- Whether to automatically show the window when new completion items are available + auto_show = true, + -- Controls how the completion items are rendered on the popup window draw = { -- Aligns the keyword you've typed to a component in the menu diff --git a/lua/blink/cmp/completion/windows/menu.lua b/lua/blink/cmp/completion/windows/menu.lua index 9fc892e3..be932b5f 100644 --- a/lua/blink/cmp/completion/windows/menu.lua +++ b/lua/blink/cmp/completion/windows/menu.lua @@ -31,6 +31,7 @@ local menu = { }), items = {}, context = nil, + auto_show = config.auto_show, open_emitter = require('blink.cmp.lib.event_emitter').new('completion_menu_open', 'BlinkCmpCompletionMenuOpen'), close_emitter = require('blink.cmp.lib.event_emitter').new('completion_menu_close', 'BlinkCmpCompletionMenuClose'), position_update_emitter = require('blink.cmp.lib.event_emitter').new( @@ -43,8 +44,6 @@ vim.api.nvim_create_autocmd({ 'CursorMovedI', 'WinScrolled', 'WinResized' }, { callback = function() menu.update_position() end, }) ---- @param context blink.cmp.Context ---- @param items blink.cmp.CompletionItem[] function menu.open_with_items(context, items) menu.context = context menu.items = items @@ -53,12 +52,7 @@ function menu.open_with_items(context, items) if not menu.renderer then menu.renderer = require('blink.cmp.completion.windows.render').new(config.draw) end menu.renderer:draw(menu.win:get_buf(), items) - menu.open() - menu.update_position() - - -- it's possible for the window to close after updating the position - -- if there was nowhere to place the window - if not menu.win:is_open() then return end + if menu.auto_show then menu.open() end end function menu.open() @@ -68,11 +62,13 @@ function menu.open() if menu.selected_item_idx ~= nil then vim.api.nvim_win_set_cursor(menu.win:get_win(), { menu.selected_item_idx, 0 }) end + menu.update_position() menu.open_emitter:emit() end function menu.close() + menu.auto_show = config.auto_show if not menu.win:is_open() then return end menu.win:close() diff --git a/lua/blink/cmp/config/completion/menu.lua b/lua/blink/cmp/config/completion/menu.lua index e378807b..876bbe11 100644 --- a/lua/blink/cmp/config/completion/menu.lua +++ b/lua/blink/cmp/config/completion/menu.lua @@ -9,6 +9,7 @@ --- @field winblend number --- @field winhighlight string --- @field scrolloff number Keep the cursor X lines away from the top/bottom of the window +--- @field auto_show boolean Whether to automatically show the window when new completion items are available --- @field draw blink.cmp.Draw Controls how the completion items are rendered on the popup window --- @class (exact) blink.cmp.CompletionMenuOrderConfig @@ -36,6 +37,9 @@ local window = { -- TODO: implement order = { n = 'bottom_up', s = 'top_down' }, + -- Whether to automatically show the window when new completion items are available + auto_show = true, + -- Controls how the completion items are rendered on the popup window draw = { -- Aligns the keyword you've typed to a component in the menu diff --git a/lua/blink/cmp/init.lua b/lua/blink/cmp/init.lua index 6818fb4a..85278f1a 100644 --- a/lua/blink/cmp/init.lua +++ b/lua/blink/cmp/init.lua @@ -22,7 +22,10 @@ end function cmp.show() if require('blink.cmp.completion.windows.menu').win:is_open() then return end - vim.schedule(function() require('blink.cmp.completion.trigger').show({ force = true }) end) + vim.schedule(function() + require('blink.cmp.completion.windows.menu').auto_show = true + require('blink.cmp.completion.trigger').show({ force = true }) + end) return true end