Lower postfix suggestions in completions list

This commit is contained in:
Aleksei Trifonov 2022-03-31 02:27:33 +03:00
parent 259182b50b
commit 0d1f4f9b27
3 changed files with 20 additions and 7 deletions

View File

@ -240,12 +240,12 @@ fn build<'ctx>(
let mut item =
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), label);
item.detail(detail).snippet_edit(cap, edit);
if ctx.original_token.text() == label {
let relevance =
CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() };
item.set_relevance(relevance);
}
let relevance = if ctx.original_token.text() == label {
CompletionRelevance { exact_postfix_snippet_match: true, ..Default::default() }
} else {
CompletionRelevance { is_postfix: true, ..Default::default() }
};
item.set_relevance(relevance);
item
}
}

View File

@ -152,6 +152,8 @@ pub struct CompletionRelevance {
/// Basically, we want to guarantee that postfix snippets always takes
/// precedence over everything else.
pub exact_postfix_snippet_match: bool,
/// Set in cases when item is postfix, but not exact
pub is_postfix: bool,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@ -179,7 +181,7 @@ pub enum CompletionRelevanceTypeMatch {
}
impl CompletionRelevance {
const BASE_LINE: u32 = 2;
const BASE_LINE: u32 = 3;
/// Provides a relevance score. Higher values are more relevant.
///
/// The absolute value of the relevance score is not meaningful, for
@ -199,6 +201,9 @@ pub fn score(&self) -> u32 {
if self.is_private_editable {
score -= 1;
}
if self.is_postfix {
score -= 3;
}
// score increases
if self.exact_name_match {
@ -215,6 +220,7 @@ pub fn score(&self) -> u32 {
if self.exact_postfix_snippet_match {
score += 100;
}
score
}
@ -573,6 +579,10 @@ fn relevance_score() {
// This test asserts that the relevance score for these items is ascending, and
// that any items in the same vec have the same score.
let expected_relevance_order = vec![
vec![CompletionRelevance {
is_postfix: true,
..CompletionRelevance::default()
}],
vec![CompletionRelevance {
is_op_method: true,
is_private_editable: true,

View File

@ -615,6 +615,7 @@ fn main() { let _: m::Spam = S$0 }
is_op_method: false,
is_private_editable: false,
exact_postfix_snippet_match: false,
is_postfix: false,
},
},
CompletionItem {
@ -636,6 +637,7 @@ fn main() { let _: m::Spam = S$0 }
is_op_method: false,
is_private_editable: false,
exact_postfix_snippet_match: false,
is_postfix: false,
},
},
]
@ -723,6 +725,7 @@ fn foo() { A { the$0 } }
is_op_method: false,
is_private_editable: false,
exact_postfix_snippet_match: false,
is_postfix: false,
},
},
]