Skip to content

Commit

Permalink
Prevent wrapping markdown headers in doc comments
Browse files Browse the repository at this point in the history
Fixes 5238

A markdown header is defined by a string that starts with `#`.

Previously, rustfmt would wrap long markdown headers when
`wrap_comments=true`. This lead to issues when rendering these headers
in HTML using rustdoc.

Now, rustfmt leaves markdown headers alone when wrapping comments.
  • Loading branch information
ytmimi authored and calebcartwright committed Mar 1, 2022
1 parent 12048e4 commit 272fb42
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ impl<'a> CommentRewrite<'a> {
i: usize,
line: &'a str,
has_leading_whitespace: bool,
is_doc_comment: bool,
) -> bool {
let num_newlines = count_newlines(orig);
let is_last = i == num_newlines;
Expand Down Expand Up @@ -789,10 +790,19 @@ impl<'a> CommentRewrite<'a> {
}
}

if self.fmt.config.wrap_comments()
let is_markdown_header_doc_comment = is_doc_comment && line.starts_with("#");

// We only want to wrap the comment if:
// 1) wrap_comments = true is configured
// 2) The comment is not the start of a markdown header doc comment
// 3) The comment width exceeds the shape's width
// 4) No URLS were found in the commnet
let should_wrap_comment = self.fmt.config.wrap_comments()
&& !is_markdown_header_doc_comment
&& unicode_str_width(line) > self.fmt.shape.width
&& !has_url(line)
{
&& !has_url(line);

if should_wrap_comment {
match rewrite_string(line, &self.fmt, self.max_width) {
Some(ref s) => {
self.is_prev_line_multi_line = s.contains('\n');
Expand Down Expand Up @@ -882,7 +892,7 @@ fn rewrite_comment_inner(
});

for (i, (line, has_leading_whitespace)) in lines.enumerate() {
if rewriter.handle_line(orig, i, line, has_leading_whitespace) {
if rewriter.handle_line(orig, i, line, has_leading_whitespace, is_doc_comment) {
break;
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/source/issue-5238/markdown_header_wrap_comments_false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-wrap_comments: false

/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
fn not_documented_with_markdown_header() {
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
}

/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
fn documented_with_markdown_header() {
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
}
11 changes: 11 additions & 0 deletions tests/source/issue-5238/markdown_header_wrap_comments_true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-wrap_comments: true

/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
fn not_documented_with_markdown_header() {
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
}

/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
fn documented_with_markdown_header() {
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
}
11 changes: 11 additions & 0 deletions tests/target/issue-5238/markdown_header_wrap_comments_false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-wrap_comments: false

/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
fn not_documented_with_markdown_header() {
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
}

/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
fn documented_with_markdown_header() {
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
}
14 changes: 14 additions & 0 deletions tests/target/issue-5238/markdown_header_wrap_comments_true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// rustfmt-wrap_comments: true

/// no markdown header so rustfmt should wrap this comment when
/// `format_code_in_doc_comments = true` and `wrap_comments = true`
fn not_documented_with_markdown_header() {
// This is just a normal inline comment so rustfmt should wrap this comment
// when `wrap_comments = true`
}

/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
fn documented_with_markdown_header() {
// # We're using a markdown header in an inline comment. rustfmt should be
// able to wrap this comment when `wrap_comments = true`
}

0 comments on commit 272fb42

Please sign in to comment.