Skip to content

Commit

Permalink
rustfmt doc code blocks with multiple comma-separated attributes
Browse files Browse the repository at this point in the history
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 #3158
  • Loading branch information
nipunn1313 authored and calebcartwright committed Sep 9, 2021
1 parent a806883 commit b10ab51
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 29 deletions.
52 changes: 23 additions & 29 deletions src/comment.rs
Expand Up @@ -394,28 +394,26 @@ fn identify_comment(
}
}

/// Attributes for code blocks in rustdoc.
/// See https://doc.rust-lang.org/rustdoc/print.html#attributes
/// Enum indicating if the code block contains rust based on attributes
enum CodeBlockAttribute {
Rust,
Ignore,
Text,
ShouldPanic,
NoRun,
CompileFail,
NotRust,
}

impl CodeBlockAttribute {
fn new(attribute: &str) -> CodeBlockAttribute {
match attribute {
"rust" | "" => CodeBlockAttribute::Rust,
"ignore" => CodeBlockAttribute::Ignore,
"text" => CodeBlockAttribute::Text,
"should_panic" => CodeBlockAttribute::ShouldPanic,
"no_run" => CodeBlockAttribute::NoRun,
"compile_fail" => CodeBlockAttribute::CompileFail,
_ => CodeBlockAttribute::Text,
/// Parse comma separated attributes list. Return rust only if all
/// attributes are valid rust attributes
/// See https://doc.rust-lang.org/rustdoc/print.html#attributes
fn new(attributes: &str) -> CodeBlockAttribute {
for attribute in attributes.split(",") {
match attribute.trim() {
"" | "rust" | "should_panic" | "no_run" | "edition2015" | "edition2018"
| "edition2021" => (),
"ignore" | "compile_fail" | "text" => return CodeBlockAttribute::NotRust,
_ => return CodeBlockAttribute::NotRust,
}
}
CodeBlockAttribute::Rust
}
}

Expand Down Expand Up @@ -649,25 +647,21 @@ impl<'a> CommentRewrite<'a> {
} else if self.code_block_attr.is_some() {
if line.starts_with("```") {
let code_block = match self.code_block_attr.as_ref().unwrap() {
CodeBlockAttribute::Ignore | CodeBlockAttribute::Text => {
trim_custom_comment_prefix(&self.code_block_buffer)
}
_ if self.code_block_buffer.is_empty() => String::new(),
_ => {
CodeBlockAttribute::Rust
if self.fmt.config.format_code_in_doc_comments()
&& !self.code_block_buffer.is_empty() =>
{
let mut config = self.fmt.config.clone();
config.set().wrap_comments(false);
if config.format_code_in_doc_comments() {
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config, false)
{
trim_custom_comment_prefix(&s.snippet)
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
}
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config, false)
{
trim_custom_comment_prefix(&s.snippet)
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
}
}
_ => trim_custom_comment_prefix(&self.code_block_buffer),
};
if !code_block.is_empty() {
self.result.push_str(&self.comment_line_separator);
Expand Down
74 changes: 74 additions & 0 deletions tests/source/issue-3158.rs
@@ -0,0 +1,74 @@
// rustfmt-format_code_in_doc_comments: true

/// Should format
/// ```rust
/// assert!( false );
/// ```
///
/// Should format
/// ```rust,should_panic
/// assert!( false );
/// ```
///
/// Should format
/// ```rust,should_panic,edition2018
/// assert!( false );
/// ```
///
/// Should format
/// ```rust , should_panic , edition2018
/// assert!( false );
/// ```
///
/// Should not format
/// ```ignore
/// assert!( false );
/// ```
///
/// Should not format (not all are rust)
/// ```rust,ignore
/// assert!( false );
/// ```
///
/// Should not format (rust compile_fail)
/// ```compile_fail
/// assert!( false );
/// ```
///
/// Should not format (rust compile_fail)
/// ```rust,compile_fail
/// assert!( false );
/// ```
///
/// Various unspecified ones that should format
/// ```
/// assert!( false );
/// ```
///
/// ```,
/// assert!( false );
/// ```
///
/// ```,,,,,
/// assert!( false );
/// ```
///
/// ```,,, rust ,,
/// assert!( false );
/// ```
///
/// Should not format
/// ```,,, rust , ignore,
/// assert!( false );
/// ```
///
/// Few empty ones
/// ```
/// ```
///
/// ```rust
/// ```
///
/// ```ignore
/// ```
fn foo() {}
74 changes: 74 additions & 0 deletions tests/target/issue-3158.rs
@@ -0,0 +1,74 @@
// rustfmt-format_code_in_doc_comments: true

/// Should format
/// ```rust
/// assert!(false);
/// ```
///
/// Should format
/// ```rust,should_panic
/// assert!(false);
/// ```
///
/// Should format
/// ```rust,should_panic,edition2018
/// assert!(false);
/// ```
///
/// Should format
/// ```rust , should_panic , edition2018
/// assert!(false);
/// ```
///
/// Should not format
/// ```ignore
/// assert!( false );
/// ```
///
/// Should not format (not all are rust)
/// ```rust,ignore
/// assert!( false );
/// ```
///
/// Should not format (rust compile_fail)
/// ```compile_fail
/// assert!( false );
/// ```
///
/// Should not format (rust compile_fail)
/// ```rust,compile_fail
/// assert!( false );
/// ```
///
/// Various unspecified ones that should format
/// ```
/// assert!(false);
/// ```
///
/// ```,
/// assert!(false);
/// ```
///
/// ```,,,,,
/// assert!(false);
/// ```
///
/// ```,,, rust ,,
/// assert!(false);
/// ```
///
/// Should not format
/// ```,,, rust , ignore,
/// assert!( false );
/// ```
///
/// Few empty ones
/// ```
/// ```
///
/// ```rust
/// ```
///
/// ```ignore
/// ```
fn foo() {}

0 comments on commit b10ab51

Please sign in to comment.