Skip to content

Commit

Permalink
feat: add callback to hide/cancel, rework show callback
Browse files Browse the repository at this point in the history
Closes #806
  • Loading branch information
Saghen committed Dec 30, 2024
1 parent fa4312c commit 73a5f4e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lua/blink/cmp/completion/trigger/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
--- @field is_trigger_character fun(char: string, is_show_on_x?: boolean): boolean
--- @field suppress_events_for_callback fun(cb: fun())
--- @field show_if_on_trigger_character fun(opts?: { is_accept?: boolean })
--- @field show fun(opts?: { trigger_kind: blink.cmp.CompletionTriggerKind, trigger_character?: string, force?: boolean, send_upstream?: boolean, providers?: string[] })
--- @field show fun(opts?: { trigger_kind: blink.cmp.CompletionTriggerKind, trigger_character?: string, force?: boolean, send_upstream?: boolean, providers?: string[] }): blink.cmp.Context
--- @field hide fun()
--- @field within_query_bounds fun(cursor: number[]): boolean
--- @field get_bounds fun(regex: vim.regex, line: string, cursor: number[]): blink.cmp.ContextBounds
Expand Down Expand Up @@ -204,6 +204,7 @@ function trigger.show(opts)
})

if opts.send_upstream ~= false then trigger.show_emitter:emit({ context = trigger.context }) end
return trigger.context
end

function trigger.hide()
Expand Down
40 changes: 30 additions & 10 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,57 @@ function cmp.is_visible()
or require('blink.cmp.completion.windows.ghost_text').is_open()
end

--- @params opts? { providers?: string[], callback?: fun(did_show: boolean) }
--- @params opts? { providers?: string[], callback?: fun() }
function cmp.show(opts)
if require('blink.cmp.completion.windows.menu').win:is_open() and not (opts and opts.providers) then
if opts and opts.callback then opts.callback(false) end
return
end
opts = opts or {}

-- TODO: when passed a list of providers, we should check if we're already showing the menu
-- with that list of providers
if require('blink.cmp.completion.windows.menu').win:is_open() and not (opts and opts.providers) then return end

vim.schedule(function()
require('blink.cmp.completion.windows.menu').auto_show = true
require('blink.cmp.completion.trigger').show({

-- HACK: because blink is event based, we don't have an easy way to know when the "show"
-- event completes. So we wait for the menu to open and check if we're in the same context
local context
if opts.callback then
vim.api.nvim_create_autocmd('User', {
pattern = 'BlinkCmpShow',
callback = function(event)
if context ~= nil and event.data.context.id == context.id then opts.callback() end
end,
once = true,
})
end

context = require('blink.cmp.completion.trigger').show({
force = true,
providers = opts and opts.providers,
trigger_kind = 'manual',
})
if opts and opts.callback then opts.callback(true) end
end)
return true
end

function cmp.hide()
--- @params opts? { callback?: fun() }
function cmp.hide(opts)
if not cmp.is_visible() then return end

vim.schedule(require('blink.cmp.completion.trigger').hide)
vim.schedule(function()
require('blink.cmp.completion.trigger').hide()
if opts and opts.callback then opts.callback() end
end)
return true
end

function cmp.cancel()
--- @params opts? { callback?: fun() }
function cmp.cancel(opts)
if not cmp.is_visible() then return end
vim.schedule(function()
require('blink.cmp.completion.list').undo_preview()
require('blink.cmp.completion.trigger').hide()
if opts and opts.callback then opts.callback() end
end)
return true
end
Expand Down

0 comments on commit 73a5f4e

Please sign in to comment.