Skip to content

Commit

Permalink
feat: add auto_show property for menu
Browse files Browse the repository at this point in the history
Closes #402
  • Loading branch information
Saghen committed Dec 3, 2024
1 parent 8c1fdc9 commit 29fe017
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions lua/blink/cmp/completion/windows/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions lua/blink/cmp/config/completion/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 29fe017

Please sign in to comment.