Skip to content

Commit b10ab51

Browse files
nipunn1313calebcartwright
authored andcommitted
rustfmt doc code blocks with multiple comma-separated attributes
Added test covering this. Chose to treat the code block as rust if and only if all of the comma-separated attributes are rust-valid. Chose to allow/preserve whitespace around commas Fixes rust-lang#3158
1 parent a806883 commit b10ab51

File tree

3 files changed

+171
-29
lines changed

3 files changed

+171
-29
lines changed

src/comment.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,26 @@ fn identify_comment(
394394
}
395395
}
396396

397-
/// Attributes for code blocks in rustdoc.
398-
/// See https://doc.rust-lang.org/rustdoc/print.html#attributes
397+
/// Enum indicating if the code block contains rust based on attributes
399398
enum CodeBlockAttribute {
400399
Rust,
401-
Ignore,
402-
Text,
403-
ShouldPanic,
404-
NoRun,
405-
CompileFail,
400+
NotRust,
406401
}
407402

408403
impl CodeBlockAttribute {
409-
fn new(attribute: &str) -> CodeBlockAttribute {
410-
match attribute {
411-
"rust" | "" => CodeBlockAttribute::Rust,
412-
"ignore" => CodeBlockAttribute::Ignore,
413-
"text" => CodeBlockAttribute::Text,
414-
"should_panic" => CodeBlockAttribute::ShouldPanic,
415-
"no_run" => CodeBlockAttribute::NoRun,
416-
"compile_fail" => CodeBlockAttribute::CompileFail,
417-
_ => CodeBlockAttribute::Text,
404+
/// Parse comma separated attributes list. Return rust only if all
405+
/// attributes are valid rust attributes
406+
/// See https://doc.rust-lang.org/rustdoc/print.html#attributes
407+
fn new(attributes: &str) -> CodeBlockAttribute {
408+
for attribute in attributes.split(",") {
409+
match attribute.trim() {
410+
"" | "rust" | "should_panic" | "no_run" | "edition2015" | "edition2018"
411+
| "edition2021" => (),
412+
"ignore" | "compile_fail" | "text" => return CodeBlockAttribute::NotRust,
413+
_ => return CodeBlockAttribute::NotRust,
414+
}
418415
}
416+
CodeBlockAttribute::Rust
419417
}
420418
}
421419

@@ -649,25 +647,21 @@ impl<'a> CommentRewrite<'a> {
649647
} else if self.code_block_attr.is_some() {
650648
if line.starts_with("```") {
651649
let code_block = match self.code_block_attr.as_ref().unwrap() {
652-
CodeBlockAttribute::Ignore | CodeBlockAttribute::Text => {
653-
trim_custom_comment_prefix(&self.code_block_buffer)
654-
}
655-
_ if self.code_block_buffer.is_empty() => String::new(),
656-
_ => {
650+
CodeBlockAttribute::Rust
651+
if self.fmt.config.format_code_in_doc_comments()
652+
&& !self.code_block_buffer.is_empty() =>
653+
{
657654
let mut config = self.fmt.config.clone();
658655
config.set().wrap_comments(false);
659-
if config.format_code_in_doc_comments() {
660-
if let Some(s) =
661-
crate::format_code_block(&self.code_block_buffer, &config, false)
662-
{
663-
trim_custom_comment_prefix(&s.snippet)
664-
} else {
665-
trim_custom_comment_prefix(&self.code_block_buffer)
666-
}
656+
if let Some(s) =
657+
crate::format_code_block(&self.code_block_buffer, &config, false)
658+
{
659+
trim_custom_comment_prefix(&s.snippet)
667660
} else {
668661
trim_custom_comment_prefix(&self.code_block_buffer)
669662
}
670663
}
664+
_ => trim_custom_comment_prefix(&self.code_block_buffer),
671665
};
672666
if !code_block.is_empty() {
673667
self.result.push_str(&self.comment_line_separator);

tests/source/issue-3158.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Should format
4+
/// ```rust
5+
/// assert!( false );
6+
/// ```
7+
///
8+
/// Should format
9+
/// ```rust,should_panic
10+
/// assert!( false );
11+
/// ```
12+
///
13+
/// Should format
14+
/// ```rust,should_panic,edition2018
15+
/// assert!( false );
16+
/// ```
17+
///
18+
/// Should format
19+
/// ```rust , should_panic , edition2018
20+
/// assert!( false );
21+
/// ```
22+
///
23+
/// Should not format
24+
/// ```ignore
25+
/// assert!( false );
26+
/// ```
27+
///
28+
/// Should not format (not all are rust)
29+
/// ```rust,ignore
30+
/// assert!( false );
31+
/// ```
32+
///
33+
/// Should not format (rust compile_fail)
34+
/// ```compile_fail
35+
/// assert!( false );
36+
/// ```
37+
///
38+
/// Should not format (rust compile_fail)
39+
/// ```rust,compile_fail
40+
/// assert!( false );
41+
/// ```
42+
///
43+
/// Various unspecified ones that should format
44+
/// ```
45+
/// assert!( false );
46+
/// ```
47+
///
48+
/// ```,
49+
/// assert!( false );
50+
/// ```
51+
///
52+
/// ```,,,,,
53+
/// assert!( false );
54+
/// ```
55+
///
56+
/// ```,,, rust ,,
57+
/// assert!( false );
58+
/// ```
59+
///
60+
/// Should not format
61+
/// ```,,, rust , ignore,
62+
/// assert!( false );
63+
/// ```
64+
///
65+
/// Few empty ones
66+
/// ```
67+
/// ```
68+
///
69+
/// ```rust
70+
/// ```
71+
///
72+
/// ```ignore
73+
/// ```
74+
fn foo() {}

tests/target/issue-3158.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// Should format
4+
/// ```rust
5+
/// assert!(false);
6+
/// ```
7+
///
8+
/// Should format
9+
/// ```rust,should_panic
10+
/// assert!(false);
11+
/// ```
12+
///
13+
/// Should format
14+
/// ```rust,should_panic,edition2018
15+
/// assert!(false);
16+
/// ```
17+
///
18+
/// Should format
19+
/// ```rust , should_panic , edition2018
20+
/// assert!(false);
21+
/// ```
22+
///
23+
/// Should not format
24+
/// ```ignore
25+
/// assert!( false );
26+
/// ```
27+
///
28+
/// Should not format (not all are rust)
29+
/// ```rust,ignore
30+
/// assert!( false );
31+
/// ```
32+
///
33+
/// Should not format (rust compile_fail)
34+
/// ```compile_fail
35+
/// assert!( false );
36+
/// ```
37+
///
38+
/// Should not format (rust compile_fail)
39+
/// ```rust,compile_fail
40+
/// assert!( false );
41+
/// ```
42+
///
43+
/// Various unspecified ones that should format
44+
/// ```
45+
/// assert!(false);
46+
/// ```
47+
///
48+
/// ```,
49+
/// assert!(false);
50+
/// ```
51+
///
52+
/// ```,,,,,
53+
/// assert!(false);
54+
/// ```
55+
///
56+
/// ```,,, rust ,,
57+
/// assert!(false);
58+
/// ```
59+
///
60+
/// Should not format
61+
/// ```,,, rust , ignore,
62+
/// assert!( false );
63+
/// ```
64+
///
65+
/// Few empty ones
66+
/// ```
67+
/// ```
68+
///
69+
/// ```rust
70+
/// ```
71+
///
72+
/// ```ignore
73+
/// ```
74+
fn foo() {}

0 commit comments

Comments
 (0)