1
1
local config = require (' blink.cmp.config' )
2
2
local text_edits = {}
3
3
4
+ --- Position is a https://microsoft.github.io/language-server-protocol/specifications/specification-current/#position
5
+ --- @param position lsp.Position
6
+ --- @param offset_encoding string | nil utf-8 | utf-16 | utf-32
7
+ --- @return integer
8
+ local function get_line_byte_from_position (position , offset_encoding )
9
+ local bufnr = vim .api .nvim_get_current_buf ()
10
+ -- LSP's line and characters are 0-indexed
11
+ -- Vim's line and columns are 1-indexed
12
+ local col = position .character
13
+ -- When on the first character, we can ignore the difference between byte and
14
+ -- character
15
+ if col > 0 then
16
+ local line = vim .api .nvim_buf_get_lines (bufnr , position .line , position .line + 1 , true )[1 ] or ' '
17
+ return vim .lsp .util ._str_byteindex_enc (line , col , offset_encoding or ' utf-16' )
18
+ end
19
+ return col
20
+ end
21
+
4
22
--- @param item blink.cmp.CompletionItem
5
23
--- @return lsp.TextEdit
6
24
function text_edits .get_from_item (item )
@@ -17,6 +35,15 @@ function text_edits.get_from_item(item)
17
35
end
18
36
19
37
local text_edit = vim .deepcopy (item .textEdit )
38
+
39
+ local client = vim .lsp .get_client_by_id (client_id )
40
+ local offset_encoding = client ~= nil and client .offset_encoding or ' utf-8'
41
+
42
+ if offset_encoding ~= ' utf-8' then
43
+ text_edit .range .start .character = get_line_byte_from_position (text_edit .range .start , offset_encoding )
44
+ text_edit .range [' end' ].character = get_line_byte_from_position (text_edit .range [' end' ], offset_encoding )
45
+ end
46
+
20
47
local offset = vim .api .nvim_win_get_cursor (0 )[2 ] - item .cursor_column
21
48
text_edit .range [' end' ].character = text_edit .range [' end' ].character + offset
22
49
return text_edit
@@ -26,12 +53,9 @@ function text_edits.get_from_item(item)
26
53
return text_edits .guess_text_edit (item )
27
54
end
28
55
29
- --- @param client_id number
30
56
--- @param edits lsp.TextEdit[]
31
- function text_edits .apply_text_edits (client_id , edits )
32
- local client = vim .lsp .get_client_by_id (client_id )
33
- local offset_encoding = client ~= nil and client .offset_encoding or ' utf-16'
34
- vim .lsp .util .apply_text_edits (edits , vim .api .nvim_get_current_buf (), offset_encoding )
57
+ function text_edits .apply_text_edits (edits )
58
+ vim .lsp .util .apply_text_edits (edits , vim .api .nvim_get_current_buf (), ' utf-8' )
35
59
end
36
60
37
61
--- @param text_edit lsp.TextEdit
@@ -52,7 +76,7 @@ function text_edits.undo_text_edit(text_edit)
52
76
text_edit .range = text_edits .get_undo_text_edit_range (text_edit )
53
77
text_edit .newText = ' '
54
78
55
- vim . lsp . util . apply_text_edits ({ text_edit }, vim . api . nvim_get_current_buf (), ' utf-16 ' )
79
+ text_edits . apply_text_edits ({ text_edit })
56
80
end
57
81
58
82
--- @param item blink.cmp.CompletionItem
@@ -69,13 +93,15 @@ function text_edits.guess_text_edit(item)
69
93
local current_line = vim .api .nvim_win_get_cursor (0 )[1 ]
70
94
71
95
-- convert to 0-index
72
- return {
96
+ local text_edit = {
73
97
range = {
74
98
start = { line = current_line - 1 , character = range .start_col - 1 },
75
99
[' end' ] = { line = current_line - 1 , character = range .start_col - 1 + range .length },
76
100
},
77
101
newText = word ,
78
102
}
103
+
104
+ return text_edit
79
105
end
80
106
81
107
return text_edits
0 commit comments