Skip to content

Commit 49981f2

Browse files
committed
fix: convert additional text edits to utf-8
Closes #397
1 parent e9baeea commit 49981f2

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lua/blink/cmp/completion/accept/init.lua

+6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ local function accept(ctx, item, callback)
1515
sources
1616
.resolve(item)
1717
:map(function(resolved_item)
18+
-- Get additional text edits, converted to utf-8
1819
local all_text_edits =
1920
vim.deepcopy(resolved_item and resolved_item.additionalTextEdits or item.additionalTextEdits or {})
21+
all_text_edits = vim.tbl_map(
22+
function(text_edit) return text_edits_lib.to_utf_8(text_edit, text_edits_lib.offset_encoding_from_item(item)) end,
23+
all_text_edits
24+
)
2025

2126
item = vim.deepcopy(item)
27+
-- TODO: it's not obvious that this is converting to utf-8
2228
item.textEdit = text_edits_lib.get_from_item(item)
2329

2430
-- Create an undo point, if it's not a snippet, since the snippet engine should handle undo

lua/blink/cmp/lib/text_edits.lua

+21-10
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ function text_edits.get_from_item(item)
102102
text_edit.replace = nil
103103
--- @cast text_edit lsp.TextEdit
104104

105-
local client = vim.lsp.get_client_by_id(item.client_id)
106-
local offset_encoding = client ~= nil and client.offset_encoding or 'utf-8'
107-
108105
-- Adjust the position of the text edit to be the current cursor position
109106
-- since the data might be outdated. We compare the cursor column position
110107
-- from when the items were fetched versus the current.
@@ -113,14 +110,25 @@ function text_edits.get_from_item(item)
113110
local offset = vim.api.nvim_win_get_cursor(0)[2] - item.cursor_column
114111
text_edit.range['end'].character = text_edit.range['end'].character + offset
115112

116-
-- convert the offset encoding to utf-8 if necessary
113+
-- convert the offset encoding to utf-8
117114
-- TODO: we have to do this last because it applies a max on the position based on the length of the line
118115
-- so it would break the offset code when removing characters at the end of the line
119-
if offset_encoding ~= 'utf-8' then
120-
text_edit.range.start.character = get_line_byte_from_position(text_edit.range.start, offset_encoding)
121-
text_edit.range['end'].character = get_line_byte_from_position(text_edit.range['end'], offset_encoding)
122-
end
116+
local offset_encoding = text_edits.offset_encoding_from_item(item)
117+
text_edit = text_edits.to_utf_8(text_edit, offset_encoding)
118+
119+
return text_edit
120+
end
123121

122+
function text_edits.offset_encoding_from_item(item)
123+
local client = vim.lsp.get_client_by_id(item.client_id)
124+
return client ~= nil and client.offset_encoding or 'utf-8'
125+
end
126+
127+
function text_edits.to_utf_8(text_edit, offset_encoding)
128+
if offset_encoding == 'utf-8' then return text_edit end
129+
text_edit = vim.deepcopy(text_edit)
130+
text_edit.range.start.character = get_line_byte_from_position(text_edit.range.start, offset_encoding)
131+
text_edit.range['end'].character = get_line_byte_from_position(text_edit.range['end'], offset_encoding)
124132
return text_edit
125133
end
126134

@@ -131,8 +139,11 @@ function text_edits.guess(item)
131139
local word = item.insertText or item.label
132140

133141
local keyword = config.completion.keyword
134-
local range =
135-
require('blink.cmp.lib.utils').get_regex_around_cursor(keyword.range, keyword.regex, keyword.exclude_from_prefix_regex)
142+
local range = require('blink.cmp.lib.utils').get_regex_around_cursor(
143+
keyword.range,
144+
keyword.regex,
145+
keyword.exclude_from_prefix_regex
146+
)
136147
local current_line = vim.api.nvim_win_get_cursor(0)[1]
137148

138149
-- convert to 0-index

0 commit comments

Comments
 (0)