add completion relevance score

This commit is contained in:
Josh Mcguigan
2021-03-11 19:11:14 -08:00
parent c0e9530fd0
commit 3679821eea
5 changed files with 82 additions and 37 deletions

View File

@@ -6,9 +6,10 @@ use std::{
use ide::{
Annotation, AnnotationKind, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind,
Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlPunct,
HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat, Markup, NavigationTarget,
ReferenceAccess, RenameError, Runnable, Severity, SourceChange, TextEdit, TextRange, TextSize,
CompletionRelevance, Documentation, FileId, FileRange, FileSystemEdit, Fold, FoldKind,
Highlight, HlMod, HlPunct, HlRange, HlTag, Indel, InlayHint, InlayKind, InsertTextFormat,
Markup, NavigationTarget, ReferenceAccess, RenameError, Runnable, Severity, SourceChange,
TextEdit, TextRange, TextSize,
};
use ide_db::SymbolKind;
use itertools::Itertools;
@@ -213,12 +214,22 @@ pub(crate) fn completion_item(
..Default::default()
};
if item.relevance().is_relevant() {
lsp_item.preselect = Some(true);
// HACK: sort preselect items first
lsp_item.sort_text = Some(format!(" {}", item.label()));
fn set_score(res: &mut lsp_types::CompletionItem, relevance: CompletionRelevance) {
if relevance.is_relevant() {
res.preselect = Some(true);
}
// The relevance needs to be inverted to come up with a sort score
// because the client will sort ascending.
let sort_score = relevance.score() ^ 0xFF;
// Zero pad the string to ensure values are sorted numerically
// even though the client is sorting alphabetically. Three
// characters is enough to fit the largest u8, which is the
// type of the relevance score.
res.sort_text = Some(format!("{:03}", sort_score));
}
set_score(&mut lsp_item, item.relevance());
if item.deprecated() {
lsp_item.tags = Some(vec![lsp_types::CompletionItemTag::Deprecated])
}
@@ -228,10 +239,9 @@ pub(crate) fn completion_item(
}
let mut res = match item.ref_match() {
Some(mutability) => {
Some((mutability, relevance)) => {
let mut lsp_item_with_ref = lsp_item.clone();
lsp_item.preselect = Some(true);
lsp_item.sort_text = Some(format!(" {}", item.label()));
set_score(&mut lsp_item_with_ref, relevance);
lsp_item_with_ref.label =
format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label);
if let Some(lsp_types::CompletionTextEdit::Edit(it)) = &mut lsp_item_with_ref.text_edit
@@ -1107,13 +1117,13 @@ mod tests {
(
"&arg",
Some(
" arg",
"253",
),
),
(
"arg",
Some(
" arg",
"254",
),
),
]