Skip to content

Commit 272fb42

Browse files
ytmimicalebcartwright
authored andcommitted
Prevent wrapping markdown headers in doc comments
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.
1 parent 12048e4 commit 272fb42

5 files changed

+61
-4
lines changed

Diff for: src/comment.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ impl<'a> CommentRewrite<'a> {
683683
i: usize,
684684
line: &'a str,
685685
has_leading_whitespace: bool,
686+
is_doc_comment: bool,
686687
) -> bool {
687688
let num_newlines = count_newlines(orig);
688689
let is_last = i == num_newlines;
@@ -789,10 +790,19 @@ impl<'a> CommentRewrite<'a> {
789790
}
790791
}
791792

792-
if self.fmt.config.wrap_comments()
793+
let is_markdown_header_doc_comment = is_doc_comment && line.starts_with("#");
794+
795+
// We only want to wrap the comment if:
796+
// 1) wrap_comments = true is configured
797+
// 2) The comment is not the start of a markdown header doc comment
798+
// 3) The comment width exceeds the shape's width
799+
// 4) No URLS were found in the commnet
800+
let should_wrap_comment = self.fmt.config.wrap_comments()
801+
&& !is_markdown_header_doc_comment
793802
&& unicode_str_width(line) > self.fmt.shape.width
794-
&& !has_url(line)
795-
{
803+
&& !has_url(line);
804+
805+
if should_wrap_comment {
796806
match rewrite_string(line, &self.fmt, self.max_width) {
797807
Some(ref s) => {
798808
self.is_prev_line_multi_line = s.contains('\n');
@@ -882,7 +892,7 @@ fn rewrite_comment_inner(
882892
});
883893

884894
for (i, (line, has_leading_whitespace)) in lines.enumerate() {
885-
if rewriter.handle_line(orig, i, line, has_leading_whitespace) {
895+
if rewriter.handle_line(orig, i, line, has_leading_whitespace, is_doc_comment) {
886896
break;
887897
}
888898
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-wrap_comments: false
2+
3+
/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
4+
fn not_documented_with_markdown_header() {
5+
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
6+
}
7+
8+
/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
9+
fn documented_with_markdown_header() {
10+
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
4+
fn not_documented_with_markdown_header() {
5+
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
6+
}
7+
8+
/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
9+
fn documented_with_markdown_header() {
10+
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-wrap_comments: false
2+
3+
/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
4+
fn not_documented_with_markdown_header() {
5+
// This is just a normal inline comment so rustfmt should wrap this comment when `wrap_comments = true`
6+
}
7+
8+
/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
9+
fn documented_with_markdown_header() {
10+
// # We're using a markdown header in an inline comment. rustfmt should be able to wrap this comment when `wrap_comments = true`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// no markdown header so rustfmt should wrap this comment when
4+
/// `format_code_in_doc_comments = true` and `wrap_comments = true`
5+
fn not_documented_with_markdown_header() {
6+
// This is just a normal inline comment so rustfmt should wrap this comment
7+
// when `wrap_comments = true`
8+
}
9+
10+
/// # We're using a markdown header here so rustfmt should refuse to wrap this comment in all circumstances
11+
fn documented_with_markdown_header() {
12+
// # We're using a markdown header in an inline comment. rustfmt should be
13+
// able to wrap this comment when `wrap_comments = true`
14+
}

0 commit comments

Comments
 (0)