Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,9 @@ impl<'a> CommentRewrite<'a> {
if !self.code_block_buffer.is_empty() {
// There is a code block that is not properly enclosed by backticks.
// We will leave them untouched.
self.result.push_str(&self.comment_line_separator);
self.result.push_str(&Self::join_block(
&trim_custom_comment_prefix(&self.code_block_buffer),
&self.comment_line_separator,
));
self.result.push_str(
&self.finish_code_block(&trim_custom_comment_prefix(&self.code_block_buffer)),
);
}

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

fn finish_code_block(&self, code_block: &str) -> String {
let result = match code_block.lines().next().unwrap() {
"" => self.comment_line_separator.trim_end().to_string(),
_ => self.comment_line_separator.clone(),
};

result
+ &Self::join_block(
// preserve trailing newline:
// block is non-empty, so it must end with "\n",
// and join_block calls: str.lines().collect().join("\n"),
// which will strip a trailing newline
&format!("{code_block}\n"),
&self.comment_line_separator,
)
}

fn handle_line(
&mut self,
orig: &'a str,
Expand Down Expand Up @@ -775,9 +790,7 @@ impl<'a> CommentRewrite<'a> {
_ => trim_custom_comment_prefix(&self.code_block_buffer),
};
if !code_block.is_empty() {
self.result.push_str(&self.comment_line_separator);
self.result
.push_str(&Self::join_block(&code_block, &self.comment_line_separator));
self.result.push_str(&self.finish_code_block(&code_block));
}
self.code_block_buffer.clear();
self.result.push_str(&self.comment_line_separator);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-format_code_in_doc_comments: true

/// ```text
///
/// This is a non-rust code block
/// ```
struct S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-format_code_in_doc_comments: true

/// ```text
///
/// This is a non-rust code block
struct S;
10 changes: 10 additions & 0 deletions tests/source/issue-6609/complete_code_block_trailing_blank_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-format_code_in_doc_comments: true

/// Some comment.
///
/// ```text
/// This is a non-rust code block
///
///
/// ```
struct S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-format_code_in_doc_comments: true

/// Some comment.
///
/// ```text
/// This is a non-rust code block
///
///
struct S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// rustfmt-format_code_in_doc_comments: true

/// ```text
///
/// This is a non-rust code block
/// ```
struct S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-format_code_in_doc_comments: true

/// ```text
///
/// This is a non-rust code block
struct S;
10 changes: 10 additions & 0 deletions tests/target/issue-6609/complete_code_block_trailing_blank_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-format_code_in_doc_comments: true

/// Some comment.
///
/// ```text
/// This is a non-rust code block
///
///
/// ```
struct S;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// rustfmt-format_code_in_doc_comments: true

/// Some comment.
///
/// ```text
/// This is a non-rust code block
///
///
struct S;
Loading