about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsec65 <106604020+sec65@users.noreply.github.com>2022-06-17 05:15:16 +0200
committerGitHub <noreply@github.com>2022-06-16 22:15:16 -0500
commit33c60740d3e1387a5979cac93785e605de8470e0 (patch)
tree55b4d31b976ce431947564639f5b9c68a7d7f930
parente44380b34167ff554ca7bd6ccd446a511927e8aa (diff)
downloadrust-33c60740d3e1387a5979cac93785e605de8470e0.tar.gz
rust-33c60740d3e1387a5979cac93785e605de8470e0.zip
Add width for codeblocks in comments (#5372)
* add doc_comment_code_block_width configuration

* updated config docu

* Updated docu and changed tests to config folder
-rw-r--r--Configurations.md8
-rw-r--r--src/comment.rs4
-rw-r--r--src/config/mod.rs3
-rw-r--r--tests/source/configs/doc_comment_code_block_width/100.rs16
-rw-r--r--tests/source/configs/doc_comment_code_block_width/100_greater_max_width.rs17
-rw-r--r--tests/source/configs/doc_comment_code_block_width/50.rs16
-rw-r--r--tests/target/configs/doc_comment_code_block_width/100.rs16
-rw-r--r--tests/target/configs/doc_comment_code_block_width/100_greater_max_width.rs29
-rw-r--r--tests/target/configs/doc_comment_code_block_width/50.rs22
9 files changed, 131 insertions, 0 deletions
diff --git a/Configurations.md b/Configurations.md
index 8c84614352c..8b96b9d3689 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. 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)
+- **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/configs/doc_comment_code_block_width/100.rs b/tests/source/configs/doc_comment_code_block_width/100.rs
new file mode 100644
index 00000000000..51578076167
--- /dev/null
+++ b/tests/source/configs/doc_comment_code_block_width/100.rs
@@ -0,0 +1,16 @@
+// 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, ParserError> {
+///         Self::from_bytes_manual_slice(v, 0, v.len()      )
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(v, 0, v.len()       )
+    }
+}
diff --git a/tests/source/configs/doc_comment_code_block_width/100_greater_max_width.rs b/tests/source/configs/doc_comment_code_block_width/100_greater_max_width.rs
new file mode 100644
index 00000000000..96505c69714
--- /dev/null
+++ b/tests/source/configs/doc_comment_code_block_width/100_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, ParserError> {
+///         Self::from_bytes_manual_slice(v, 0, v.len()      )
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(v, 0, v.len()       )
+    }
+}
diff --git a/tests/source/configs/doc_comment_code_block_width/50.rs b/tests/source/configs/doc_comment_code_block_width/50.rs
new file mode 100644
index 00000000000..2c6307951c8
--- /dev/null
+++ b/tests/source/configs/doc_comment_code_block_width/50.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, ParserError> {
+///         Self::from_bytes_manual_slice(v, 0, v.len()      )
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(v, 0, v.len()       )
+    }
+}
diff --git a/tests/target/configs/doc_comment_code_block_width/100.rs b/tests/target/configs/doc_comment_code_block_width/100.rs
new file mode 100644
index 00000000000..c010a28aab6
--- /dev/null
+++ b/tests/target/configs/doc_comment_code_block_width/100.rs
@@ -0,0 +1,16 @@
+// 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, ParserError> {
+///         Self::from_bytes_manual_slice(v, 0, v.len())
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(v, 0, v.len())
+    }
+}
diff --git a/tests/target/configs/doc_comment_code_block_width/100_greater_max_width.rs b/tests/target/configs/doc_comment_code_block_width/100_greater_max_width.rs
new file mode 100644
index 00000000000..6bcb99b915f
--- /dev/null
+++ b/tests/target/configs/doc_comment_code_block_width/100_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, ParserError> {
+///         Self::from_bytes_manual_slice(
+///             v,
+///             0,
+///             v.len(),
+///         )
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(
+        v: &[u8],
+    ) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(
+            v,
+            0,
+            v.len(),
+        )
+    }
+}
diff --git a/tests/target/configs/doc_comment_code_block_width/50.rs b/tests/target/configs/doc_comment_code_block_width/50.rs
new file mode 100644
index 00000000000..e8ab6f28bdc
--- /dev/null
+++ b/tests/target/configs/doc_comment_code_block_width/50.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, ParserError> {
+///         Self::from_bytes_manual_slice(
+///             v,
+///             0,
+///             v.len(),
+///         )
+///     }
+/// }
+/// ```
+
+impl Test {
+    pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+        Self::from_bytes_manual_slice(v, 0, v.len())
+    }
+}