Skip to content

Commit

Permalink
feat: support callback on cmp.accept()
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 13, 2024
1 parent 97989c8 commit be3e9cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
--- @field apply_preview fun(item: blink.cmp.CompletionItem)
--- @field accept fun(opts?: blink.cmp.CompletionListAcceptOpts): boolean Applies the currently selected item, returning true if it succeeded

--- @class blink.cmp.CompletionListAcceptOpts
--- @class blink.cmp.CompletionListSelectAndAcceptOpts
--- @field callback? fun() Called after the item is accepted

--- @class blink.cmp.CompletionListAcceptOpts : blink.cmp.CompletionListSelectAndAcceptOpts
--- @field index? number The index of the item to accept, if not provided, the currently selected item will be accepted

--- @class blink.cmp.CompletionListShowEvent
Expand Down Expand Up @@ -210,7 +213,10 @@ function list.accept(opts)

list.undo_preview()
local accept = require('blink.cmp.completion.accept')
accept(list.context, item, function() list.accept_emitter:emit({ item = item, context = list.context }) end)
accept(list.context, item, function()
list.accept_emitter:emit({ item = item, context = list.context })
if opts.callback then opts.callback() end
end)
return true
end

Expand Down
12 changes: 10 additions & 2 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ function cmp.accept(opts)
return true
end

function cmp.select_and_accept()
--- @param opts? blink.cmp.CompletionListSelectAndAcceptOpts
function cmp.select_and_accept(opts)
if not cmp.is_visible() then return end

local completion_list = require('blink.cmp.completion.list')
vim.schedule(function() completion_list.accept({ index = completion_list.selected_item_idx or 1 }) end)
vim.schedule(
function()
completion_list.accept({
index = completion_list.selected_item_idx or 1,
callback = opts and opts.callback,
})
end
)
return true
end

Expand Down

0 comments on commit be3e9cf

Please sign in to comment.