mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
Cleanup relevance scoring
This commit is contained in:
parent
a9ae0b0855
commit
28251e486c
1 changed files with 47 additions and 62 deletions
|
@ -187,7 +187,6 @@ pub enum CompletionRelevancePostfixMatch {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompletionRelevance {
|
impl CompletionRelevance {
|
||||||
const BASE_LINE: u32 = 3;
|
|
||||||
/// Provides a relevance score. Higher values are more relevant.
|
/// Provides a relevance score. Higher values are more relevant.
|
||||||
///
|
///
|
||||||
/// The absolute value of the relevance score is not meaningful, for
|
/// The absolute value of the relevance score is not meaningful, for
|
||||||
|
@ -197,35 +196,42 @@ impl CompletionRelevance {
|
||||||
///
|
///
|
||||||
/// See is_relevant if you need to make some judgement about score
|
/// See is_relevant if you need to make some judgement about score
|
||||||
/// in an absolute sense.
|
/// in an absolute sense.
|
||||||
pub fn score(&self) -> u32 {
|
pub fn score(self) -> u32 {
|
||||||
let mut score = Self::BASE_LINE;
|
let mut score = 0;
|
||||||
|
let CompletionRelevance {
|
||||||
|
exact_name_match,
|
||||||
|
type_match,
|
||||||
|
is_local,
|
||||||
|
is_op_method,
|
||||||
|
is_private_editable,
|
||||||
|
postfix_match,
|
||||||
|
} = self;
|
||||||
|
|
||||||
// score decreases
|
// lower rank private things
|
||||||
if self.is_op_method {
|
if !is_private_editable {
|
||||||
score -= 1;
|
|
||||||
}
|
|
||||||
if self.is_private_editable {
|
|
||||||
score -= 1;
|
|
||||||
}
|
|
||||||
if self.postfix_match.is_some() {
|
|
||||||
score -= 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
// score increases
|
|
||||||
if self.exact_name_match {
|
|
||||||
score += 1;
|
score += 1;
|
||||||
}
|
}
|
||||||
score += match self.type_match {
|
// lower rank trait op methods
|
||||||
Some(CompletionRelevanceTypeMatch::Exact) => 4,
|
if !is_op_method {
|
||||||
|
score += 10;
|
||||||
|
}
|
||||||
|
if exact_name_match {
|
||||||
|
score += 10;
|
||||||
|
}
|
||||||
|
score += match postfix_match {
|
||||||
|
Some(CompletionRelevancePostfixMatch::Exact) => 100,
|
||||||
|
Some(CompletionRelevancePostfixMatch::NonExact) => 0,
|
||||||
|
None => 3,
|
||||||
|
};
|
||||||
|
score += match type_match {
|
||||||
|
Some(CompletionRelevanceTypeMatch::Exact) => 8,
|
||||||
Some(CompletionRelevanceTypeMatch::CouldUnify) => 3,
|
Some(CompletionRelevanceTypeMatch::CouldUnify) => 3,
|
||||||
None => 0,
|
None => 0,
|
||||||
};
|
};
|
||||||
if self.is_local {
|
// slightly prefer locals
|
||||||
|
if is_local {
|
||||||
score += 1;
|
score += 1;
|
||||||
}
|
}
|
||||||
if self.postfix_match == Some(CompletionRelevancePostfixMatch::Exact) {
|
|
||||||
score += 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
score
|
score
|
||||||
}
|
}
|
||||||
|
@ -234,7 +240,7 @@ impl CompletionRelevance {
|
||||||
/// some threshold such that we think it is especially likely
|
/// some threshold such that we think it is especially likely
|
||||||
/// to be relevant.
|
/// to be relevant.
|
||||||
pub fn is_relevant(&self) -> bool {
|
pub fn is_relevant(&self) -> bool {
|
||||||
self.score() > Self::BASE_LINE
|
self.score() > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -584,55 +590,34 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn relevance_score() {
|
fn relevance_score() {
|
||||||
|
use CompletionRelevance as Cr;
|
||||||
|
let default = Cr::default();
|
||||||
// This test asserts that the relevance score for these items is ascending, and
|
// This test asserts that the relevance score for these items is ascending, and
|
||||||
// that any items in the same vec have the same score.
|
// that any items in the same vec have the same score.
|
||||||
let expected_relevance_order = vec![
|
let expected_relevance_order = vec![
|
||||||
vec![CompletionRelevance {
|
vec![],
|
||||||
postfix_match: Some(CompletionRelevancePostfixMatch::NonExact),
|
vec![Cr { is_op_method: true, is_private_editable: true, ..default }],
|
||||||
..CompletionRelevance::default()
|
vec![Cr { is_op_method: true, ..default }],
|
||||||
}],
|
vec![Cr { postfix_match: Some(CompletionRelevancePostfixMatch::NonExact), ..default }],
|
||||||
vec![CompletionRelevance {
|
vec![Cr { is_private_editable: true, ..default }],
|
||||||
is_op_method: true,
|
vec![default],
|
||||||
is_private_editable: true,
|
vec![Cr { is_local: true, ..default }],
|
||||||
..CompletionRelevance::default()
|
vec![Cr { type_match: Some(CompletionRelevanceTypeMatch::CouldUnify), ..default }],
|
||||||
}],
|
vec![Cr { type_match: Some(CompletionRelevanceTypeMatch::Exact), ..default }],
|
||||||
vec![
|
vec![Cr { exact_name_match: true, ..default }],
|
||||||
CompletionRelevance { is_private_editable: true, ..CompletionRelevance::default() },
|
vec![Cr { exact_name_match: true, is_local: true, ..default }],
|
||||||
CompletionRelevance { is_op_method: true, ..CompletionRelevance::default() },
|
vec![Cr {
|
||||||
],
|
|
||||||
vec![CompletionRelevance::default()],
|
|
||||||
vec![
|
|
||||||
CompletionRelevance { exact_name_match: true, ..CompletionRelevance::default() },
|
|
||||||
CompletionRelevance { is_local: true, ..CompletionRelevance::default() },
|
|
||||||
],
|
|
||||||
vec![CompletionRelevance {
|
|
||||||
exact_name_match: true,
|
|
||||||
is_local: true,
|
|
||||||
..CompletionRelevance::default()
|
|
||||||
}],
|
|
||||||
vec![CompletionRelevance {
|
|
||||||
type_match: Some(CompletionRelevanceTypeMatch::CouldUnify),
|
|
||||||
..CompletionRelevance::default()
|
|
||||||
}],
|
|
||||||
vec![CompletionRelevance {
|
|
||||||
type_match: Some(CompletionRelevanceTypeMatch::Exact),
|
|
||||||
..CompletionRelevance::default()
|
|
||||||
}],
|
|
||||||
vec![CompletionRelevance {
|
|
||||||
exact_name_match: true,
|
exact_name_match: true,
|
||||||
type_match: Some(CompletionRelevanceTypeMatch::Exact),
|
type_match: Some(CompletionRelevanceTypeMatch::Exact),
|
||||||
..CompletionRelevance::default()
|
..default
|
||||||
}],
|
}],
|
||||||
vec![CompletionRelevance {
|
vec![Cr {
|
||||||
exact_name_match: true,
|
exact_name_match: true,
|
||||||
type_match: Some(CompletionRelevanceTypeMatch::Exact),
|
type_match: Some(CompletionRelevanceTypeMatch::Exact),
|
||||||
is_local: true,
|
is_local: true,
|
||||||
..CompletionRelevance::default()
|
..default
|
||||||
}],
|
|
||||||
vec![CompletionRelevance {
|
|
||||||
postfix_match: Some(CompletionRelevancePostfixMatch::Exact),
|
|
||||||
..CompletionRelevance::default()
|
|
||||||
}],
|
}],
|
||||||
|
vec![Cr { postfix_match: Some(CompletionRelevancePostfixMatch::Exact), ..default }],
|
||||||
];
|
];
|
||||||
|
|
||||||
check_relevance_score_ordered(expected_relevance_order);
|
check_relevance_score_ordered(expected_relevance_order);
|
||||||
|
|
Loading…
Reference in a new issue