Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 46d3603

Browse files
committed
Use insertText as textEdit.newText if textEditText is missing
Defaults can have `editRange` set without a `textEdit.newText` or `item.textEditText` This caused an error when applying a snippet as the `newText` was missing.
1 parent ba907ad commit 46d3603

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lua/lsp_compl.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ local function apply_defaults(item, defaults)
142142
if defaults.editRange then
143143
local textEdit = item.textEdit or {}
144144
item.textEdit = textEdit
145-
textEdit.newText = textEdit.newText or item.textEditText
145+
textEdit.newText = textEdit.newText or item.textEditText or item.insertText
146146
if defaults.editRange.start then
147147
textEdit.range = textEdit.range or defaults.editRange
148148
elseif defaults.editRange.insert then
@@ -726,7 +726,11 @@ function M.attach(client, bufnr, opts)
726726
api.nvim_buf_attach(bufnr, false, {
727727
on_detach = function(_, b)
728728
triggers_by_buf[b] = nil
729-
end
729+
end,
730+
on_reload = function(_, b)
731+
M.detach(client.id, b)
732+
M.attach(client, b, opts)
733+
end,
730734
})
731735
end
732736
local signature_triggers = vim.tbl_get(client.server_capabilities, 'signatureHelpProvider', 'triggerCharacters')

tests/lsp_compl_spec.lua

+28
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ describe('lsp_compl', function()
174174
assert.are.same({ line = 1, character = 1}, candidate.user_data.textEdit.range.start)
175175
end)
176176

177+
it("uses insertText as textEdit.newText if there are editRange defaults but no textEditText", function()
178+
local server = new_server({
179+
isIncomplete = false,
180+
itemDefaults = {
181+
editRange = {
182+
start = { line = 1, character = 1 },
183+
['end'] = { line = 1, character = 4 },
184+
},
185+
insertTextFormat = 2,
186+
data = 'foobar',
187+
},
188+
items = {
189+
{
190+
insertText = "the-insertText",
191+
label = 'hello',
192+
data = 'item-property-has-priority',
193+
}
194+
}
195+
})
196+
vim.lsp.start({ name = 'server', cmd = server, on_attach = compl.attach })
197+
api.nvim_buf_set_lines(buf, 0, -1, true, {'a'})
198+
api.nvim_win_set_cursor(win, { 1, 1 })
199+
compl.trigger_completion()
200+
local candidate = capture.matches[1]
201+
local item = candidate.user_data
202+
assert.are.same("the-insertText", item.textEdit.newText)
203+
end)
204+
177205
it('executes commands', function()
178206
local items = {
179207
{

0 commit comments

Comments
 (0)