diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b54af8299..39372bcb129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] +- Change option `format_doc_comment` to `format_code_in_doc_comment`. - `use_small_heuristics` changed to be an enum and stabilised. Configuration options are now ready for 1.0. diff --git a/Configurations.md b/Configurations.md index 7557b3637f1..dc91a3d255a 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1978,9 +1978,9 @@ fn main() { } ``` -## `format_doc_comments` +## `format_code_in_doc_comments` -Format doc comments. +Format code snippet included in doc comments. - **Default value**: `false` - **Possible values**: `true`, `false` diff --git a/src/comment.rs b/src/comment.rs index 7f7dce1166d..232d3745e59 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -353,7 +353,7 @@ fn identify_comment( trim_left_preserve_layout(first_group, shape.indent, config)? } else if !config.normalize_comments() && !config.wrap_comments() - && !config.format_doc_comments() + && !config.format_code_in_doc_comments() { light_rewrite_comment(first_group, shape.indent, config, is_doc_comment) } else { @@ -656,9 +656,16 @@ impl<'a> CommentRewrite<'a> { _ => { let mut config = self.fmt.config.clone(); config.set().wrap_comments(false); - match crate::format_code_block(&self.code_block_buffer, &config) { - Some(ref s) => trim_custom_comment_prefix(&s.snippet), - None => trim_custom_comment_prefix(&self.code_block_buffer), + if config.format_code_in_doc_comments() { + if let Some(s) = + crate::format_code_block(&self.code_block_buffer, &config) + { + trim_custom_comment_prefix(&s.snippet) + } else { + trim_custom_comment_prefix(&self.code_block_buffer) + } + } else { + trim_custom_comment_prefix(&self.code_block_buffer) } } }; diff --git a/src/config/mod.rs b/src/config/mod.rs index 82a7f5fc39a..d2189904965 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -40,7 +40,7 @@ create_config! { // Comments. macros, and strings wrap_comments: bool, false, false, "Break comments to fit on the line"; - format_doc_comments: bool, false, false, "Format doc comments."; + format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments."; comment_width: usize, 80, false, "Maximum length of comments. No effect unless wrap_comments = true"; normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible"; diff --git a/tests/source/doc-comment-with-example.rs b/tests/source/doc-comment-with-example.rs index 58d98acfa25..e74ceefd195 100644 --- a/tests/source/doc-comment-with-example.rs +++ b/tests/source/doc-comment-with-example.rs @@ -1,4 +1,4 @@ -// rustfmt-format_doc_comments: true +// rustfmt-format_code_in_doc_comments: true /// Foo /// diff --git a/tests/source/invalid-rust-code-in-doc-comment.rs b/tests/source/invalid-rust-code-in-doc-comment.rs index 6d33dcfce55..835b0261b76 100644 --- a/tests/source/invalid-rust-code-in-doc-comment.rs +++ b/tests/source/invalid-rust-code-in-doc-comment.rs @@ -1,4 +1,4 @@ -// rustfmt-format_doc_comments: true +// rustfmt-format_code_in_doc_comments: true /// ```rust /// if (true) { … } diff --git a/tests/source/issue-2520.rs b/tests/source/issue-2520.rs index d8ecf1f7d69..5a23f10430d 100644 --- a/tests/source/issue-2520.rs +++ b/tests/source/issue-2520.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true //! ```rust //! println!( "hello, world" ); diff --git a/tests/source/issue-2523.rs b/tests/source/issue-2523.rs index 693d06e131a..491d5c38fc2 100644 --- a/tests/source/issue-2523.rs +++ b/tests/source/issue-2523.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true // Do not unindent macro calls in comment with unformattable syntax. //! ```rust diff --git a/tests/source/issue-3055/original.rs b/tests/source/issue-3055/original.rs index 1f7d33ac5ae..ad505547ac0 100644 --- a/tests/source/issue-3055/original.rs +++ b/tests/source/issue-3055/original.rs @@ -1,4 +1,5 @@ // rustfmt-wrap_comments: true +// rustfmt-format_code_in_doc_comments: true /// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui. /// diff --git a/tests/source/itemized-blocks/no_wrap.rs b/tests/source/itemized-blocks/no_wrap.rs index 6354d39eb96..a7b6a10a010 100644 --- a/tests/source/itemized-blocks/no_wrap.rs +++ b/tests/source/itemized-blocks/no_wrap.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true //! This is a list: //! * Outer diff --git a/tests/source/itemized-blocks/wrap.rs b/tests/source/itemized-blocks/wrap.rs index 61448dce852..955cc698b79 100644 --- a/tests/source/itemized-blocks/wrap.rs +++ b/tests/source/itemized-blocks/wrap.rs @@ -1,4 +1,5 @@ // rustfmt-wrap_comments: true +// rustfmt-format_code_in_doc_comments: true // rustfmt-max_width: 50 //! This is a list: diff --git a/tests/source/normalize_doc_attributes_should_not_imply_format_doc_comments.rs b/tests/source/normalize_doc_attributes_should_not_imply_format_doc_comments.rs new file mode 100644 index 00000000000..a97705bfb3b --- /dev/null +++ b/tests/source/normalize_doc_attributes_should_not_imply_format_doc_comments.rs @@ -0,0 +1,15 @@ +// rustfmt-normalize_doc_attributes: true + +/// Foo +/// +/// # Example +/// ``` +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))] +/// # #![cfg_attr(not(dox), no_std)] +/// fn foo() { } +/// ``` +/// +fn foo() {} + +#[doc = "Bar documents"] +fn bar() {} diff --git a/tests/source/wrap_comments_should_not_imply_format_doc_comments.rs b/tests/source/wrap_comments_should_not_imply_format_doc_comments.rs new file mode 100644 index 00000000000..78b3ce146f2 --- /dev/null +++ b/tests/source/wrap_comments_should_not_imply_format_doc_comments.rs @@ -0,0 +1,16 @@ +// rustfmt-wrap_comments: true + +/// Foo +/// +/// # Example +/// ``` +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))] +/// # #![cfg_attr(not(dox), no_std)] +/// fn foo() { } +/// ``` +/// +fn foo() {} + +/// A long commment for wrapping +/// This is a long long long long long long long long long long long long long long long long long long long long sentence. +fn bar() {} diff --git a/tests/target/doc-comment-with-example.rs b/tests/target/doc-comment-with-example.rs index bec931d13c7..c5a4e779ea2 100644 --- a/tests/target/doc-comment-with-example.rs +++ b/tests/target/doc-comment-with-example.rs @@ -1,4 +1,4 @@ -// rustfmt-format_doc_comments: true +// rustfmt-format_code_in_doc_comments: true /// Foo /// diff --git a/tests/target/invalid-rust-code-in-doc-comment.rs b/tests/target/invalid-rust-code-in-doc-comment.rs index 2593410a418..f8479d4e345 100644 --- a/tests/target/invalid-rust-code-in-doc-comment.rs +++ b/tests/target/invalid-rust-code-in-doc-comment.rs @@ -1,4 +1,4 @@ -// rustfmt-format_doc_comments: true +// rustfmt-format_code_in_doc_comments: true /// ```rust /// if (true) { … } diff --git a/tests/target/issue-2520.rs b/tests/target/issue-2520.rs index 012921f441c..7c134d3972b 100644 --- a/tests/target/issue-2520.rs +++ b/tests/target/issue-2520.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true //! ```rust //! println!("hello, world"); diff --git a/tests/target/issue-2523.rs b/tests/target/issue-2523.rs index c62e058cde6..612f93249ac 100644 --- a/tests/target/issue-2523.rs +++ b/tests/target/issue-2523.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true // Do not unindent macro calls in comment with unformattable syntax. //! ```rust diff --git a/tests/target/issue-3055/original.rs b/tests/target/issue-3055/original.rs index 1455ac1a916..de27ccfb344 100644 --- a/tests/target/issue-3055/original.rs +++ b/tests/target/issue-3055/original.rs @@ -1,4 +1,5 @@ // rustfmt-wrap_comments: true +// rustfmt-format_code_in_doc_comments: true /// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc /// commodo ultricies dui. diff --git a/tests/target/itemized-blocks/no_wrap.rs b/tests/target/itemized-blocks/no_wrap.rs index 945c3fbbca0..de885638272 100644 --- a/tests/target/itemized-blocks/no_wrap.rs +++ b/tests/target/itemized-blocks/no_wrap.rs @@ -1,4 +1,5 @@ // rustfmt-normalize_comments: true +// rustfmt-format_code_in_doc_comments: true //! This is a list: //! * Outer diff --git a/tests/target/itemized-blocks/wrap.rs b/tests/target/itemized-blocks/wrap.rs index 0a21c474ca6..a4907303c9e 100644 --- a/tests/target/itemized-blocks/wrap.rs +++ b/tests/target/itemized-blocks/wrap.rs @@ -1,4 +1,5 @@ // rustfmt-wrap_comments: true +// rustfmt-format_code_in_doc_comments: true // rustfmt-max_width: 50 //! This is a list: diff --git a/tests/target/normalize_doc_attributes_should_not_imply_format_doc_comments.rs b/tests/target/normalize_doc_attributes_should_not_imply_format_doc_comments.rs new file mode 100644 index 00000000000..562d9565e9a --- /dev/null +++ b/tests/target/normalize_doc_attributes_should_not_imply_format_doc_comments.rs @@ -0,0 +1,15 @@ +// rustfmt-normalize_doc_attributes: true + +/// Foo +/// +/// # Example +/// ``` +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))] +/// # #![cfg_attr(not(dox), no_std)] +/// fn foo() { } +/// ``` +/// +fn foo() {} + +///Bar documents +fn bar() {} diff --git a/tests/target/wrap_comments_should_not_imply_format_doc_comments.rs b/tests/target/wrap_comments_should_not_imply_format_doc_comments.rs new file mode 100644 index 00000000000..d61d4d7c216 --- /dev/null +++ b/tests/target/wrap_comments_should_not_imply_format_doc_comments.rs @@ -0,0 +1,16 @@ +// rustfmt-wrap_comments: true + +/// Foo +/// +/// # Example +/// ``` +/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))] +/// # #![cfg_attr(not(dox), no_std)] +/// fn foo() { } +/// ``` +fn foo() {} + +/// A long commment for wrapping +/// This is a long long long long long long long long long long long long long +/// long long long long long long long sentence. +fn bar() {}