Skip to content

Commit 33c6074

Browse files
authored
Add width for codeblocks in comments (rust-lang#5372)
* add doc_comment_code_block_width configuration * updated config docu * Updated docu and changed tests to config folder
1 parent e44380b commit 33c6074

File tree

9 files changed

+131
-0
lines changed

9 files changed

+131
-0
lines changed

Configurations.md

+8
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@ fn add_one(x: i32) -> i32 {
926926
}
927927
```
928928

929+
## `doc_comment_code_block_width`
930+
931+
Max width for code snippets included in doc comments. Only used if [`format_code_in_doc_comments`](#format_code_in_doc_comments) is true.
932+
933+
- **Default value**: `100`
934+
- **Possible values**: any positive integer that is less than or equal to the value specified for [`max_width`](#max_width)
935+
- **Stable**: No (tracking issue: [#5359](https://github.com/rust-lang/rustfmt/issues/5359))
936+
929937
## `format_generated_files`
930938

931939
Format generated files. A file is considered generated

src/comment.rs

+4
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ impl<'a> CommentRewrite<'a> {
730730
{
731731
let mut config = self.fmt.config.clone();
732732
config.set().wrap_comments(false);
733+
let comment_max_width = config
734+
.doc_comment_code_block_width()
735+
.min(config.max_width());
736+
config.set().max_width(comment_max_width);
733737
if let Some(s) =
734738
crate::format_code_block(&self.code_block_buffer, &config, false)
735739
{

src/config/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ create_config! {
5757
// Comments. macros, and strings
5858
wrap_comments: bool, false, false, "Break comments to fit on the line";
5959
format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments.";
60+
doc_comment_code_block_width: usize, 100, false, "Maximum width for code snippets in doc \
61+
comments. No effect unless format_code_in_doc_comments = true";
6062
comment_width: usize, 80, false,
6163
"Maximum length of comments. No effect unless wrap_comments = true";
6264
normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";
@@ -532,6 +534,7 @@ chain_width = 60
532534
single_line_if_else_max_width = 50
533535
wrap_comments = false
534536
format_code_in_doc_comments = false
537+
doc_comment_code_block_width = 100
535538
comment_width = 80
536539
normalize_comments = false
537540
normalize_doc_attributes = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-doc_comment_code_block_width: 100
3+
4+
/// ```rust
5+
/// impl Test {
6+
/// pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
7+
/// Self::from_bytes_manual_slice(v, 0, v.len() )
8+
/// }
9+
/// }
10+
/// ```
11+
12+
impl Test {
13+
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
14+
Self::from_bytes_manual_slice(v, 0, v.len() )
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-max_width: 50
2+
// rustfmt-format_code_in_doc_comments: true
3+
// rustfmt-doc_comment_code_block_width: 100
4+
5+
/// ```rust
6+
/// impl Test {
7+
/// pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
8+
/// Self::from_bytes_manual_slice(v, 0, v.len() )
9+
/// }
10+
/// }
11+
/// ```
12+
13+
impl Test {
14+
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
15+
Self::from_bytes_manual_slice(v, 0, v.len() )
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-doc_comment_code_block_width: 50
3+
4+
/// ```rust
5+
/// impl Test {
6+
/// pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
7+
/// Self::from_bytes_manual_slice(v, 0, v.len() )
8+
/// }
9+
/// }
10+
/// ```
11+
12+
impl Test {
13+
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
14+
Self::from_bytes_manual_slice(v, 0, v.len() )
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-doc_comment_code_block_width: 100
3+
4+
/// ```rust
5+
/// impl Test {
6+
/// pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
7+
/// Self::from_bytes_manual_slice(v, 0, v.len())
8+
/// }
9+
/// }
10+
/// ```
11+
12+
impl Test {
13+
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
14+
Self::from_bytes_manual_slice(v, 0, v.len())
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-max_width: 50
2+
// rustfmt-format_code_in_doc_comments: true
3+
// rustfmt-doc_comment_code_block_width: 100
4+
5+
/// ```rust
6+
/// impl Test {
7+
/// pub const fn from_bytes(
8+
/// v: &[u8],
9+
/// ) -> Result<Self, ParserError> {
10+
/// Self::from_bytes_manual_slice(
11+
/// v,
12+
/// 0,
13+
/// v.len(),
14+
/// )
15+
/// }
16+
/// }
17+
/// ```
18+
19+
impl Test {
20+
pub const fn from_bytes(
21+
v: &[u8],
22+
) -> Result<Self, ParserError> {
23+
Self::from_bytes_manual_slice(
24+
v,
25+
0,
26+
v.len(),
27+
)
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
// rustfmt-doc_comment_code_block_width: 50
3+
4+
/// ```rust
5+
/// impl Test {
6+
/// pub const fn from_bytes(
7+
/// v: &[u8],
8+
/// ) -> Result<Self, ParserError> {
9+
/// Self::from_bytes_manual_slice(
10+
/// v,
11+
/// 0,
12+
/// v.len(),
13+
/// )
14+
/// }
15+
/// }
16+
/// ```
17+
18+
impl Test {
19+
pub const fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
20+
Self::from_bytes_manual_slice(v, 0, v.len())
21+
}
22+
}

0 commit comments

Comments
 (0)