@@ -56,46 +56,54 @@ local function get_line_byte_from_position(position, offset_encoding)
56
56
end
57
57
58
58
--- Gets the text edit from an item, handling insert/replace ranges and converts
59
- --- offset encodings (utf-16 | utf-32) to byte offset (equivalent to utf-8)
59
+ --- offset encodings (utf-16 | utf-32) to utf-8
60
60
--- @param item blink.cmp.CompletionItem
61
61
--- @return lsp.TextEdit
62
62
function text_edits .get_from_item (item )
63
- -- Adjust the position of the text edit to be the current cursor position
64
- -- since the data might be outdated. We compare the cursor column position
65
- -- from when the items were fetched versus the current.
66
- -- hack: is there a better way?
67
- if item .textEdit ~= nil then
68
- -- FIXME: temporarily convert insertReplaceEdit to regular textEdit
69
- if item .textEdit .insert ~= nil then
70
- item .textEdit .range = item .textEdit .insert
71
- elseif item .textEdit .replace ~= nil then
72
- item .textEdit .range = item .textEdit .replace
73
- end
63
+ local text_edit = vim .deepcopy (item .textEdit )
64
+
65
+ -- Fallback to default edit range if defined, and no text edit was set
66
+ if text_edit == nil and item .editRange ~= nil then
67
+ text_edit = {
68
+ newText = item .textEditText or item .insertText or item .label ,
69
+ -- FIXME: temporarily convert insertReplaceEdit to regular textEdit
70
+ range = item .editRange .insert or item .editRange .replace or item .editRange ,
71
+ }
72
+ end
74
73
75
- local text_edit = vim .deepcopy (item .textEdit )
74
+ -- Guess the text edit if the item doesn't define it
75
+ if text_edit == nil then return text_edits .guess (item ) end
76
76
77
- local client = vim .lsp .get_client_by_id (item .client_id )
78
- local offset_encoding = client ~= nil and client .offset_encoding or ' utf-8'
77
+ -- FIXME: temporarily convert insertReplaceEdit to regular textEdit
78
+ text_edit .range = text_edit .range or text_edit .insert or text_edit .replace
79
+ text_edit .insert = nil
80
+ text_edit .replace = nil
81
+ --- @cast text_edit lsp.TextEdit
79
82
80
- if offset_encoding ~= ' utf-8' then
81
- text_edit .range .start .character = get_line_byte_from_position (text_edit .range .start , offset_encoding )
82
- text_edit .range [' end' ].character = get_line_byte_from_position (text_edit .range [' end' ], offset_encoding )
83
- end
83
+ local client = vim .lsp .get_client_by_id (item .client_id )
84
+ local offset_encoding = client ~= nil and client .offset_encoding or ' utf-8'
84
85
85
- local offset = vim .api .nvim_win_get_cursor (0 )[2 ] - item .cursor_column
86
- text_edit .range [' end' ].character = text_edit .range [' end' ].character + offset
87
- return text_edit
86
+ -- convert the offset encoding to utf-8 if necessary
87
+ if offset_encoding ~= ' utf-8' then
88
+ text_edit .range .start .character = get_line_byte_from_position (text_edit .range .start , offset_encoding )
89
+ text_edit .range [' end' ].character = get_line_byte_from_position (text_edit .range [' end' ], offset_encoding )
88
90
end
89
91
90
- -- No text edit so we fallback to our own resolution
91
- return text_edits .guess (item )
92
+ -- Adjust the position of the text edit to be the current cursor position
93
+ -- since the data might be outdated. We compare the cursor column position
94
+ -- from when the items were fetched versus the current.
95
+ -- HACK: is there a better way?
96
+ -- TODO: take into account the offset_encoding
97
+ local offset = vim .api .nvim_win_get_cursor (0 )[2 ] - item .cursor_column
98
+ text_edit .range [' end' ].character = text_edit .range [' end' ].character + offset
99
+ return text_edit
92
100
end
93
101
94
102
--- Uses the keyword_regex to guess the text edit ranges
95
103
--- @param item blink.cmp.CompletionItem
96
104
--- TODO: doesnt work when the item contains characters not included in the context regex
97
105
function text_edits .guess (item )
98
- local word = item .textEditText or item . insertText or item .label
106
+ local word = item .insertText or item .label
99
107
100
108
local cmp_config = config .trigger .completion
101
109
local range = require (' blink.cmp.utils' ).get_regex_around_cursor (
0 commit comments