Skip to content

Commit bd51e8e

Browse files
authored
Do not add trailing space to empty lines in comment code blocks (#4304)
Currently, the first line pushed in a code block is a comment line delimiter that includes a trailing space, even if the content of that line is empty. Later lines are handled correctly because their content is checked before adding the comment line delimiter. So, just apply the same logic as is done for later lines to the first line. Closes #4251
1 parent c3e6be7 commit bd51e8e

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

Diff for: src/formatting/comment.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,22 @@ impl<'a> CommentRewrite<'a> {
576576
cr
577577
}
578578

579-
fn join_block(s: &str, sep: &str) -> String {
579+
fn join_block(s: &str, sep: &str, add_sep_prefix: bool) -> String {
580580
let mut result = String::with_capacity(s.len() + 128);
581581
let mut iter = s.lines().peekable();
582+
let get_sep = |line: Option<&&str>| -> &str {
583+
match line {
584+
Some(&"") => sep.trim_end(),
585+
Some(..) => sep,
586+
None => "",
587+
}
588+
};
589+
if add_sep_prefix {
590+
result.push_str(get_sep(iter.peek()));
591+
}
582592
while let Some(line) = iter.next() {
583593
result.push_str(line);
584-
result.push_str(match iter.peek() {
585-
Some(next_line) if next_line.is_empty() => sep.trim_end(),
586-
Some(..) => &sep,
587-
None => "",
588-
});
594+
result.push_str(get_sep(iter.peek()));
589595
}
590596
result
591597
}
@@ -594,10 +600,10 @@ impl<'a> CommentRewrite<'a> {
594600
if !self.code_block_buffer.is_empty() {
595601
// There is a code block that is not properly enclosed by backticks.
596602
// We will leave them untouched.
597-
self.result.push_str(&self.comment_line_separator);
598603
self.result.push_str(&Self::join_block(
599604
&trim_custom_comment_prefix(&self.code_block_buffer),
600605
&self.comment_line_separator,
606+
true, /* add_sep_prefix */
601607
));
602608
}
603609

@@ -615,10 +621,12 @@ impl<'a> CommentRewrite<'a> {
615621
Some(s) => self.result.push_str(&Self::join_block(
616622
&s,
617623
&format!("{}{}", self.comment_line_separator, ib.line_start),
624+
false, /* add_sep_prefix */
618625
)),
619626
None => self.result.push_str(&Self::join_block(
620627
&ib.original_block_as_string(),
621628
&self.comment_line_separator,
629+
false, /* add_sep_prefix */
622630
)),
623631
};
624632
}
@@ -658,10 +666,12 @@ impl<'a> CommentRewrite<'a> {
658666
Some(s) => self.result.push_str(&Self::join_block(
659667
&s,
660668
&format!("{}{}", self.comment_line_separator, ib.line_start),
669+
false, /* add_sep_prefix */
661670
)),
662671
None => self.result.push_str(&Self::join_block(
663672
&ib.original_block_as_string(),
664673
&self.comment_line_separator,
674+
false, /* add_sep_prefix */
665675
)),
666676
};
667677
} else if self.code_block_attr.is_some() {
@@ -689,9 +699,11 @@ impl<'a> CommentRewrite<'a> {
689699
}
690700
};
691701
if !code_block.is_empty() {
692-
self.result.push_str(&self.comment_line_separator);
693-
self.result
694-
.push_str(&Self::join_block(&code_block, &self.comment_line_separator));
702+
self.result.push_str(&Self::join_block(
703+
&code_block,
704+
&self.comment_line_separator,
705+
true, /* add_sep_prefix */
706+
));
695707
}
696708
self.code_block_buffer.clear();
697709
self.result.push_str(&self.comment_line_separator);

Diff for: tests/source/issue-4251.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-wrap_comments: true
2+
3+
//! ```
4+
//!
5+
//! use something;
6+
//! ```

Diff for: tests/target/issue-4251.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-wrap_comments: true
2+
3+
//! ```
4+
//!
5+
//! use something;
6+
//! ```

0 commit comments

Comments
 (0)