Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ function list.get_item_idx_in_list(item)
end

function list.select(idx, opts)
if idx ~= nil and idx < 0 then idx = math.max(#list.items + idx + 1, 1) end
opts = opts or {}

local item = list.items[idx]

local auto_insert = opts.auto_insert
Expand Down
33 changes: 20 additions & 13 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ function cmp.is_ghost_text_visible() return require('blink.cmp.completion.window
--- @return boolean
function cmp.is_documentation_visible() return require('blink.cmp.completion.windows.documentation').win:is_open() end

--- @class blink.cmp.ShowOpts
--- @field providers? string[] List of providers to show
--- @field initial_selected_item_idx? number The index of the item to select initially
--- @field callback? fun() Called after the menu is shown

--- Show the completion window
--- @param opts? { providers?: string[], initial_selected_item_idx?: number, callback?: fun() }
--- @param opts? blink.cmp.ShowOpts
function cmp.show(opts)
opts = opts or {}

Expand Down Expand Up @@ -92,30 +97,32 @@ end
--- @params opts? { providers?: string[], callback?: fun() }
function cmp.show_and_insert(opts)
opts = opts or {}
opts.initial_selected_item_idx = 1
opts.initial_selected_item_idx = opts.initial_selected_item_idx or 1
return cmp.show(opts)
end

--- Select the first completion item if there are multiple candidates, or accept it if there is only one, after showing
--- @param opts? blink.cmp.CompletionListSelectAndAcceptOpts
--- @param opts? blink.cmp.ShowOpts
function cmp.show_and_insert_or_accept_single(opts)
local list = require('blink.cmp.completion.list')
opts = opts or {}

-- If the candidate list has been filtered down to exactly one item, accept it.
if #list.items == 1 then
vim.schedule(function() list.accept({ index = 1, callback = opts and opts.callback }) end)
vim.schedule(function() list.accept({ index = 1, callback = opts.callback }) end)
return true
end

return cmp.show_and_insert({
callback = function()
if #list.items == 1 then
list.accept({ index = 1, callback = opts and opts.callback })
elseif opts and opts.callback then
opts.callback()
end
end,
})
local callback = opts.callback
opts.initial_selected_item_idx = opts.initial_selected_item_idx or 1
opts.callback = function()
if #list.items == 1 then
list.accept({ index = 1, callback = callback })
elseif callback then
callback()
end
end
return cmp.show(opts)
end

--- Hide the completion window
Expand Down
5 changes: 4 additions & 1 deletion lua/blink/cmp/keymap/presets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ local presets_keymaps = {

cmdline = {
['<Tab>'] = { 'show_and_insert_or_accept_single', 'select_next' },
['<S-Tab>'] = { 'show_and_insert_or_accept_single', 'select_prev' },
['<S-Tab>'] = {
function(cmp) cmp.show_and_insert_or_accept_single({ initial_selected_item_idx = -1 }) end,

This comment was marked as resolved.

This comment was marked as resolved.

'select_prev',
},

['<C-space>'] = { 'show', 'fallback' },

Expand Down