1
1
use std:: fmt:: Write ;
2
2
3
- use hir:: { AdtDef , Ty , db:: HirDatabase , source_binder:: function_from_child_node, StructField } ;
3
+ use hir:: { AdtDef , Ty , db:: HirDatabase , source_binder:: function_from_child_node} ;
4
4
5
5
use ra_syntax:: ast:: { self , AstNode } ;
6
6
@@ -26,7 +26,7 @@ pub(crate) fn fill_struct_fields(mut ctx: AssistCtx<impl HirDatabase>) -> Option
26
26
struct FillStructFields < ' a , ' b : ' a , DB > {
27
27
ctx : & ' a mut AssistCtx < ' b , DB > ,
28
28
named_field_list : & ' a ast:: NamedFieldList ,
29
- struct_fields : Vec < StructField > ,
29
+ struct_fields : Vec < ( String , String ) > ,
30
30
struct_lit : & ' a ast:: StructLit ,
31
31
}
32
32
@@ -64,35 +64,29 @@ where
64
64
Ty :: Adt { def_id : AdtDef :: Struct ( s) , .. } => s,
65
65
_ => return None ,
66
66
} ;
67
- self . struct_fields = struct_def. fields ( self . ctx . db ) ;
67
+ self . struct_fields = struct_def
68
+ . fields ( self . ctx . db )
69
+ . into_iter ( )
70
+ . map ( |f| ( f. name ( self . ctx . db ) . to_string ( ) , "()" . into ( ) ) )
71
+ . collect ( ) ;
68
72
Some ( ( ) )
69
73
}
70
74
71
75
fn remove_already_included_fields ( & mut self ) -> Option < ( ) > {
72
76
for ast_field in self . named_field_list . fields ( ) {
77
+ let expr = ast_field. expr ( ) ?. syntax ( ) . text ( ) . to_string ( ) ;
73
78
let name_from_ast = ast_field. name_ref ( ) ?. text ( ) . to_string ( ) ;
74
- if let Some ( idx) = self
75
- . struct_fields
76
- . iter ( )
77
- . map ( |f| f. name ( self . ctx . db ) . to_string ( ) )
78
- . position ( |n| n == name_from_ast)
79
- {
80
- self . struct_fields . remove ( idx) ;
79
+ if let Some ( idx) = self . struct_fields . iter ( ) . position ( |( n, _) | n == & name_from_ast) {
80
+ self . struct_fields [ idx] = ( name_from_ast, expr) ;
81
81
}
82
82
}
83
83
Some ( ( ) )
84
84
}
85
85
86
- fn struct_fields_string ( & self ) -> Option < String > {
86
+ fn struct_fields_string ( & mut self ) -> Option < String > {
87
87
let mut buf = String :: from ( "{\n " ) ;
88
- for field in self . named_field_list . fields ( ) {
89
- let expr = field. expr ( ) ?. syntax ( ) . text ( ) . to_string ( ) ;
90
- let field_name = field. name_ref ( ) ?. syntax ( ) . text ( ) . to_string ( ) ;
91
- write ! ( & mut buf, " {}: {},\n " , field_name, expr) . unwrap ( ) ;
92
- }
93
- for field in & self . struct_fields {
94
- let field_name = field. name ( self . ctx . db ) . to_string ( ) ;
95
- write ! ( & mut buf, " {}: (),\n " , field_name) . unwrap ( ) ;
88
+ for ( name, expr) in & self . struct_fields {
89
+ write ! ( & mut buf, " {}: {},\n " , name, expr) . unwrap ( ) ;
96
90
}
97
91
buf. push_str ( "}" ) ;
98
92
Some ( buf)
@@ -233,11 +227,11 @@ mod tests {
233
227
234
228
fn main() {
235
229
let s = <|>S {
236
- c: (1, 2),
237
- e: "foo",
238
230
a: (),
239
231
b: (),
232
+ c: (1, 2),
240
233
d: (),
234
+ e: "foo",
241
235
}
242
236
}
243
237
"# ,
0 commit comments