Skip to content

Commit 78ac56e

Browse files
committed
fix: auto insert breaking on single line text edit
closes #169
1 parent 7d6b50b commit 78ac56e

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

lua/blink/cmp/accept/preview.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--- @param item blink.cmp.CompletionItem
2-
local function preview(item)
2+
local function preview(item, previous_text_edit)
33
local text_edits_lib = require('blink.cmp.accept.text-edits')
44
local text_edit = text_edits_lib.get_from_item(item)
55

@@ -9,6 +9,8 @@ local function preview(item)
99
text_edit.newText = expanded_snippet and tostring(expanded_snippet) or text_edit.newText
1010
end
1111

12+
if previous_text_edit ~= nil then text_edit.range = text_edits_lib.get_undo_text_edit_range(previous_text_edit) end
13+
1214
text_edits_lib.apply_text_edits(item.client_id, { text_edit })
1315
vim.api.nvim_win_set_cursor(0, {
1416
text_edit.range.start.line + 1,

lua/blink/cmp/accept/text-edits.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ function text_edits.apply_text_edits(client_id, edits)
2828
end
2929

3030
--- @param text_edit lsp.TextEdit
31-
function text_edits.undo_text_edit(text_edit)
31+
function text_edits.get_undo_text_edit_range(text_edit)
3232
text_edit = vim.deepcopy(text_edit)
3333
local lines = vim.split(text_edit.newText, '\n')
34-
local range = text_edit.range
34+
local last_line_len = lines[#lines] and #lines[#lines] or 0
3535

36+
local range = text_edit.range
3637
range['end'].line = range.start.line + #lines - 1
37-
range['end'].character = lines[#lines] and #lines[#lines] or 0
38-
text_edit.newText = ''
38+
range['end'].character = #lines > 1 and last_line_len or range.start.character + last_line_len
3939

40-
vim.lsp.util.apply_text_edits({ text_edit }, vim.api.nvim_get_current_buf(), 'utf-16')
40+
return range
4141
end
4242

4343
--- @param item blink.cmp.CompletionItem

lua/blink/cmp/windows/autocomplete.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ local function select(line, skip_auto_insert)
185185
-- when auto_insert is enabled, we immediately apply the text edit
186186
if config.windows.autocomplete.selection == 'auto_insert' and selected_item ~= nil and not skip_auto_insert then
187187
require('blink.cmp.trigger.completion').suppress_events_for_callback(function()
188-
if autocomplete.preview_text_edit ~= nil and autocomplete.preview_context_id == autocomplete.context.id then
189-
text_edits_lib.undo_text_edit(autocomplete.preview_text_edit)
190-
end
191-
autocomplete.preview_text_edit = require('blink.cmp.accept.preview')(selected_item)
188+
if autocomplete.preview_context_id ~= autocomplete.context.id then autocomplete.preview_text_edit = nil end
189+
autocomplete.preview_text_edit =
190+
require('blink.cmp.accept.preview')(selected_item, autocomplete.preview_text_edit)
192191
autocomplete.preview_context_id = autocomplete.context.id
193192
end)
194193
end

0 commit comments

Comments
 (0)