@@ -10,8 +10,8 @@ use crate::rewrite::RewriteContext;
10
10
use crate :: shape:: { Indent , Shape } ;
11
11
use crate :: string:: { rewrite_string, StringFormat } ;
12
12
use crate :: utils:: {
13
- count_newlines, first_line_width, last_line_width , trim_left_preserve_layout ,
14
- trimmed_last_line_width, unicode_str_width,
13
+ count_newlines, first_line_width, is_horizontal_space , last_line_width ,
14
+ trim_left_preserve_layout , trimmed_last_line_width, unicode_str_width,
15
15
} ;
16
16
use crate :: { ErrorKind , FormattingError } ;
17
17
@@ -346,7 +346,14 @@ fn identify_comment(
346
346
let ( first_group, rest) = orig. split_at ( first_group_ending) ;
347
347
let rewritten_first_group =
348
348
if !config. normalize_comments ( ) && has_bare_lines && style. is_block_comment ( ) {
349
- trim_left_preserve_layout ( first_group, shape. indent , config) ?
349
+ let reindented = trim_left_preserve_layout ( first_group, shape. indent , config) ?;
350
+
351
+ // If wrap_comments is enabled, then remove any trailing blank lines
352
+ if config. wrap_comments ( ) {
353
+ strip_comment_trailing_lines ( reindented, style)
354
+ } else {
355
+ reindented
356
+ }
350
357
} else if !config. normalize_comments ( )
351
358
&& !config. wrap_comments ( )
352
359
&& !(
@@ -1053,6 +1060,46 @@ fn trim_end_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
1053
1060
}
1054
1061
}
1055
1062
1063
+ /// Removes blank lines at the end of block comments
1064
+ fn strip_comment_trailing_lines ( comment : String , style : CommentStyle < ' _ > ) -> String {
1065
+ debug_assert ! (
1066
+ style. is_block_comment( ) ,
1067
+ "strip_suffix(style.closer()) ensures this function is a no-op on non-block comments"
1068
+ ) ;
1069
+
1070
+ // If final line is a normal closing line, look for trailing blank lines
1071
+ if let Some ( without_closer) = comment
1072
+ . strip_suffix ( style. closer ( ) )
1073
+ . map ( |s| s. trim_end_matches ( is_horizontal_space) )
1074
+ . and_then ( |s| s. strip_suffix ( '\n' ) )
1075
+ {
1076
+ let mut trimmed = without_closer. trim_end ( ) ;
1077
+ let closer_line = & comment[ without_closer. len ( ) ..] ;
1078
+ let line_start = style. line_start ( ) . trim_end ( ) ;
1079
+ // Start trimming trailing blank lines
1080
+ loop {
1081
+ // Remove any asterisk-prefixed or bare block comment lines
1082
+ let t = trimmed. trim_end_matches ( line_start) . trim_end ( ) ;
1083
+ if t. len ( ) == trimmed. len ( ) {
1084
+ // If no trimming occurred
1085
+ break ;
1086
+ } else {
1087
+ // ... otherwise, keep trying to trim
1088
+ trimmed = t;
1089
+ }
1090
+ }
1091
+
1092
+ if without_closer. len ( ) == trimmed. len ( ) {
1093
+ // No blank lines were removed
1094
+ comment
1095
+ } else {
1096
+ trimmed. to_string ( ) + closer_line
1097
+ }
1098
+ } else {
1099
+ // Final line either didn't have a closer or had non-whitespace along with the closer
1100
+ comment
1101
+ }
1102
+ }
1056
1103
/// Trims whitespace and aligns to indent, but otherwise does not change comments.
1057
1104
fn light_rewrite_comment (
1058
1105
orig : & str ,
0 commit comments