Skip to content

Commit

Permalink
Add width for codeblocks in comments (#5372)
Browse files Browse the repository at this point in the history
* add doc_comment_code_block_width configuration

* updated config docu

* Updated docu and changed tests to config folder
  • Loading branch information
sec65 committed Jun 17, 2022
1 parent e44380b commit 33c6074
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/source/configs/doc_comment_code_block_width/100.rs
Original file line number Diff line number Diff line change
@@ -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() )
}
}
Original file line number Diff line number Diff line change
@@ -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() )
}
}
16 changes: 16 additions & 0 deletions tests/source/configs/doc_comment_code_block_width/50.rs
Original file line number Diff line number Diff line change
@@ -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() )
}
}
16 changes: 16 additions & 0 deletions tests/target/configs/doc_comment_code_block_width/100.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -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(),
)
}
}
22 changes: 22 additions & 0 deletions tests/target/configs/doc_comment_code_block_width/50.rs
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 33c6074

Please sign in to comment.