@@ -187,7 +187,6 @@ pub enum CompletionRelevancePostfixMatch {
187
187
}
188
188
189
189
impl CompletionRelevance {
190
- const BASE_LINE : u32 = 3 ;
191
190
/// Provides a relevance score. Higher values are more relevant.
192
191
///
193
192
/// The absolute value of the relevance score is not meaningful, for
@@ -197,35 +196,42 @@ impl CompletionRelevance {
197
196
///
198
197
/// See is_relevant if you need to make some judgement about score
199
198
/// in an absolute sense.
200
- pub fn score ( & self ) -> u32 {
201
- let mut score = Self :: BASE_LINE ;
202
-
203
- // score decreases
204
- if self . is_op_method {
205
- score -= 1 ;
206
- }
207
- if self . is_private_editable {
208
- score -= 1 ;
199
+ pub fn score ( self ) -> u32 {
200
+ let mut score = 0 ;
201
+ let CompletionRelevance {
202
+ exact_name_match,
203
+ type_match,
204
+ is_local,
205
+ is_op_method,
206
+ is_private_editable,
207
+ postfix_match,
208
+ } = self ;
209
+
210
+ // lower rank private things
211
+ if !is_private_editable {
212
+ score += 1 ;
209
213
}
210
- if self . postfix_match . is_some ( ) {
211
- score -= 3 ;
214
+ // lower rank trait op methods
215
+ if !is_op_method {
216
+ score += 10 ;
212
217
}
213
-
214
- // score increases
215
- if self . exact_name_match {
216
- score += 1 ;
218
+ if exact_name_match {
219
+ score += 10 ;
217
220
}
218
- score += match self . type_match {
219
- Some ( CompletionRelevanceTypeMatch :: Exact ) => 4 ,
221
+ score += match postfix_match {
222
+ Some ( CompletionRelevancePostfixMatch :: Exact ) => 100 ,
223
+ Some ( CompletionRelevancePostfixMatch :: NonExact ) => 0 ,
224
+ None => 3 ,
225
+ } ;
226
+ score += match type_match {
227
+ Some ( CompletionRelevanceTypeMatch :: Exact ) => 8 ,
220
228
Some ( CompletionRelevanceTypeMatch :: CouldUnify ) => 3 ,
221
229
None => 0 ,
222
230
} ;
223
- if self . is_local {
231
+ // slightly prefer locals
232
+ if is_local {
224
233
score += 1 ;
225
234
}
226
- if self . postfix_match == Some ( CompletionRelevancePostfixMatch :: Exact ) {
227
- score += 100 ;
228
- }
229
235
230
236
score
231
237
}
@@ -234,7 +240,7 @@ impl CompletionRelevance {
234
240
/// some threshold such that we think it is especially likely
235
241
/// to be relevant.
236
242
pub fn is_relevant ( & self ) -> bool {
237
- self . score ( ) > Self :: BASE_LINE
243
+ self . score ( ) > 0
238
244
}
239
245
}
240
246
@@ -584,55 +590,34 @@ mod tests {
584
590
585
591
#[ test]
586
592
fn relevance_score ( ) {
593
+ use CompletionRelevance as Cr ;
594
+ let default = Cr :: default ( ) ;
587
595
// This test asserts that the relevance score for these items is ascending, and
588
596
// that any items in the same vec have the same score.
589
597
let expected_relevance_order = vec ! [
590
- vec![ CompletionRelevance {
591
- postfix_match: Some ( CompletionRelevancePostfixMatch :: NonExact ) ,
592
- ..CompletionRelevance :: default ( )
593
- } ] ,
594
- vec![ CompletionRelevance {
595
- is_op_method: true ,
596
- is_private_editable: true ,
597
- ..CompletionRelevance :: default ( )
598
- } ] ,
599
- vec![
600
- CompletionRelevance { is_private_editable: true , ..CompletionRelevance :: default ( ) } ,
601
- CompletionRelevance { is_op_method: true , ..CompletionRelevance :: default ( ) } ,
602
- ] ,
603
- vec![ CompletionRelevance :: default ( ) ] ,
604
- vec![
605
- CompletionRelevance { exact_name_match: true , ..CompletionRelevance :: default ( ) } ,
606
- CompletionRelevance { is_local: true , ..CompletionRelevance :: default ( ) } ,
607
- ] ,
608
- vec![ CompletionRelevance {
609
- exact_name_match: true ,
610
- is_local: true ,
611
- ..CompletionRelevance :: default ( )
612
- } ] ,
613
- vec![ CompletionRelevance {
614
- type_match: Some ( CompletionRelevanceTypeMatch :: CouldUnify ) ,
615
- ..CompletionRelevance :: default ( )
616
- } ] ,
617
- vec![ CompletionRelevance {
618
- type_match: Some ( CompletionRelevanceTypeMatch :: Exact ) ,
619
- ..CompletionRelevance :: default ( )
620
- } ] ,
621
- vec![ CompletionRelevance {
598
+ vec![ ] ,
599
+ vec![ Cr { is_op_method: true , is_private_editable: true , ..default } ] ,
600
+ vec![ Cr { is_op_method: true , ..default } ] ,
601
+ vec![ Cr { postfix_match: Some ( CompletionRelevancePostfixMatch :: NonExact ) , ..default } ] ,
602
+ vec![ Cr { is_private_editable: true , ..default } ] ,
603
+ vec![ default ] ,
604
+ vec![ Cr { is_local: true , ..default } ] ,
605
+ vec![ Cr { type_match: Some ( CompletionRelevanceTypeMatch :: CouldUnify ) , ..default } ] ,
606
+ vec![ Cr { type_match: Some ( CompletionRelevanceTypeMatch :: Exact ) , ..default } ] ,
607
+ vec![ Cr { exact_name_match: true , ..default } ] ,
608
+ vec![ Cr { exact_name_match: true , is_local: true , ..default } ] ,
609
+ vec![ Cr {
622
610
exact_name_match: true ,
623
611
type_match: Some ( CompletionRelevanceTypeMatch :: Exact ) ,
624
- ..CompletionRelevance :: default ( )
612
+ ..default
625
613
} ] ,
626
- vec![ CompletionRelevance {
614
+ vec![ Cr {
627
615
exact_name_match: true ,
628
616
type_match: Some ( CompletionRelevanceTypeMatch :: Exact ) ,
629
617
is_local: true ,
630
- ..CompletionRelevance :: default ( )
631
- } ] ,
632
- vec![ CompletionRelevance {
633
- postfix_match: Some ( CompletionRelevancePostfixMatch :: Exact ) ,
634
- ..CompletionRelevance :: default ( )
618
+ ..default
635
619
} ] ,
620
+ vec![ Cr { postfix_match: Some ( CompletionRelevancePostfixMatch :: Exact ) , ..default } ] ,
636
621
] ;
637
622
638
623
check_relevance_score_ordered ( expected_relevance_order) ;
0 commit comments