From c9e436e941c2cb653d34aad84ef78b53e5a4e821 Mon Sep 17 00:00:00 2001 From: sec65 Date: Tue, 7 Jun 2022 13:27:01 +0200 Subject: [PATCH 1/3] add doc_comment_code_block_width configuration --- Configurations.md | 8 +++++ src/comment.rs | 4 +++ src/config/mod.rs | 3 ++ .../code_comment_width_greater_max_width.rs | 17 +++++++++++ .../issue-5359/with_code_comment_max_width.rs | 16 ++++++++++ .../issue-5359/wo_code_comment_max_width.rs | 15 ++++++++++ .../code_comment_width_greater_max_width.rs | 29 +++++++++++++++++++ .../issue-5359/with_code_comment_max_width.rs | 22 ++++++++++++++ .../issue-5359/wo_code_comment_max_width.rs | 15 ++++++++++ 9 files changed, 129 insertions(+) create mode 100644 tests/source/issue-5359/code_comment_width_greater_max_width.rs create mode 100644 tests/source/issue-5359/with_code_comment_max_width.rs create mode 100644 tests/source/issue-5359/wo_code_comment_max_width.rs create mode 100644 tests/target/issue-5359/code_comment_width_greater_max_width.rs create mode 100644 tests/target/issue-5359/with_code_comment_max_width.rs create mode 100644 tests/target/issue-5359/wo_code_comment_max_width.rs diff --git a/Configurations.md b/Configurations.md index 8c84614352c..fbe7735855b 100644 --- a/Configurations.md +++ b/Configurations.md @@ -926,6 +926,14 @@ fn add_one(x: i32) -> i32 { } ``` +## `doc_comment_code_block_width` + +Max width for code snippets included in doc comments. + +- **Default value**: `100` +- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width) +- **Stable**: No (tracking issue: [#5359](https://github.com/rust-lang/rustfmt/issues/5359)) + ## `format_generated_files` Format generated files. A file is considered generated diff --git a/src/comment.rs b/src/comment.rs index eb195b1f762..4d565afc1e0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -730,6 +730,10 @@ impl<'a> CommentRewrite<'a> { { let mut config = self.fmt.config.clone(); config.set().wrap_comments(false); + let comment_max_width = config + .doc_comment_code_block_width() + .min(config.max_width()); + config.set().max_width(comment_max_width); if let Some(s) = crate::format_code_block(&self.code_block_buffer, &config, false) { diff --git a/src/config/mod.rs b/src/config/mod.rs index a5169528187..f49c18d3a46 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -57,6 +57,8 @@ create_config! { // Comments. macros, and strings wrap_comments: bool, false, false, "Break comments to fit on the line"; format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments."; + doc_comment_code_block_width: usize, 100, false, "Maximum width for code snippets in doc \ + comments. No effect unless format_code_in_doc_comments = true"; 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"; @@ -532,6 +534,7 @@ chain_width = 60 single_line_if_else_max_width = 50 wrap_comments = false format_code_in_doc_comments = false +doc_comment_code_block_width = 100 comment_width = 80 normalize_comments = false normalize_doc_attributes = false diff --git a/tests/source/issue-5359/code_comment_width_greater_max_width.rs b/tests/source/issue-5359/code_comment_width_greater_max_width.rs new file mode 100644 index 00000000000..96505c69714 --- /dev/null +++ b/tests/source/issue-5359/code_comment_width_greater_max_width.rs @@ -0,0 +1,17 @@ +// rustfmt-max_width: 50 +// rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 100 + +/// ```rust +/// impl Test { +/// pub const fn from_bytes(v: &[u8]) -> Result { +/// Self::from_bytes_manual_slice(v, 0, v.len() ) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len() ) + } +} diff --git a/tests/source/issue-5359/with_code_comment_max_width.rs b/tests/source/issue-5359/with_code_comment_max_width.rs new file mode 100644 index 00000000000..2c6307951c8 --- /dev/null +++ b/tests/source/issue-5359/with_code_comment_max_width.rs @@ -0,0 +1,16 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 50 + +/// ```rust +/// impl Test { +/// pub const fn from_bytes(v: &[u8]) -> Result { +/// Self::from_bytes_manual_slice(v, 0, v.len() ) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len() ) + } +} diff --git a/tests/source/issue-5359/wo_code_comment_max_width.rs b/tests/source/issue-5359/wo_code_comment_max_width.rs new file mode 100644 index 00000000000..62d3013ef25 --- /dev/null +++ b/tests/source/issue-5359/wo_code_comment_max_width.rs @@ -0,0 +1,15 @@ +// rustfmt-format_code_in_doc_comments: true + +/// ```rust +/// impl Test { +/// pub const fn from_bytes(v: &[u8]) -> Result { +/// Self::from_bytes_manual_slice(v, 0, v.len() ) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len() ) + } +} diff --git a/tests/target/issue-5359/code_comment_width_greater_max_width.rs b/tests/target/issue-5359/code_comment_width_greater_max_width.rs new file mode 100644 index 00000000000..6bcb99b915f --- /dev/null +++ b/tests/target/issue-5359/code_comment_width_greater_max_width.rs @@ -0,0 +1,29 @@ +// rustfmt-max_width: 50 +// rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 100 + +/// ```rust +/// impl Test { +/// pub const fn from_bytes( +/// v: &[u8], +/// ) -> Result { +/// Self::from_bytes_manual_slice( +/// v, +/// 0, +/// v.len(), +/// ) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes( + v: &[u8], + ) -> Result { + Self::from_bytes_manual_slice( + v, + 0, + v.len(), + ) + } +} diff --git a/tests/target/issue-5359/with_code_comment_max_width.rs b/tests/target/issue-5359/with_code_comment_max_width.rs new file mode 100644 index 00000000000..e8ab6f28bdc --- /dev/null +++ b/tests/target/issue-5359/with_code_comment_max_width.rs @@ -0,0 +1,22 @@ +// rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 50 + +/// ```rust +/// impl Test { +/// pub const fn from_bytes( +/// v: &[u8], +/// ) -> Result { +/// Self::from_bytes_manual_slice( +/// v, +/// 0, +/// v.len(), +/// ) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } +} diff --git a/tests/target/issue-5359/wo_code_comment_max_width.rs b/tests/target/issue-5359/wo_code_comment_max_width.rs new file mode 100644 index 00000000000..69a1f84540a --- /dev/null +++ b/tests/target/issue-5359/wo_code_comment_max_width.rs @@ -0,0 +1,15 @@ +// rustfmt-format_code_in_doc_comments: true + +/// ```rust +/// impl Test { +/// pub const fn from_bytes(v: &[u8]) -> Result { +/// Self::from_bytes_manual_slice(v, 0, v.len()) +/// } +/// } +/// ``` + +impl Test { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } +} From 55fef436f05594cfb5b73a6c970c5fc44ec19f10 Mon Sep 17 00:00:00 2001 From: sec65 Date: Tue, 7 Jun 2022 13:34:15 +0200 Subject: [PATCH 2/3] updated config docu --- Configurations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Configurations.md b/Configurations.md index fbe7735855b..800be4ade92 100644 --- a/Configurations.md +++ b/Configurations.md @@ -928,7 +928,7 @@ fn add_one(x: i32) -> i32 { ## `doc_comment_code_block_width` -Max width for code snippets included in doc comments. +Max width for code snippets included in doc comments. Only used if `format_code_in_doc_comments` is true. - **Default value**: `100` - **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width) From 77f18438ee03a334b406e0411d6b7c1a45334b30 Mon Sep 17 00:00:00 2001 From: sec65 Date: Thu, 16 Jun 2022 08:52:02 +0200 Subject: [PATCH 3/3] Updated docu and changed tests to config folder --- Configurations.md | 2 +- .../doc_comment_code_block_width/100.rs} | 1 + .../doc_comment_code_block_width/100_greater_max_width.rs} | 0 .../doc_comment_code_block_width/50.rs} | 0 .../doc_comment_code_block_width/100.rs} | 1 + .../doc_comment_code_block_width/100_greater_max_width.rs} | 0 .../doc_comment_code_block_width/50.rs} | 0 7 files changed, 3 insertions(+), 1 deletion(-) rename tests/source/{issue-5359/wo_code_comment_max_width.rs => configs/doc_comment_code_block_width/100.rs} (89%) rename tests/source/{issue-5359/code_comment_width_greater_max_width.rs => configs/doc_comment_code_block_width/100_greater_max_width.rs} (100%) rename tests/source/{issue-5359/with_code_comment_max_width.rs => configs/doc_comment_code_block_width/50.rs} (100%) rename tests/target/{issue-5359/wo_code_comment_max_width.rs => configs/doc_comment_code_block_width/100.rs} (89%) rename tests/target/{issue-5359/code_comment_width_greater_max_width.rs => configs/doc_comment_code_block_width/100_greater_max_width.rs} (100%) rename tests/target/{issue-5359/with_code_comment_max_width.rs => configs/doc_comment_code_block_width/50.rs} (100%) diff --git a/Configurations.md b/Configurations.md index 800be4ade92..8b96b9d3689 100644 --- a/Configurations.md +++ b/Configurations.md @@ -928,7 +928,7 @@ fn add_one(x: i32) -> i32 { ## `doc_comment_code_block_width` -Max width for code snippets included in doc comments. Only used if `format_code_in_doc_comments` is true. +Max width for code snippets included in doc comments. Only used if [`format_code_in_doc_comments`](#format_code_in_doc_comments) is true. - **Default value**: `100` - **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width) diff --git a/tests/source/issue-5359/wo_code_comment_max_width.rs b/tests/source/configs/doc_comment_code_block_width/100.rs similarity index 89% rename from tests/source/issue-5359/wo_code_comment_max_width.rs rename to tests/source/configs/doc_comment_code_block_width/100.rs index 62d3013ef25..51578076167 100644 --- a/tests/source/issue-5359/wo_code_comment_max_width.rs +++ b/tests/source/configs/doc_comment_code_block_width/100.rs @@ -1,4 +1,5 @@ // rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 100 /// ```rust /// impl Test { diff --git a/tests/source/issue-5359/code_comment_width_greater_max_width.rs b/tests/source/configs/doc_comment_code_block_width/100_greater_max_width.rs similarity index 100% rename from tests/source/issue-5359/code_comment_width_greater_max_width.rs rename to tests/source/configs/doc_comment_code_block_width/100_greater_max_width.rs diff --git a/tests/source/issue-5359/with_code_comment_max_width.rs b/tests/source/configs/doc_comment_code_block_width/50.rs similarity index 100% rename from tests/source/issue-5359/with_code_comment_max_width.rs rename to tests/source/configs/doc_comment_code_block_width/50.rs diff --git a/tests/target/issue-5359/wo_code_comment_max_width.rs b/tests/target/configs/doc_comment_code_block_width/100.rs similarity index 89% rename from tests/target/issue-5359/wo_code_comment_max_width.rs rename to tests/target/configs/doc_comment_code_block_width/100.rs index 69a1f84540a..c010a28aab6 100644 --- a/tests/target/issue-5359/wo_code_comment_max_width.rs +++ b/tests/target/configs/doc_comment_code_block_width/100.rs @@ -1,4 +1,5 @@ // rustfmt-format_code_in_doc_comments: true +// rustfmt-doc_comment_code_block_width: 100 /// ```rust /// impl Test { diff --git a/tests/target/issue-5359/code_comment_width_greater_max_width.rs b/tests/target/configs/doc_comment_code_block_width/100_greater_max_width.rs similarity index 100% rename from tests/target/issue-5359/code_comment_width_greater_max_width.rs rename to tests/target/configs/doc_comment_code_block_width/100_greater_max_width.rs diff --git a/tests/target/issue-5359/with_code_comment_max_width.rs b/tests/target/configs/doc_comment_code_block_width/50.rs similarity index 100% rename from tests/target/issue-5359/with_code_comment_max_width.rs rename to tests/target/configs/doc_comment_code_block_width/50.rs