11//! Completes references after dot (fields and method calls).
22
3- use either:: Either ;
43use rustc_hash:: FxHashSet ;
54
65use crate :: { context:: CompletionContext , patterns:: ImmediateLocation , Completions } ;
@@ -20,10 +19,13 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
2019 if matches ! ( ctx. completion_location, Some ( ImmediateLocation :: MethodCall { .. } ) ) {
2120 cov_mark:: hit!( test_no_struct_field_completion_for_method_call) ;
2221 } 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+ ) ;
2729 }
2830 complete_methods ( ctx, & receiver_ty, |func| acc. add_method ( ctx, func, None , None ) ) ;
2931}
@@ -38,14 +40,13 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
3840 if let Some ( func) = ctx. function_def . as_ref ( ) . and_then ( |fn_| ctx. sema . to_def ( fn_) ) {
3941 if let Some ( self_) = func. self_param ( ctx. db ) {
4042 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+ ) ;
4950 complete_methods ( ctx, & ty, |func| {
5051 acc. add_method ( ctx, func, Some ( hir:: known:: SELF_PARAM ) , None )
5152 } ) ;
@@ -54,17 +55,19 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
5455}
5556
5657fn complete_fields (
58+ acc : & mut Completions ,
5759 ctx : & CompletionContext ,
5860 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 ) ,
6063) {
6164 for receiver in receiver. autoderef ( ctx. db ) {
6265 for ( field, ty) in receiver. fields ( ctx. db ) {
63- f ( Either :: Left ( field) , ty) ;
66+ named_field ( acc , field, ty) ;
6467 }
6568 for ( i, ty) in receiver. tuple_fields ( ctx. db ) . into_iter ( ) . enumerate ( ) {
6669 // Tuple fields are always public (tuple struct fields are handled above).
67- f ( Either :: Right ( i ) , ty) ;
70+ tuple_index ( acc , i , ty) ;
6871 }
6972 }
7073}
0 commit comments