@@ -20,8 +20,8 @@ use ide_db::{
2020use syntax:: TextRange ;
2121
2222use crate :: {
23- item:: ImportEdit , CompletionContext , CompletionItem , CompletionItemKind , CompletionKind ,
24- CompletionScore ,
23+ item:: { ImportEdit , Relevance } ,
24+ CompletionContext , CompletionItem , CompletionItemKind , CompletionKind ,
2525} ;
2626
2727use crate :: render:: { enum_variant:: render_variant, function:: render_fn, macro_:: render_macro} ;
@@ -117,7 +117,7 @@ impl<'a> RenderContext<'a> {
117117 node. docs ( self . db ( ) )
118118 }
119119
120- fn active_name_and_type ( & self ) -> Option < ( String , Type ) > {
120+ fn expected_name_and_type ( & self ) -> Option < ( String , Type ) > {
121121 if let Some ( record_field) = & self . completion . record_field_syntax {
122122 cov_mark:: hit!( record_field_type_match) ;
123123 let ( struct_field, _local) = self . completion . sema . resolve_record_field ( record_field) ?;
@@ -155,8 +155,8 @@ impl<'a> Render<'a> {
155155 . set_documentation ( field. docs ( self . ctx . db ( ) ) )
156156 . set_deprecated ( is_deprecated) ;
157157
158- if let Some ( score ) = compute_score ( & self . ctx , & ty, & name. to_string ( ) ) {
159- item = item. set_score ( score ) ;
158+ if let Some ( relevance ) = compute_relevance ( & self . ctx , & ty, & name. to_string ( ) ) {
159+ item = item. set_relevance ( relevance ) ;
160160 }
161161
162162 item. build ( )
@@ -247,18 +247,15 @@ impl<'a> Render<'a> {
247247 } ;
248248
249249 if let ScopeDef :: Local ( local) = resolution {
250- if let Some ( ( active_name, active_type) ) = self . ctx . active_name_and_type ( ) {
251- let ty = local. ty ( self . ctx . db ( ) ) ;
252- if let Some ( score) =
253- compute_score_from_active ( & active_type, & active_name, & ty, & local_name)
254- {
255- item = item. set_score ( score) ;
256- }
257-
258- if let Some ( ty_without_ref) = active_type. remove_ref ( ) {
250+ let ty = local. ty ( self . ctx . db ( ) ) ;
251+ if let Some ( relevance) = compute_relevance ( & self . ctx , & ty, & local_name) {
252+ item = item. set_relevance ( relevance)
253+ }
254+ if let Some ( ( _expected_name, expected_type) ) = self . ctx . expected_name_and_type ( ) {
255+ if let Some ( ty_without_ref) = expected_type. remove_ref ( ) {
259256 if ty_without_ref == ty {
260257 cov_mark:: hit!( suggest_ref) ;
261- let mutability = if active_type . is_mutable_reference ( ) {
258+ let mutability = if expected_type . is_mutable_reference ( ) {
262259 Mutability :: Mut
263260 } else {
264261 Mutability :: Shared
@@ -326,33 +323,14 @@ impl<'a> Render<'a> {
326323 }
327324}
328325
329- fn compute_score_from_active (
330- active_type : & Type ,
331- active_name : & str ,
332- ty : & Type ,
333- name : & str ,
334- ) -> Option < CompletionScore > {
335- // Compute score
336- // For the same type
337- if active_type != ty {
338- return None ;
339- }
340-
341- let mut res = CompletionScore :: TypeMatch ;
342-
343- // If same type + same name then go top position
344- if active_name == name {
345- res = CompletionScore :: TypeAndNameMatch
346- }
347-
326+ fn compute_relevance ( ctx : & RenderContext , ty : & Type , name : & str ) -> Option < Relevance > {
327+ let ( expected_name, expected_type) = ctx. expected_name_and_type ( ) ?;
328+ let mut res = Relevance :: default ( ) ;
329+ res. exact_type_match = ty == & expected_type;
330+ res. exact_name_match = name == & expected_name;
348331 Some ( res)
349332}
350333
351- fn compute_score ( ctx : & RenderContext , ty : & Type , name : & str ) -> Option < CompletionScore > {
352- let ( active_name, active_type) = ctx. active_name_and_type ( ) ?;
353- compute_score_from_active ( & active_type, & active_name, ty, name)
354- }
355-
356334#[ cfg( test) ]
357335mod tests {
358336 use std:: cmp:: Reverse ;
@@ -361,32 +339,33 @@ mod tests {
361339
362340 use crate :: {
363341 test_utils:: { check_edit, do_completion, get_all_items, TEST_CONFIG } ,
364- CompletionKind , CompletionScore ,
342+ CompletionKind , Relevance ,
365343 } ;
366344
367345 fn check ( ra_fixture : & str , expect : Expect ) {
368346 let actual = do_completion ( ra_fixture, CompletionKind :: Reference ) ;
369347 expect. assert_debug_eq ( & actual) ;
370348 }
371349
372- fn check_scores ( ra_fixture : & str , expect : Expect ) {
373- fn display_score ( score : Option < CompletionScore > ) -> & ' static str {
374- match score {
375- Some ( CompletionScore :: TypeMatch ) => "[type]" ,
376- Some ( CompletionScore :: TypeAndNameMatch ) => "[type+name]" ,
377- None => "[]" . into ( ) ,
350+ fn check_relevance ( ra_fixture : & str , expect : Expect ) {
351+ fn display_relevance ( relevance : Relevance ) -> & ' static str {
352+ match relevance {
353+ Relevance { exact_type_match : true , exact_name_match : true } => "[type+name]" ,
354+ Relevance { exact_type_match : true , exact_name_match : false } => "[type]" ,
355+ Relevance { exact_type_match : false , exact_name_match : true } => "[name]" ,
356+ Relevance { exact_type_match : false , exact_name_match : false } => "[]" ,
378357 }
379358 }
380359
381360 let mut completions = get_all_items ( TEST_CONFIG , ra_fixture) ;
382- completions. sort_by_key ( |it| ( Reverse ( it. score ( ) ) , it. label ( ) . to_string ( ) ) ) ;
361+ completions. sort_by_key ( |it| ( Reverse ( it. relevance ( ) ) , it. label ( ) . to_string ( ) ) ) ;
383362 let actual = completions
384363 . into_iter ( )
385364 . filter ( |it| it. completion_kind == CompletionKind :: Reference )
386365 . map ( |it| {
387366 let tag = it. kind ( ) . unwrap ( ) . tag ( ) ;
388- let score = display_score ( it. score ( ) ) ;
389- format ! ( "{} {} {}\n " , tag, it. label( ) , score )
367+ let relevance = display_relevance ( it. relevance ( ) ) ;
368+ format ! ( "{} {} {}\n " , tag, it. label( ) , relevance )
390369 } )
391370 . collect :: < String > ( ) ;
392371 expect. assert_eq ( & actual) ;
@@ -849,9 +828,9 @@ fn foo(xs: Vec<i128>)
849828 }
850829
851830 #[ test]
852- fn active_param_score ( ) {
831+ fn active_param_relevance ( ) {
853832 cov_mark:: check!( active_param_type_match) ;
854- check_scores (
833+ check_relevance (
855834 r#"
856835struct S { foo: i64, bar: u32, baz: u32 }
857836fn test(bar: u32) { }
@@ -866,9 +845,9 @@ fn foo(s: S) { test(s.$0) }
866845 }
867846
868847 #[ test]
869- fn record_field_scores ( ) {
848+ fn record_field_relevances ( ) {
870849 cov_mark:: check!( record_field_type_match) ;
871- check_scores (
850+ check_relevance (
872851 r#"
873852struct A { foo: i64, bar: u32, baz: u32 }
874853struct B { x: (), y: f32, bar: u32 }
@@ -883,8 +862,8 @@ fn foo(a: A) { B { bar: a.$0 }; }
883862 }
884863
885864 #[ test]
886- fn record_field_and_call_scores ( ) {
887- check_scores (
865+ fn record_field_and_call_relevances ( ) {
866+ check_relevance (
888867 r#"
889868struct A { foo: i64, bar: u32, baz: u32 }
890869struct B { x: (), y: f32, bar: u32 }
@@ -897,7 +876,7 @@ fn foo(a: A) { B { bar: f(a.$0) }; }
897876 fd baz []
898877 "# ] ] ,
899878 ) ;
900- check_scores (
879+ check_relevance (
901880 r#"
902881struct A { foo: i64, bar: u32, baz: u32 }
903882struct B { x: (), y: f32, bar: u32 }
@@ -914,7 +893,7 @@ fn foo(a: A) { f(B { bar: a.$0 }); }
914893
915894 #[ test]
916895 fn prioritize_exact_ref_match ( ) {
917- check_scores (
896+ check_relevance (
918897 r#"
919898struct WorldSnapshot { _f: () };
920899fn go(world: &WorldSnapshot) { go(w$0) }
@@ -929,7 +908,7 @@ fn go(world: &WorldSnapshot) { go(w$0) }
929908
930909 #[ test]
931910 fn too_many_arguments ( ) {
932- check_scores (
911+ check_relevance (
933912 r#"
934913struct Foo;
935914fn f(foo: &Foo) { f(foo, w$0) }
@@ -997,6 +976,10 @@ fn main() {
997976 Local,
998977 ),
999978 detail: "S",
979+ relevance: Relevance {
980+ exact_name_match: true,
981+ exact_type_match: false,
982+ },
1000983 ref_match: "&mut ",
1001984 },
1002985 ]
0 commit comments