Skip to content

Commit

Permalink
refactor: re-order text edit apply args
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Jan 19, 2025
1 parent 13ca003 commit d5b9431
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lua/blink/cmp/completion/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ local function accept(ctx, item, callback)
-- so we empty the newText and apply
local temp_text_edit = vim.deepcopy(item.textEdit)
temp_text_edit.newText = ''
text_edits_lib.apply(all_text_edits, temp_text_edit)
text_edits_lib.apply(temp_text_edit, all_text_edits)

-- Expand the snippet
require('blink.cmp.config').snippets.expand(item.textEdit.newText)

-- OR Normal: Apply the text edit and move the cursor
else
text_edits_lib.apply(all_text_edits, item.textEdit)
text_edits_lib.apply(item.textEdit, all_text_edits)

-- TODO: should move the cursor only by the offset since text edit handles everything else?
ctx.set_cursor({ ctx.get_cursor()[1], item.textEdit.range.start.character + #item.textEdit.newText + offset })
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/accept/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local function preview(item)
text_edit.range.start.character + #text_edit.newText,
}

text_edits_lib.apply({}, text_edit)
text_edits_lib.apply(text_edit)

local original_cursor = vim.api.nvim_win_get_cursor(0)
local cursor_moved = false
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ end
function list.undo_preview()
if list.preview_undo == nil then return end

require('blink.cmp.lib.text_edits').apply({}, list.preview_undo.text_edit)
require('blink.cmp.lib.text_edits').apply(list.preview_undo.text_edit)
if list.preview_undo.cursor then
require('blink.cmp.completion.trigger.context').set_cursor(list.preview_undo.cursor)
end
Expand Down
7 changes: 3 additions & 4 deletions lua/blink/cmp/lib/text_edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ local context = require('blink.cmp.completion.trigger.context')
local text_edits = {}

--- Applies one or more text edits to the current buffer, assuming utf-8 encoding
--- @async
--- @param additional_text_edits lsp.TextEdit[] # additional text edits that can e.g. add import statements.
--- @param text_edit lsp.TextEdit # the main text edit (at the cursor). Can be repeated.
function text_edits.apply(additional_text_edits, text_edit)
--- @param additional_text_edits? lsp.TextEdit[] # additional text edits that can e.g. add import statements.
function text_edits.apply(text_edit, additional_text_edits)
local mode = context.get_mode()
if mode == 'default' then
vim.lsp.util.apply_text_edits(additional_text_edits, vim.api.nvim_get_current_buf(), 'utf-8')
vim.lsp.util.apply_text_edits(additional_text_edits or {}, vim.api.nvim_get_current_buf(), 'utf-8')

-- Fill the `.` register so that dot-repeat works. This also changes the
-- text in the buffer - currently there is no way to do this in Neovim
Expand Down

0 comments on commit d5b9431

Please sign in to comment.