diff --git a/src/comment.rs b/src/comment.rs index f9d8a0fa70c..e89a3afa8d0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -760,7 +760,17 @@ impl<'a> CommentRewrite<'a> { self.code_block_attr = None; self.item_block = None; if let Some(stripped) = line.strip_prefix("```") { - self.code_block_attr = Some(CodeBlockAttribute::new(stripped)) + // The current line is the start of a code fence, and we want to make sure that + // code fences always start on their own line. + if self.buffer_contains_comment() && !self.result.ends_with('\n') { + self.result.push_str(&self.comment_line_separator); + self.result.push_str(line) + } else { + // the comment buffer is empty, so we can just write the line + self.result.push_str(line) + } + self.code_block_attr = Some(CodeBlockAttribute::new(stripped)); + return false; } else if self.fmt.config.wrap_comments() && ItemizedBlock::is_itemized_line(line) { let ib = ItemizedBlock::new(line); self.item_block = Some(ib); diff --git a/tests/source/issue-5244/wrap_comments_true.rs b/tests/source/issue-5244/wrap_comments_true.rs new file mode 100644 index 00000000000..484dc52f980 --- /dev/null +++ b/tests/source/issue-5244/wrap_comments_true.rs @@ -0,0 +1,7 @@ +// rustfmt-wrap_comments:true + +/// Here is me writing some documentation that is too long oh me oh my now some code please! +/// ``` +/// test +/// ``` +fn foo() {} diff --git a/tests/target/issue-5244/wrap_comments_false.rs b/tests/target/issue-5244/wrap_comments_false.rs new file mode 100644 index 00000000000..a98fe803fe9 --- /dev/null +++ b/tests/target/issue-5244/wrap_comments_false.rs @@ -0,0 +1,7 @@ +// rustfmt-wrap_comments:false + +/// Here is me writing some documentation that is too long oh me oh my now some code please! +/// ``` +/// test +/// ``` +fn foo() {} diff --git a/tests/target/issue-5244/wrap_comments_true.rs b/tests/target/issue-5244/wrap_comments_true.rs new file mode 100644 index 00000000000..f1ad550ba17 --- /dev/null +++ b/tests/target/issue-5244/wrap_comments_true.rs @@ -0,0 +1,8 @@ +// rustfmt-wrap_comments:true + +/// Here is me writing some documentation that is too long oh me oh my now some +/// code please! +/// ``` +/// test +/// ``` +fn foo() {}