@@ -76,9 +76,20 @@ impl AsRef<ListItem> for ListItem {
76
76
}
77
77
}
78
78
79
+ #[ derive( PartialEq , Eq ) ]
80
+ pub enum ListItemCommentStyle {
81
+ // Try to keep the comment on the same line with the item.
82
+ SameLine ,
83
+ // Put the comment on the previous or the next line of the item.
84
+ DifferentLine ,
85
+ // No comment available.
86
+ None ,
87
+ }
88
+
79
89
pub struct ListItem {
80
90
// None for comments mean that they are not present.
81
91
pub pre_comment : Option < String > ,
92
+ pub pre_comment_style : ListItemCommentStyle ,
82
93
// Item should include attributes and doc comments. None indicates a failed
83
94
// rewrite.
84
95
pub item : Option < String > ,
@@ -111,6 +122,7 @@ impl ListItem {
111
122
pub fn from_str < S : Into < String > > ( s : S ) -> ListItem {
112
123
ListItem {
113
124
pre_comment : None ,
125
+ pre_comment_style : ListItemCommentStyle :: None ,
114
126
item : Some ( s. into ( ) ) ,
115
127
post_comment : None ,
116
128
new_lines : false ,
@@ -279,8 +291,23 @@ where
279
291
result. push_str ( & comment) ;
280
292
281
293
if tactic == DefinitiveListTactic :: Vertical {
282
- result. push ( '\n' ) ;
283
- result. push_str ( indent_str) ;
294
+ // We cannot keep pre-comments on the same line if the comment if normalized.
295
+ let keep_comment = if formatting. config . normalize_comments ( ) {
296
+ false
297
+ } else if item. pre_comment_style == ListItemCommentStyle :: DifferentLine {
298
+ false
299
+ } else {
300
+ // We will try to keep the comment on the same line with the item here.
301
+ // 1 = ` `
302
+ let total_width = total_item_width ( item) + item_sep_len + 1 ;
303
+ total_width <= formatting. shape . width
304
+ } ;
305
+ if keep_comment {
306
+ result. push ( ' ' ) ;
307
+ } else {
308
+ result. push ( '\n' ) ;
309
+ result. push_str ( indent_str) ;
310
+ }
284
311
} else {
285
312
result. push ( ' ' ) ;
286
313
}
@@ -448,12 +475,34 @@ where
448
475
. span_to_snippet ( mk_sp ( self . prev_span_end , ( self . get_lo ) ( & item) ) )
449
476
. unwrap ( ) ;
450
477
let trimmed_pre_snippet = pre_snippet. trim ( ) ;
451
- let has_pre_comment =
452
- trimmed_pre_snippet. contains ( "//" ) || trimmed_pre_snippet. contains ( "/*" ) ;
453
- let pre_comment = if has_pre_comment {
454
- Some ( trimmed_pre_snippet. to_owned ( ) )
478
+ let has_single_line_comment = trimmed_pre_snippet. starts_with ( "//" ) ;
479
+ let has_block_comment = trimmed_pre_snippet. starts_with ( "/*" ) ;
480
+ let ( pre_comment, pre_comment_style) = if has_single_line_comment {
481
+ (
482
+ Some ( trimmed_pre_snippet. to_owned ( ) ) ,
483
+ ListItemCommentStyle :: DifferentLine ,
484
+ )
485
+ } else if has_block_comment {
486
+ let comment_end = pre_snippet. chars ( ) . rev ( ) . position ( |c| c == '/' ) . unwrap ( ) ;
487
+ if pre_snippet
488
+ . chars ( )
489
+ . rev ( )
490
+ . take ( comment_end + 1 )
491
+ . find ( |c| * c == '\n' )
492
+ . is_some ( )
493
+ {
494
+ (
495
+ Some ( trimmed_pre_snippet. to_owned ( ) ) ,
496
+ ListItemCommentStyle :: DifferentLine ,
497
+ )
498
+ } else {
499
+ (
500
+ Some ( trimmed_pre_snippet. to_owned ( ) ) ,
501
+ ListItemCommentStyle :: SameLine ,
502
+ )
503
+ }
455
504
} else {
456
- None
505
+ ( None , ListItemCommentStyle :: None )
457
506
} ;
458
507
459
508
// Post-comment
@@ -542,6 +591,7 @@ where
542
591
543
592
ListItem {
544
593
pre_comment : pre_comment,
594
+ pre_comment_style : pre_comment_style,
545
595
item : ( self . get_item_string ) ( & item) ,
546
596
post_comment : post_comment,
547
597
new_lines : new_lines,
0 commit comments