Skip to content

Commit de85dec

Browse files
committed
(mini.completion) Respect InsertReplaceText for LSP completion start.
Details: - Resolves #708.
1 parent be7c6b5 commit de85dec

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lua/mini/completion.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -1304,9 +1304,11 @@ H.get_completion_start_server = function(response_data, line_num)
13041304
if response_data.err or type(response_data.result) ~= 'table' then return end
13051305
local items = response_data.result.items or response_data.result
13061306
for _, item in pairs(items) do
1307-
if type(item.textEdit) == 'table' and item.textEdit.range.start.line == line_num then
1308-
-- NOTE: Ignore case when items contain several conflicting starts
1309-
return item.textEdit.range.start.character
1307+
if type(item.textEdit) == 'table' then
1308+
-- NOTE: As per LSP spec, `textEdit` can be either `TextEdit` or `InsertReplaceEdit`
1309+
local range = type(item.textEdit.range) == 'table' and item.textEdit.range or item.textEdit.insert
1310+
-- NOTE: Return immediately, ignoring possibly several conflicting starts
1311+
return range.start.character
13101312
end
13111313
end
13121314
end

tests/dir-completion/mock-months-lsp.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@ local construct_additionTextEdits = function(id, name)
4444
}
4545
end
4646

47-
local construct_textEdit = function(name)
47+
local construct_textEdit = function(name, kind)
4848
if _G.mock_textEdit == nil then return end
4949
local new_text, pos = _G.mock_textEdit.new_text, _G.mock_textEdit.pos
50+
local is_insertreplaceedit = kind == 'InsertReplaceEdit'
51+
local range = {
52+
start = { line = pos[1] - 1, character = pos[2] - 1 },
53+
['end'] = { line = pos[1] - 1, character = pos[2] },
54+
}
5055
return {
5156
newText = new_text(name),
52-
range = {
53-
start = { line = pos[1] - 1, character = pos[2] - 1 },
54-
['end'] = { line = pos[1] - 1, character = pos[2] },
55-
},
57+
[is_insertreplaceedit and 'insert' or 'range'] = range,
58+
replace = is_insertreplaceedit and range or nil,
5659
}
5760
end
5861

@@ -71,8 +74,12 @@ Months.requests = {
7174
res.additionalTextEdits = construct_additionTextEdits('completion', item.name)
7275
end
7376

74-
if vim.tbl_contains({ 'April', 'August' }, item.name) then
75-
res.textEdit = construct_textEdit(item.name)
77+
if item.name == 'April' then
78+
res.textEdit = construct_textEdit(item.name, 'InsertReplaceEdit')
79+
res.filterText = construct_filterText(item.name)
80+
end
81+
if item.name == 'August' then
82+
res.textEdit = construct_textEdit(item.name, 'textEdit')
7683
res.filterText = construct_filterText(item.name)
7784
end
7885

0 commit comments

Comments
 (0)