diff --git a/runtime/lua/vim/lsp/completion.lua b/runtime/lua/vim/lsp/completion.lua index 4cce188798..7795f42645 100644 --- a/runtime/lua/vim/lsp/completion.lua +++ b/runtime/lua/vim/lsp/completion.lua @@ -195,7 +195,11 @@ local function get_completion_word(item, prefix, match) -- -- Typing `i` would remove the candidate because newText starts with `t`. 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 return item.filterText else diff --git a/test/functional/plugin/lsp/completion_spec.lua b/test/functional/plugin/lsp/completion_spec.lua index a713b47b93..81380e31a0 100644 --- a/test/functional/plugin/lsp/completion_spec.lua +++ b/test/functional/plugin/lsp/completion_spec.lua @@ -193,13 +193,23 @@ describe('vim.lsp.completion: item conversion', function() label = 'printf', kind = 3, detail = 'int', + sortText = '1', 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 item = result.items[1] - eq('printf(const char *restrict, ...)', item.abbr) - eq('stdio.h', item.menu) + eq('printf(const char *restrict, ...)', result.items[1].abbr) + eq('stdio.h', result.items[1].menu) + eq('flush', result.items[2].word) end) ---@param prefix string