@@ -159,16 +159,24 @@ pub(crate) fn extract_struct_from_enum_variant(
159159 & field_list,
160160 generic_params. clone ( ) ,
161161 & enum_ast,
162- comments_for_struct,
163162 ) ;
164163
165164 let enum_ast = variant. parent_enum ( ) ;
166165 let indent = enum_ast. indent_level ( ) ;
167166 let def = def. indent ( indent) ;
168167
169- editor. insert_all (
168+ let mut insert_items: Vec < SyntaxElement > = Vec :: new ( ) ;
169+ for attr in enum_ast. attrs ( ) {
170+ insert_items. push ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
171+ insert_items. push ( make. whitespace ( "\n " ) . into ( ) ) ;
172+ }
173+ insert_items. extend ( comments_for_struct) ;
174+ insert_items. push ( def. syntax ( ) . clone ( ) . into ( ) ) ;
175+ insert_items. push ( make. whitespace ( & format ! ( "\n \n {indent}" ) ) . into ( ) ) ;
176+ editor. insert_all_with_whitespace (
170177 Position :: before ( enum_ast. syntax ( ) ) ,
171- vec ! [ def. syntax( ) . clone( ) . into( ) , make. whitespace( & format!( "\n \n {indent}" ) ) . into( ) ] ,
178+ insert_items,
179+ & make,
172180 ) ;
173181
174182 update_variant ( & make, & mut editor, & variant, generic_params) ;
@@ -288,67 +296,47 @@ fn create_struct_def(
288296 field_list : & Either < ast:: RecordFieldList , ast:: TupleFieldList > ,
289297 generics : Option < ast:: GenericParamList > ,
290298 enum_ : & ast:: Enum ,
291- comments : Vec < SyntaxElement > ,
292299) -> ast:: Struct {
293300 let enum_vis = enum_. visibility ( ) ;
294301
295302 // for fields without any existing visibility, use visibility of enum
296303 let field_list: ast:: FieldList = match field_list {
297304 Either :: Left ( field_list) => {
298305 if let Some ( vis) = & enum_vis {
299- let ( mut fl_editor, new_fl) = SyntaxEditor :: with_ast_node ( field_list) ;
300- for field in new_fl. fields ( ) {
306+ let new_fields = field_list. fields ( ) . map ( |field| {
301307 if field. visibility ( ) . is_none ( )
302- && let Some ( field_name) = field. name ( )
308+ && let Some ( name) = field. name ( )
309+ && let Some ( ty) = field. ty ( )
303310 {
304- fl_editor. insert_all (
305- Position :: before ( field_name. syntax ( ) ) ,
306- vec ! [ vis. syntax( ) . clone( ) . into( ) , make. whitespace( " " ) . into( ) ] ,
307- ) ;
311+ make. record_field ( Some ( vis. clone ( ) ) , name, ty)
312+ } else {
313+ field
308314 }
309- }
310- let new_fl = fl_editor. finish ( ) . new_root ( ) . clone ( ) ;
311- ast:: RecordFieldList :: cast ( new_fl) . unwrap ( ) . into ( )
315+ } ) ;
316+ make. record_field_list ( new_fields) . into ( )
312317 } else {
313318 field_list. clone ( ) . into ( )
314319 }
315320 }
316321 Either :: Right ( field_list) => {
317322 if let Some ( vis) = & enum_vis {
318- let ( mut fl_editor, new_fl) = SyntaxEditor :: with_ast_node ( field_list) ;
319- for field in new_fl. fields ( ) {
323+ let new_fields = field_list. fields ( ) . map ( |field| {
320324 if field. visibility ( ) . is_none ( )
321325 && let Some ( ty) = field. ty ( )
322326 {
323- fl_editor. insert_all (
324- Position :: before ( ty. syntax ( ) ) ,
325- vec ! [ vis. syntax( ) . clone( ) . into( ) , make. whitespace( " " ) . into( ) ] ,
326- ) ;
327+ make. tuple_field ( Some ( vis. clone ( ) ) , ty)
328+ } else {
329+ field
327330 }
328- }
329- let new_fl = fl_editor. finish ( ) . new_root ( ) . clone ( ) ;
330- ast:: TupleFieldList :: cast ( new_fl) . unwrap ( ) . into ( )
331+ } ) ;
332+ make. tuple_field_list ( new_fields) . into ( )
331333 } else {
332334 field_list. clone ( ) . into ( )
333335 }
334336 }
335337 } ;
336338
337- let strukt = make. struct_ ( enum_vis, name, generics, field_list) ;
338- let mut items_to_prepend: Vec < SyntaxElement > = Vec :: new ( ) ;
339- for attr in enum_. attrs ( ) {
340- items_to_prepend. push ( attr. syntax ( ) . clone ( ) . into ( ) ) ;
341- items_to_prepend. push ( make. whitespace ( "\n " ) . into ( ) ) ;
342- }
343- items_to_prepend. extend ( comments) ;
344-
345- if !items_to_prepend. is_empty ( ) {
346- let ( mut strukt_editor, strukt_root) = SyntaxEditor :: with_ast_node ( & strukt) ;
347- strukt_editor. insert_all ( Position :: first_child_of ( strukt_root. syntax ( ) ) , items_to_prepend) ;
348- ast:: Struct :: cast ( strukt_editor. finish ( ) . new_root ( ) . clone ( ) ) . unwrap ( )
349- } else {
350- strukt
351- }
339+ make. struct_ ( enum_vis, name, generics, field_list)
352340}
353341
354342fn update_variant (
0 commit comments