1
1
//! Completes references after dot (fields and method calls).
2
2
3
- use either:: Either ;
4
3
use rustc_hash:: FxHashSet ;
5
4
6
5
use crate :: { context:: CompletionContext , patterns:: ImmediateLocation , Completions } ;
@@ -20,10 +19,13 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
20
19
if matches ! ( ctx. completion_location, Some ( ImmediateLocation :: MethodCall { .. } ) ) {
21
20
cov_mark:: hit!( test_no_struct_field_completion_for_method_call) ;
22
21
} else {
23
- complete_fields ( ctx, & receiver_ty, |field, ty| match field {
24
- Either :: Left ( field) => acc. add_field ( ctx, None , field, & ty) ,
25
- Either :: Right ( tuple_idx) => acc. add_tuple_field ( ctx, None , tuple_idx, & ty) ,
26
- } ) ;
22
+ complete_fields (
23
+ acc,
24
+ ctx,
25
+ & receiver_ty,
26
+ |acc, field, ty| acc. add_field ( ctx, None , field, & ty) ,
27
+ |acc, field, ty| acc. add_tuple_field ( ctx, None , field, & ty) ,
28
+ ) ;
27
29
}
28
30
complete_methods ( ctx, & receiver_ty, |func| acc. add_method ( ctx, func, None , None ) ) ;
29
31
}
@@ -38,14 +40,13 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
38
40
if let Some ( func) = ctx. function_def . as_ref ( ) . and_then ( |fn_| ctx. sema . to_def ( fn_) ) {
39
41
if let Some ( self_) = func. self_param ( ctx. db ) {
40
42
let ty = self_. ty ( ctx. db ) ;
41
- complete_fields ( ctx, & ty, |field, ty| match field {
42
- either:: Either :: Left ( field) => {
43
- acc. add_field ( ctx, Some ( hir:: known:: SELF_PARAM ) , field, & ty)
44
- }
45
- either:: Either :: Right ( tuple_idx) => {
46
- acc. add_tuple_field ( ctx, Some ( hir:: known:: SELF_PARAM ) , tuple_idx, & ty)
47
- }
48
- } ) ;
43
+ complete_fields (
44
+ acc,
45
+ ctx,
46
+ & ty,
47
+ |acc, field, ty| acc. add_field ( ctx, Some ( hir:: known:: SELF_PARAM ) , field, & ty) ,
48
+ |acc, field, ty| acc. add_tuple_field ( ctx, Some ( hir:: known:: SELF_PARAM ) , field, & ty) ,
49
+ ) ;
49
50
complete_methods ( ctx, & ty, |func| {
50
51
acc. add_method ( ctx, func, Some ( hir:: known:: SELF_PARAM ) , None )
51
52
} ) ;
@@ -54,17 +55,19 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
54
55
}
55
56
56
57
fn complete_fields (
58
+ acc : & mut Completions ,
57
59
ctx : & CompletionContext ,
58
60
receiver : & hir:: Type ,
59
- mut f : impl FnMut ( Either < hir:: Field , usize > , hir:: Type ) ,
61
+ mut named_field : impl FnMut ( & mut Completions , hir:: Field , hir:: Type ) ,
62
+ mut tuple_index : impl FnMut ( & mut Completions , usize , hir:: Type ) ,
60
63
) {
61
64
for receiver in receiver. autoderef ( ctx. db ) {
62
65
for ( field, ty) in receiver. fields ( ctx. db ) {
63
- f ( Either :: Left ( field) , ty) ;
66
+ named_field ( acc , field, ty) ;
64
67
}
65
68
for ( i, ty) in receiver. tuple_fields ( ctx. db ) . into_iter ( ) . enumerate ( ) {
66
69
// Tuple fields are always public (tuple struct fields are handled above).
67
- f ( Either :: Right ( i ) , ty) ;
70
+ tuple_index ( acc , i , ty) ;
68
71
}
69
72
}
70
73
}
0 commit comments