diff --git a/lua/blink/cmp/completion/list.lua b/lua/blink/cmp/completion/list.lua index 64617766..b80b72ac 100644 --- a/lua/blink/cmp/completion/list.lua +++ b/lua/blink/cmp/completion/list.lua @@ -21,7 +21,10 @@ --- --- @field undo_preview fun() --- @field apply_preview fun(item: blink.cmp.CompletionItem) ---- @field accept fun(): boolean Applies the currently selected item, returning true if it succeeded +--- @field accept fun(opts?: blink.cmp.CompletionListAcceptOpts): boolean Applies the currently selected item, returning true if it succeeded + +--- @class blink.cmp.CompletionListAcceptOpts +--- @field index? number The index of the item to accept, if not provided, the currently selected item will be accepted --- @class blink.cmp.CompletionListShowEvent --- @field items blink.cmp.CompletionItem[] @@ -164,8 +167,9 @@ end ---------- Accept ---------- -function list.accept() - local item = list.get_selected_item() +function list.accept(opts) + opts = opts or {} + local item = list.items[opts.index or list.selected_item_idx] if item == nil then return false end list.undo_preview() diff --git a/lua/blink/cmp/completion/windows/render/context.lua b/lua/blink/cmp/completion/windows/render/context.lua index dafebcf2..cf72b447 100644 --- a/lua/blink/cmp/completion/windows/render/context.lua +++ b/lua/blink/cmp/completion/windows/render/context.lua @@ -1,6 +1,7 @@ --- @class blink.cmp.DrawItemContext --- @field self blink.cmp.Draw --- @field item blink.cmp.CompletionItem +--- @field idx number --- @field label string --- @field label_detail string --- @field label_description string @@ -24,16 +25,17 @@ function context.get_from_items(draw, items) local ctxs = {} for idx, item in ipairs(items) do - ctxs[idx] = context.new(draw, item, matched_indices[idx]) + ctxs[idx] = context.new(draw, idx, item, matched_indices[idx]) end return ctxs end --- @param draw blink.cmp.Draw +--- @param item_idx number --- @param item blink.cmp.CompletionItem --- @param matched_indices number[] --- @return blink.cmp.DrawItemContext -function context.new(draw, item, matched_indices) +function context.new(draw, item_idx, item, matched_indices) local config = require('blink.cmp.config').appearance local kind = require('blink.cmp.types').CompletionItemKind[item.kind] or 'Unknown' local kind_icon = config.kind_icons[kind] or config.kind_icons.Field @@ -54,6 +56,7 @@ function context.new(draw, item, matched_indices) return { self = draw, item = item, + idx = item_idx, label = label, label_detail = label_detail, label_description = label_description, diff --git a/lua/blink/cmp/init.lua b/lua/blink/cmp/init.lua index 8b76d4da..c9279561 100644 --- a/lua/blink/cmp/init.lua +++ b/lua/blink/cmp/init.lua @@ -41,14 +41,15 @@ function cmp.cancel() return true end -function cmp.accept() +--- @param opts? blink.cmp.CompletionListAcceptOpts +function cmp.accept(opts) if not require('blink.cmp.completion.windows.menu').win:is_open() then return end local completion_list = require('blink.cmp.completion.list') local item = completion_list.get_selected_item() if item == nil then return end - vim.schedule(function() completion_list.accept() end) + vim.schedule(function() completion_list.accept(opts) end) return true end