@@ -15,50 +15,50 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
15
15
) => {
16
16
let ty = ctx. sema . type_of_expr ( & Expr :: RecordExpr ( record_expr. clone ( ) ) ) ;
17
17
18
- let default_trait = ctx. famous_defs ( ) . core_default_Default ( ) ;
19
- let impl_default_trait =
20
- default_trait. zip ( ty. as_ref ( ) ) . map_or ( false , |( default_trait, ty) | {
21
- ty. original . impls_trait ( ctx. db , default_trait, & [ ] )
22
- } ) ;
23
-
24
- let missing_fields = match ty. and_then ( |t| t. adjusted ( ) . as_adt ( ) ) {
25
- Some ( hir:: Adt :: Union ( un) ) => {
26
- // ctx.sema.record_literal_missing_fields will always return
27
- // an empty Vec on a union literal. This is normally
28
- // reasonable, but here we'd like to present the full list
29
- // of fields if the literal is empty.
30
- let were_fields_specified = record_expr
31
- . record_expr_field_list ( )
32
- . and_then ( |fl| fl. fields ( ) . next ( ) )
33
- . is_some ( ) ;
34
-
35
- match were_fields_specified {
36
- false => un. fields ( ctx. db ) . into_iter ( ) . map ( |f| ( f, f. ty ( ctx. db ) ) ) . collect ( ) ,
37
- true => vec ! [ ] ,
38
- }
18
+ if let Some ( hir:: Adt :: Union ( un) ) = ty. as_ref ( ) . and_then ( |t| t. original . as_adt ( ) ) {
19
+ // ctx.sema.record_literal_missing_fields will always return
20
+ // an empty Vec on a union literal. This is normally
21
+ // reasonable, but here we'd like to present the full list
22
+ // of fields if the literal is empty.
23
+ let were_fields_specified = record_expr
24
+ . record_expr_field_list ( )
25
+ . and_then ( |fl| fl. fields ( ) . next ( ) )
26
+ . is_some ( ) ;
27
+
28
+ match were_fields_specified {
29
+ false => un. fields ( ctx. db ) . into_iter ( ) . map ( |f| ( f, f. ty ( ctx. db ) ) ) . collect ( ) ,
30
+ true => vec ! [ ] ,
39
31
}
40
- _ => ctx. sema . record_literal_missing_fields ( record_expr) ,
41
- } ;
42
- if impl_default_trait && !missing_fields. is_empty ( ) && ctx. path_qual ( ) . is_none ( ) {
43
- let completion_text = "..Default::default()" ;
44
- let mut item =
45
- CompletionItem :: new ( SymbolKind :: Field , ctx. source_range ( ) , completion_text) ;
46
- let completion_text =
47
- completion_text. strip_prefix ( ctx. token . text ( ) ) . unwrap_or ( completion_text) ;
48
- item. insert_text ( completion_text) . set_relevance ( CompletionRelevance {
49
- exact_postfix_snippet_match : true ,
50
- ..Default :: default ( )
51
- } ) ;
52
- item. add_to ( acc) ;
53
- }
54
- if ctx. previous_token_is ( T ! [ . ] ) {
55
- let mut item =
56
- CompletionItem :: new ( CompletionItemKind :: Snippet , ctx. source_range ( ) , ".." ) ;
57
- item. insert_text ( "." ) ;
58
- item. add_to ( acc) ;
59
- return None ;
32
+ } else {
33
+ let missing_fields = ctx. sema . record_literal_missing_fields ( record_expr) ;
34
+
35
+ let default_trait = ctx. famous_defs ( ) . core_default_Default ( ) ;
36
+ let impl_default_trait =
37
+ default_trait. zip ( ty. as_ref ( ) ) . map_or ( false , |( default_trait, ty) | {
38
+ ty. original . impls_trait ( ctx. db , default_trait, & [ ] )
39
+ } ) ;
40
+
41
+ if impl_default_trait && !missing_fields. is_empty ( ) && ctx. path_qual ( ) . is_none ( ) {
42
+ let completion_text = "..Default::default()" ;
43
+ let mut item =
44
+ CompletionItem :: new ( SymbolKind :: Field , ctx. source_range ( ) , completion_text) ;
45
+ let completion_text =
46
+ completion_text. strip_prefix ( ctx. token . text ( ) ) . unwrap_or ( completion_text) ;
47
+ item. insert_text ( completion_text) . set_relevance ( CompletionRelevance {
48
+ exact_postfix_snippet_match : true ,
49
+ ..Default :: default ( )
50
+ } ) ;
51
+ item. add_to ( acc) ;
52
+ }
53
+ if ctx. previous_token_is ( T ! [ . ] ) {
54
+ let mut item =
55
+ CompletionItem :: new ( CompletionItemKind :: Snippet , ctx. source_range ( ) , ".." ) ;
56
+ item. insert_text ( "." ) ;
57
+ item. add_to ( acc) ;
58
+ return None ;
59
+ }
60
+ missing_fields
60
61
}
61
- missing_fields
62
62
}
63
63
Some ( ImmediateLocation :: RecordPat ( record_pat) ) => {
64
64
ctx. sema . record_pattern_missing_fields ( record_pat)
@@ -82,22 +82,17 @@ pub(crate) fn complete_record_literal(
82
82
}
83
83
84
84
match ctx. expected_type . as_ref ( ) ?. as_adt ( ) ? {
85
- hir:: Adt :: Struct ( strukt) => {
86
- if ctx. path_qual ( ) . is_none ( ) {
87
- let module =
88
- if let Some ( module) = ctx. module { module } else { strukt. module ( ctx. db ) } ;
89
- let path = module. find_use_path ( ctx. db , hir:: ModuleDef :: from ( strukt) ) ;
85
+ hir:: Adt :: Struct ( strukt) if ctx. path_qual ( ) . is_none ( ) => {
86
+ let module = if let Some ( module) = ctx. module { module } else { strukt. module ( ctx. db ) } ;
87
+ let path = module. find_use_path ( ctx. db , hir:: ModuleDef :: from ( strukt) ) ;
90
88
91
- acc. add_struct_literal ( ctx, strukt, path, None ) ;
92
- }
89
+ acc. add_struct_literal ( ctx, strukt, path, None ) ;
93
90
}
94
- hir:: Adt :: Union ( un) => {
95
- if ctx. path_qual ( ) . is_none ( ) {
96
- let module = if let Some ( module) = ctx. module { module } else { un. module ( ctx. db ) } ;
97
- let path = module. find_use_path ( ctx. db , hir:: ModuleDef :: from ( un) ) ;
91
+ hir:: Adt :: Union ( un) if ctx. path_qual ( ) . is_none ( ) => {
92
+ let module = if let Some ( module) = ctx. module { module } else { un. module ( ctx. db ) } ;
93
+ let path = module. find_use_path ( ctx. db , hir:: ModuleDef :: from ( un) ) ;
98
94
99
- acc. add_union_literal ( ctx, un, path, None ) ;
100
- }
95
+ acc. add_union_literal ( ctx, un, path, None ) ;
101
96
}
102
97
_ => { }
103
98
} ;
0 commit comments