fix(lsp): completion word includes leading space from label #38435

Problem: clangd prepends a space/bullet indicator to label. With
labelDetailsSupport enabled, the signature moves to labelDetails,
making label shorter. This flips the length comparison in
get_completion_word, causing it to use item.label directly and
insert the indicator into the buffer.

Solution: only prefer filterText over label when label starts with non-keyword
character in get_completion_word fallback branch.
This commit is contained in:
glepnir
2026-03-23 23:02:30 +08:00
committed by GitHub
parent e2afa762c8
commit f2d0b06ecb
2 changed files with 18 additions and 4 deletions

View File

@@ -195,7 +195,11 @@ local function get_completion_word(item, prefix, match)
-- --
-- Typing `i` would remove the candidate because newText starts with `t`. -- Typing `i` would remove the candidate because newText starts with `t`.
local text = parse_snippet(item.insertText or item.textEdit.newText) local text = parse_snippet(item.insertText or item.textEdit.newText)
local word = #text < #item.label and vim.fn.matchstr(text, '\\k*') or item.label local word = #text < #item.label and vim.fn.matchstr(text, '\\k*')
or (
item.filterText and vim.fn.match(item.label, '^\\k') == -1 and item.filterText
or item.label
)
if item.filterText and not match(word, prefix) then if item.filterText and not match(word, prefix) then
return item.filterText return item.filterText
else else

View File

@@ -193,13 +193,23 @@ describe('vim.lsp.completion: item conversion', function()
label = 'printf', label = 'printf',
kind = 3, kind = 3,
detail = 'int', detail = 'int',
sortText = '1',
labelDetails = { detail = '(const char *restrict, ...)', description = 'stdio.h' }, labelDetails = { detail = '(const char *restrict, ...)', description = 'stdio.h' },
}, },
{
label = ' flush',
kind = 2,
insertText = 'flush()',
insertTextFormat = 2,
filterText = 'flush',
sortText = '2',
labelDetails = { detail = '()' },
},
} }
local result = complete('|', completion_list) local result = complete('|', completion_list)
local item = result.items[1] eq('printf(const char *restrict, ...)', result.items[1].abbr)
eq('printf(const char *restrict, ...)', item.abbr) eq('stdio.h', result.items[1].menu)
eq('stdio.h', item.menu) eq('flush', result.items[2].word)
end) end)
---@param prefix string ---@param prefix string