commit 0df2c6b5d09fab392dd1a14e4b2e6a3b03203aaa
parent 025c87441502cf570bad7b71f40bc6fe88989297
Author: Mathias Fussenegger <f.mathias@zignar.net>
Date: Tue, 28 May 2024 21:37:46 +0200
feat(lsp): use fuzzy match on filterText instead of prefix match
The `complete()` mechanism matches completion candidates against
the typed text, so strict pre-filtering isn't necessary.
This is a first step towards supporting postfix snippets (like
`items@insert` in luals)
Diffstat:
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/runtime/lua/vim/lsp/completion.lua b/runtime/lua/vim/lsp/completion.lua
@@ -201,11 +201,17 @@ function M._lsp_to_complete_items(result, prefix, client_id)
return {}
end
- local function matches_prefix(item)
- return vim.startswith(get_completion_word(item), prefix)
- end
+ if prefix ~= '' then
+ ---@param item lsp.CompletionItem
+ local function match_prefix(item)
+ if item.filterText then
+ return next(vim.fn.matchfuzzy({ item.filterText }, prefix))
+ end
+ return true
+ end
- items = vim.tbl_filter(matches_prefix, items) --[[@as lsp.CompletionItem[]|]]
+ items = vim.tbl_filter(match_prefix, items) --[[@as lsp.CompletionItem[]|]]
+ end
table.sort(items, function(a, b)
return (a.sortText or a.label) < (b.sortText or b.label)
end)
diff --git a/test/functional/plugin/lsp/completion_spec.lua b/test/functional/plugin/lsp/completion_spec.lua
@@ -228,7 +228,7 @@ describe('vim.lsp.completion: item conversion', function()
},
},
{
- filterText = 'notthis_thread',
+ filterText = 'no_match',
insertText = 'notthis_thread',
insertTextFormat = 1,
kind = 9,