@@ -36,6 +36,8 @@ pub struct ListFormatting<'a> {
36
36
preserve_newline : bool ,
37
37
// Nested import lists get some special handling for the "Mixed" list type
38
38
nested : bool ,
39
+ // Whether comments should be visually aligned.
40
+ align_comments : bool ,
39
41
config : & ' a Config ,
40
42
}
41
43
@@ -50,6 +52,7 @@ impl<'a> ListFormatting<'a> {
50
52
ends_with_newline : true ,
51
53
preserve_newline : false ,
52
54
nested : false ,
55
+ align_comments : true ,
53
56
config,
54
57
}
55
58
}
@@ -89,6 +92,11 @@ impl<'a> ListFormatting<'a> {
89
92
self
90
93
}
91
94
95
+ pub fn align_comments ( mut self , align_comments : bool ) -> Self {
96
+ self . align_comments = align_comments;
97
+ self
98
+ }
99
+
92
100
pub fn needs_trailing_separator ( & self ) -> bool {
93
101
match self . trailing_separator {
94
102
// We always put separator in front.
@@ -465,23 +473,31 @@ where
465
473
let mut formatted_comment = rewrite_post_comment ( & mut item_max_width) ?;
466
474
467
475
if !starts_with_newline ( comment) {
468
- let mut comment_alignment =
469
- post_comment_alignment ( item_max_width, inner_item. len ( ) ) ;
470
- if first_line_width ( & formatted_comment)
471
- + last_line_width ( & result)
472
- + comment_alignment
473
- + 1
474
- > formatting. config . max_width ( )
475
- {
476
- item_max_width = None ;
477
- formatted_comment = rewrite_post_comment ( & mut item_max_width) ?;
478
- comment_alignment = post_comment_alignment ( item_max_width, inner_item. len ( ) ) ;
479
- }
480
- for _ in 0 ..=comment_alignment {
481
- result. push ( ' ' ) ;
476
+ if formatting. align_comments {
477
+ let mut comment_alignment =
478
+ post_comment_alignment ( item_max_width, inner_item. len ( ) ) ;
479
+ if first_line_width ( & formatted_comment)
480
+ + last_line_width ( & result)
481
+ + comment_alignment
482
+ + 1
483
+ > formatting. config . max_width ( )
484
+ {
485
+ item_max_width = None ;
486
+ formatted_comment = rewrite_post_comment ( & mut item_max_width) ?;
487
+ comment_alignment =
488
+ post_comment_alignment ( item_max_width, inner_item. len ( ) ) ;
489
+ }
490
+ for _ in 0 ..=comment_alignment {
491
+ result. push ( ' ' ) ;
492
+ }
482
493
}
483
- // An additional space for the missing trailing separator.
484
- if last && item_max_width. is_some ( ) && !separate && !formatting. separator . is_empty ( )
494
+ // An additional space for the missing trailing separator (or
495
+ // if we skipped alignment above).
496
+ if !formatting. align_comments
497
+ || ( last
498
+ && item_max_width. is_some ( )
499
+ && !separate
500
+ && !formatting. separator . is_empty ( ) )
485
501
{
486
502
result. push ( ' ' ) ;
487
503
}
@@ -902,6 +918,7 @@ pub fn struct_lit_formatting<'a>(
902
918
ends_with_newline,
903
919
preserve_newline : true ,
904
920
nested : false ,
921
+ align_comments : true ,
905
922
config : context. config ,
906
923
}
907
924
}
0 commit comments