Skip to content

Commit 0a9f14d

Browse files
Fix incorrect whitespace modifications to non-rust code blocks
* Fix whitespace was being prepended to leading blank line * Fix trailing blank line was being stripped Fixes: #6609 Fixes: #6015
1 parent 4b620cd commit 0a9f14d

9 files changed

Lines changed: 85 additions & 8 deletions

src/comment.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,9 @@ impl<'a> CommentRewrite<'a> {
664664
if !self.code_block_buffer.is_empty() {
665665
// There is a code block that is not properly enclosed by backticks.
666666
// We will leave them untouched.
667-
self.result.push_str(&self.comment_line_separator);
668-
self.result.push_str(&Self::join_block(
669-
&trim_custom_comment_prefix(&self.code_block_buffer),
670-
&self.comment_line_separator,
671-
));
667+
self.result.push_str(
668+
&self.finish_code_block(&trim_custom_comment_prefix(&self.code_block_buffer)),
669+
);
672670
}
673671

674672
if let Some(ref ib) = self.item_block {
@@ -707,6 +705,23 @@ impl<'a> CommentRewrite<'a> {
707705
self.result
708706
}
709707

708+
fn finish_code_block(&self, code_block: &str) -> String {
709+
let result = match code_block.lines().next() {
710+
None | Some("") => self.comment_line_separator.trim_end().to_string(),
711+
_ => self.comment_line_separator.clone(),
712+
};
713+
714+
result
715+
+ &Self::join_block(
716+
// preserve trailing newline:
717+
// block is non-empty, so it must end with "\n",
718+
// and join_block calls: str.lines().collect().join("\n"),
719+
// which will strip a trailing newline
720+
&format!("{code_block}\n"),
721+
&self.comment_line_separator,
722+
)
723+
}
724+
710725
fn handle_line(
711726
&mut self,
712727
orig: &'a str,
@@ -775,9 +790,7 @@ impl<'a> CommentRewrite<'a> {
775790
_ => trim_custom_comment_prefix(&self.code_block_buffer),
776791
};
777792
if !code_block.is_empty() {
778-
self.result.push_str(&self.comment_line_separator);
779-
self.result
780-
.push_str(&Self::join_block(&code_block, &self.comment_line_separator));
793+
self.result.push_str(&self.finish_code_block(&code_block));
781794
}
782795
self.code_block_buffer.clear();
783796
self.result.push_str(&self.comment_line_separator);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```text
4+
///
5+
/// This is a non-rust code block
6+
/// ```
7+
struct S;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```text
4+
///
5+
/// This is a non-rust code block
6+
struct S;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Some comment.
4+
///
5+
/// ```text
6+
/// This is a non-rust code block
7+
///
8+
///
9+
/// ```
10+
struct S;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Some comment.
4+
///
5+
/// ```text
6+
/// This is a non-rust code block
7+
///
8+
///
9+
struct S;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```text
4+
///
5+
/// This is a non-rust code block
6+
/// ```
7+
struct S;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```text
4+
///
5+
/// This is a non-rust code block
6+
struct S;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Some comment.
4+
///
5+
/// ```text
6+
/// This is a non-rust code block
7+
///
8+
///
9+
/// ```
10+
struct S;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Some comment.
4+
///
5+
/// ```text
6+
/// This is a non-rust code block
7+
///
8+
///
9+
struct S;

0 commit comments

Comments
 (0)