@@ -105,7 +105,8 @@ pub(crate) fn extract_struct_from_enum_variant(
105
105
. generic_param_list ( )
106
106
. and_then ( |known_generics| extract_generic_params ( & known_generics, & field_list) ) ;
107
107
let generics = generic_params. as_ref ( ) . map ( |generics| generics. clone_for_update ( ) ) ;
108
- let def = create_struct_def ( variant_name. clone ( ) , & field_list, generics, & enum_ast) ;
108
+ let def =
109
+ create_struct_def ( variant_name. clone ( ) , & variant, & field_list, generics, & enum_ast) ;
109
110
110
111
let enum_ast = variant. parent_enum ( ) ;
111
112
let indent = enum_ast. indent_level ( ) ;
@@ -228,6 +229,7 @@ fn tag_generics_in_variant(ty: &ast::Type, generics: &mut [(ast::GenericParam, b
228
229
229
230
fn create_struct_def (
230
231
name : ast:: Name ,
232
+ variant : & ast:: Variant ,
231
233
field_list : & Either < ast:: RecordFieldList , ast:: TupleFieldList > ,
232
234
generics : Option < ast:: GenericParamList > ,
233
235
enum_ : & ast:: Enum ,
@@ -272,6 +274,12 @@ fn create_struct_def(
272
274
273
275
let strukt = make:: struct_ ( enum_vis, name, generics, field_list) . clone_for_update ( ) ;
274
276
277
+ // take comments from variant
278
+ ted:: insert_all (
279
+ ted:: Position :: first_child_of ( strukt. syntax ( ) ) ,
280
+ take_all_comments ( variant. syntax ( ) ) ,
281
+ ) ;
282
+
275
283
// copy attributes from enum
276
284
ted:: insert_all (
277
285
ted:: Position :: first_child_of ( strukt. syntax ( ) ) ,
@@ -340,6 +348,31 @@ fn update_variant(variant: &ast::Variant, generics: Option<ast::GenericParamList
340
348
Some ( ( ) )
341
349
}
342
350
351
+ // Note: this also detaches whitespace after comments,
352
+ // since `SyntaxNode::splice_children` (and by extension `ted::insert_all_raw`)
353
+ // detaches nodes. If we only took the comments, we'd leave behind the old whitespace.
354
+ fn take_all_comments ( node : & SyntaxNode ) -> Vec < SyntaxElement > {
355
+ let mut remove_next_ws = false ;
356
+ node. children_with_tokens ( )
357
+ . filter_map ( move |child| match child. kind ( ) {
358
+ COMMENT => {
359
+ remove_next_ws = true ;
360
+ child. detach ( ) ;
361
+ Some ( child)
362
+ }
363
+ WHITESPACE if remove_next_ws => {
364
+ remove_next_ws = false ;
365
+ child. detach ( ) ;
366
+ Some ( make:: tokens:: single_newline ( ) . into ( ) )
367
+ }
368
+ _ => {
369
+ remove_next_ws = false ;
370
+ None
371
+ }
372
+ } )
373
+ . collect ( )
374
+ }
375
+
343
376
fn apply_references (
344
377
insert_use_cfg : InsertUseConfig ,
345
378
segment : ast:: PathSegment ,
@@ -602,7 +635,7 @@ enum A { One(One) }"#,
602
635
}
603
636
604
637
#[ test]
605
- fn test_extract_struct_keep_comments_and_attrs_on_variant_struct ( ) {
638
+ fn test_extract_struct_move_struct_variant_comments ( ) {
606
639
check_assist (
607
640
extract_struct_from_enum_variant,
608
641
r#"
@@ -616,22 +649,22 @@ enum A {
616
649
}
617
650
}"# ,
618
651
r#"
652
+ /* comment */
653
+ // other
654
+ /// comment
619
655
struct One{
620
656
a: u32
621
657
}
622
658
623
659
enum A {
624
- /* comment */
625
- // other
626
- /// comment
627
660
#[attr]
628
661
One(One)
629
662
}"# ,
630
663
) ;
631
664
}
632
665
633
666
#[ test]
634
- fn test_extract_struct_keep_comments_and_attrs_on_variant_tuple ( ) {
667
+ fn test_extract_struct_move_tuple_variant_comments ( ) {
635
668
check_assist (
636
669
extract_struct_from_enum_variant,
637
670
r#"
@@ -643,12 +676,12 @@ enum A {
643
676
$0One(u32, u32)
644
677
}"# ,
645
678
r#"
679
+ /* comment */
680
+ // other
681
+ /// comment
646
682
struct One(u32, u32);
647
683
648
684
enum A {
649
- /* comment */
650
- // other
651
- /// comment
652
685
#[attr]
653
686
One(One)
654
687
}"# ,
0 commit comments