Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add width for codeblocks in comments #5372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to include a link to the format_code_in_doc_comments option, similar to what's done for other options https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#array_width

What do you think?

## `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())
}
}