Skip to content

Commit

Permalink
feat: support accepting and drawing by index
Browse files Browse the repository at this point in the history
Closes #382
  • Loading branch information
Saghen committed Nov 30, 2024
1 parent cb815af commit 4b1a793
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions lua/blink/cmp/completion/windows/render/context.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4b1a793

Please sign in to comment.