Skip to content

Commit f66d7d8

Browse files
committed
Also remove trailing bare lines in comments
Block comments with bare lines are handled differently than those without bare lines (unless `normalize_comments` is configured). If `normalize_comments` is false but `wrap_comments` is true, trailing blank lines should be removed from block comments, including those with bare lines. Signed-off-by: Ross Williams <ross@ross-williams.net>
1 parent c7504a2 commit f66d7d8

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

src/comment.rs

+50-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::rewrite::RewriteContext;
1010
use crate::shape::{Indent, Shape};
1111
use crate::string::{rewrite_string, StringFormat};
1212
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,
1515
};
1616
use crate::{ErrorKind, FormattingError};
1717

@@ -346,7 +346,14 @@ fn identify_comment(
346346
let (first_group, rest) = orig.split_at(first_group_ending);
347347
let rewritten_first_group =
348348
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+
}
350357
} else if !config.normalize_comments()
351358
&& !config.wrap_comments()
352359
&& !(
@@ -1053,6 +1060,46 @@ fn trim_end_unless_two_whitespaces(s: &str, is_doc_comment: bool) -> &str {
10531060
}
10541061
}
10551062

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+
}
10561103
/// Trims whitespace and aligns to indent, but otherwise does not change comments.
10571104
fn light_rewrite_comment(
10581105
orig: &str,

src/utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ pub(crate) fn is_empty_line(s: &str) -> bool {
675675
s.is_empty() || s.chars().all(char::is_whitespace)
676676
}
677677

678+
pub(crate) fn is_horizontal_space(c: char) -> bool {
679+
c == ' ' || c == '\t'
680+
}
681+
678682
fn get_prefix_space_width(config: &Config, s: &str) -> usize {
679683
let mut width = 0;
680684
for c in s.chars() {

tests/source/comment_trailing_lines.rs

+7
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ pub mod bar {}
1919
*
2020
*/
2121
pub mod block {}
22+
23+
/**
24+
Outer Block doc comment with multiple trailing lines
25+
26+
27+
*/
28+
pub mod outer_block_doc_comment {}

tests/target/comment_trailing_lines.rs

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ pub mod bar {}
1212
* Block comment with multiple trailing lines
1313
*/
1414
pub mod block {}
15+
16+
/**
17+
Outer Block doc comment with multiple trailing lines
18+
*/
19+
pub mod outer_block_doc_comment {}

0 commit comments

Comments
 (0)