fix(lsp): trim trailing whitespace from completion words (#29122)

the `complete()` mechanism doesn't play nicely with trailing newlines or
tabs. A newline causes it to insert a null character, showing up as
`^@`.
This commit is contained in:
Mathias Fußenegger
2024-06-02 09:54:15 +02:00
committed by GitHub
parent 24cb9ba6d3
commit 19be3d2683
2 changed files with 37 additions and 1 deletions

View File

@ -153,7 +153,8 @@ local function get_completion_word(item)
return item.label
end
elseif item.textEdit then
return item.textEdit.newText
local word = item.textEdit.newText
return word:match('^(%S*)') or word
elseif item.insertText and item.insertText ~= '' then
return item.insertText
end

View File

@ -126,6 +126,41 @@ describe('vim.lsp.completion: item conversion', function()
eq(expected, result)
end)
it('trims trailing newline or tab from textEdit', function()
local range0 = {
start = { line = 0, character = 0 },
['end'] = { line = 0, character = 0 },
}
local items = {
{
detail = 'ansible.builtin',
filterText = 'lineinfile ansible.builtin.lineinfile builtin ansible',
kind = 7,
label = 'ansible.builtin.lineinfile',
sortText = '2_ansible.builtin.lineinfile',
textEdit = {
newText = 'ansible.builtin.lineinfile:\n ',
range = range0,
},
},
}
local result = complete('|', items)
result = vim.tbl_map(function(x)
return {
abbr = x.abbr,
word = x.word,
}
end, result.items)
local expected = {
{
abbr = 'ansible.builtin.lineinfile',
word = 'ansible.builtin.lineinfile:',
},
}
eq(expected, result)
end)
it('prefers wordlike components for snippets', function()
-- There are two goals here:
--