@@ -17,12 +17,11 @@ use crate::{
17
17
impl Completions {
18
18
pub ( crate ) fn add_field ( & mut self , ctx : & CompletionContext , field : hir:: Field , ty : & Type ) {
19
19
let is_deprecated = is_deprecated ( field, ctx. db ) ;
20
- let ty = ty. display ( ctx. db ) . to_string ( ) ;
21
20
let name = field. name ( ctx. db ) ;
22
21
let mut completion_item =
23
22
CompletionItem :: new ( CompletionKind :: Reference , ctx. source_range ( ) , name. to_string ( ) )
24
23
. kind ( CompletionItemKind :: Field )
25
- . detail ( ty. clone ( ) )
24
+ . detail ( ty. display ( ctx . db ) . to_string ( ) )
26
25
. set_documentation ( field. docs ( ctx. db ) )
27
26
. set_deprecated ( is_deprecated) ;
28
27
@@ -107,6 +106,12 @@ impl Completions {
107
106
}
108
107
} ;
109
108
109
+ if let ScopeDef :: Local ( local) = resolution {
110
+ if let Some ( score) = compute_score ( ctx, & local. ty ( ctx. db ) , & local_name) {
111
+ completion_item = completion_item. set_score ( score) ;
112
+ }
113
+ }
114
+
110
115
// Add `<>` for generic types
111
116
if ctx. is_path_type && !ctx. has_type_args && ctx. config . add_call_parenthesis {
112
117
if let Some ( cap) = ctx. config . snippet_cap {
@@ -319,10 +324,11 @@ impl Completions {
319
324
320
325
pub ( crate ) fn compute_score (
321
326
ctx : & CompletionContext ,
322
- // FIXME: this definitely should be a `Type`
323
- ty : & str ,
327
+ ty : & Type ,
324
328
name : & str ,
325
329
) -> Option < CompletionScore > {
330
+ // FIXME: this should not fall back to string equality.
331
+ let ty = & ty. display ( ctx. db ) . to_string ( ) ;
326
332
let ( active_name, active_type) = if let Some ( record_field) = & ctx. record_field_syntax {
327
333
tested_by ! ( test_struct_field_completion_in_record_lit) ;
328
334
let ( struct_field, _local) = ctx. sema . resolve_record_field ( record_field) ?;
@@ -1405,4 +1411,48 @@ mod tests {
1405
1411
"###
1406
1412
) ;
1407
1413
}
1414
+
1415
+ #[ test]
1416
+ fn prioritize_exact_ref_match ( ) {
1417
+ assert_debug_snapshot ! (
1418
+ do_reference_completion(
1419
+ r"
1420
+ struct WorldSnapshot { _f: () };
1421
+ fn go(world: &WorldSnapshot) {
1422
+ go(w<|>)
1423
+ }
1424
+ " ,
1425
+ ) ,
1426
+ @r###"
1427
+ [
1428
+ CompletionItem {
1429
+ label: "WorldSnapshot",
1430
+ source_range: 132..133,
1431
+ delete: 132..133,
1432
+ insert: "WorldSnapshot",
1433
+ kind: Struct,
1434
+ },
1435
+ CompletionItem {
1436
+ label: "go(…)",
1437
+ source_range: 132..133,
1438
+ delete: 132..133,
1439
+ insert: "go(${1:world})$0",
1440
+ kind: Function,
1441
+ lookup: "go",
1442
+ detail: "fn go(world: &WorldSnapshot)",
1443
+ trigger_call_info: true,
1444
+ },
1445
+ CompletionItem {
1446
+ label: "world",
1447
+ source_range: 132..133,
1448
+ delete: 132..133,
1449
+ insert: "world",
1450
+ kind: Binding,
1451
+ detail: "&WorldSnapshot",
1452
+ score: TypeAndNameMatch,
1453
+ },
1454
+ ]
1455
+ "###
1456
+ ) ;
1457
+ }
1408
1458
}
0 commit comments