editor: Use quantize score for code completions sort + Add code completions tests (#29182)

Closes #27994, #29050, #27352, #27616

This PR implements new logic for code completions, which improve cases
where local variables, etc LSP based hints are not shown on top of code
completion menu. The new logic is explained in comment of code.

This new sort is similar to VSCode's completions sort where order of
sort is like:

Fuzzy > Snippet > LSP sort_key > LSP sort_text 

whenever two items have same value, it proceeds to use next one as tie
breaker. Changing fuzzy score from float to int based makes it possible
for two items two have same fuzzy int score, making them get sorted by
next criteria.

Release Notes:

- Improved code completions to prioritize LSP hints, such as local
variables, so they appear at the top of the list.
This commit is contained in:
Smit Barmase 2025-04-23 07:23:34 +05:30 committed by GitHub
parent 6a009b447a
commit 0d3fe474db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1109 additions and 146 deletions

View file

@ -5006,7 +5006,7 @@ impl Completion {
/// A key that can be used to sort completions when displaying
/// them to the user.
pub fn sort_key(&self) -> (usize, &str) {
const DEFAULT_KIND_KEY: usize = 2;
const DEFAULT_KIND_KEY: usize = 3;
let kind_key = self
.source
// `lsp::CompletionListItemDefaults` has no `kind` field
@ -5015,6 +5015,7 @@ impl Completion {
.and_then(|lsp_completion_kind| match lsp_completion_kind {
lsp::CompletionItemKind::KEYWORD => Some(0),
lsp::CompletionItemKind::VARIABLE => Some(1),
lsp::CompletionItemKind::CONSTANT => Some(2),
_ => None,
})
.unwrap_or(DEFAULT_KIND_KEY);