Skip to content

Commit

Permalink
fix: select_and_accept not working with auto_insert
Browse files Browse the repository at this point in the history
closes #118
  • Loading branch information
Saghen committed Oct 18, 2024
1 parent 569156f commit 65eb336
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ cmp.select_and_accept = function()

vim.schedule(function()
-- select an item if none is selected
if not cmp.windows.autocomplete.get_selected_item() then cmp.windows.autocomplete.select_next() end
if not cmp.windows.autocomplete.get_selected_item() then
-- avoid running auto_insert since we're about to accept anyway
cmp.windows.autocomplete.select_next({ skip_auto_insert = true })
end

local item = cmp.windows.autocomplete.get_selected_item()
if item ~= nil then require('blink.cmp.accept')(item) end
Expand Down
15 changes: 9 additions & 6 deletions lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ function autocomplete.accept()
end

--- @param line number
local function select(line)
--- @param skip_auto_insert? boolean
local function select(line, skip_auto_insert)
autocomplete.set_has_selected(true)
vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { line, 0 })

local selected_item = autocomplete.get_selected_item()

-- when auto_insert is enabled, we immediately apply the text edit
if config.windows.autocomplete.selection == 'auto_insert' and selected_item ~= nil then
if config.windows.autocomplete.selection == 'auto_insert' and selected_item ~= nil and not skip_auto_insert then
require('blink.cmp.trigger.completion').suppress_events_for_callback(function()
if autocomplete.preview_text_edit ~= nil and autocomplete.preview_context_id == autocomplete.context.id then
text_edits_lib.undo_text_edit(autocomplete.preview_text_edit)
Expand All @@ -195,7 +196,8 @@ local function select(line)
autocomplete.event_targets.on_select(selected_item, autocomplete.context)
end

function autocomplete.select_next()
--- @params opts? { skip_auto_insert?: boolean }
function autocomplete.select_next(opts)
if not autocomplete.win:is_open() then return end

local cycle_from_bottom = config.windows.autocomplete.cycle.from_bottom
Expand All @@ -213,10 +215,11 @@ function autocomplete.select_next()
line = line + 1
end

select(line)
select(line, opts and opts.skip_auto_insert)
end

function autocomplete.select_prev()
--- @params opts? { skip_auto_insert?: boolean }
function autocomplete.select_prev(opts)
if not autocomplete.win:is_open() then return end

local cycle_from_top = config.windows.autocomplete.cycle.from_top
Expand All @@ -230,7 +233,7 @@ function autocomplete.select_prev()
line = line - 1
end

select(line)
select(line, opts and opts.skip_auto_insert)
end

function autocomplete.listen_on_select(callback) autocomplete.event_targets.on_select = callback end
Expand Down

0 comments on commit 65eb336

Please sign in to comment.