Skip to content

Commit

Permalink
Split wrap_doc_comments out of wrap_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Oct 22, 2023
1 parent 1135e56 commit 9849736
Show file tree
Hide file tree
Showing 55 changed files with 168 additions and 36 deletions.
45 changes: 45 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3074,6 +3074,51 @@ See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_s

Break comments to fit on the line

Note that no wrapping will happen if:
1. An URL was found in the comment

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#3347](https://github.com/rust-lang/rustfmt/issues/3347))

#### `false` (default):

```rust
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
// sed do eiusmod tempor incididunt ut labore et dolore
// magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea
// commodo consequat.

// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

// Information on the lorem ipsum can be found at the following url: https://en.wikipedia.org/wiki/Lorem_ipsum. Its text is: lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
struct Foo {}
```

#### `true`:

```rust
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
// sed do eiusmod tempor incididunt ut labore et dolore
// magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea
// commodo consequat.

// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
// sed do eiusmod tempor incididunt ut labore et dolore
// magna aliqua. Ut enim ad minim veniam, quis nostrud
// exercitation ullamco laboris nisi ut aliquip ex ea
// commodo consequat.

// Information on the lorem ipsum can be found at the following url: https://en.wikipedia.org/wiki/Lorem_ipsum. Its text is: lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
struct Foo {}
```

## `wrap_doc_comments`

Break comments to fit on the line

Note that no wrapping will happen if:
1. The comment is the start of a markdown header doc comment
2. An URL was found in the comment
Expand Down
24 changes: 15 additions & 9 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,11 @@ fn identify_comment(
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
trim_left_preserve_layout(first_group, shape.indent, config)?
} else if !config.normalize_comments()
&& !config.wrap_comments()
&& !(
// `format_code_in_doc_comments` should only take effect on doc comments,
// so we only consider it when this comment block is a doc comment block.
is_doc_comment && config.format_code_in_doc_comments()
)
&& (if is_doc_comment {
!config.wrap_doc_comments() && !config.format_code_in_doc_comments()
} else {
!config.wrap_comments()
})
{
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
} else {
Expand Down Expand Up @@ -774,6 +773,7 @@ impl<'a> CommentRewrite<'a> {
{
let mut config = self.fmt.config.clone();
config.set().wrap_comments(false);
config.set().wrap_doc_comments(false);
let comment_max_width = config
.doc_comment_code_block_width()
.min(config.max_width());
Expand Down Expand Up @@ -805,11 +805,17 @@ impl<'a> CommentRewrite<'a> {
return false;
}

let config_wrap_comments = if is_doc_comment {
self.fmt.config.wrap_doc_comments()
} else {
self.fmt.config.wrap_comments()
};

self.code_block_attr = None;
self.item_block = None;
if let Some(stripped) = line.strip_prefix("```") {
self.code_block_attr = Some(CodeBlockAttribute::new(stripped))
} else if self.fmt.config.wrap_comments() {
} else if config_wrap_comments {
if let Some(ib) = ItemizedBlock::new(line) {
self.item_block = Some(ib);
return false;
Expand Down Expand Up @@ -848,7 +854,7 @@ impl<'a> CommentRewrite<'a> {
// 4) No URLS were found in the comment
// If this changes, the documentation in ../Configurations.md#wrap_comments
// should be changed accordingly.
let should_wrap_comment = self.fmt.config.wrap_comments()
let should_wrap_comment = config_wrap_comments
&& !is_markdown_header_doc_comment
&& unicode_str_width(line) > self.fmt.shape.width
&& !has_url(line)
Expand Down Expand Up @@ -1907,7 +1913,7 @@ mod test {

#[test]
#[rustfmt::skip]
fn format_doc_comments() {
fn format_comments() {
let mut wrap_normalize_config: crate::config::Config = Default::default();
wrap_normalize_config.set().wrap_comments(true);
wrap_normalize_config.set().normalize_comments(true);
Expand Down
4 changes: 3 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ create_config! {
over multiple lines.";

// Comments. macros, and strings
wrap_comments: bool, false, false, "Break comments to fit on the line";
wrap_comments: bool, false, false, "Break non-doc comments to fit on the line";
wrap_doc_comments: bool, false, false, "Break doc 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";
Expand Down Expand Up @@ -624,6 +625,7 @@ chain_width = 60
single_line_if_else_max_width = 50
single_line_let_else_max_width = 50
wrap_comments = false
wrap_doc_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
comment_width = 80
Expand Down
1 change: 1 addition & 0 deletions tests/source/comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

//! Doc comment
fn test() {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/comment2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
pub mod foo {}
2 changes: 1 addition & 1 deletion tests/source/comment3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
Expand Down
1 change: 1 addition & 0 deletions tests/source/configs/struct_field_align_threshold/20.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// rustfmt-struct_field_align_threshold: 20
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-error_on_line_overflow: false

struct Foo {
Expand Down
2 changes: 2 additions & 0 deletions tests/source/configs/wrap_comments/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Wrap comments

fn main() {
//! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}

Expand Down
8 changes: 8 additions & 0 deletions tests/source/configs/wrap_doc_comments/false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_doc_comments: false
// rustfmt-max_width: 50
// rustfmt-error_on_line_overflow: false
//! Wrap comments
fn main() {
//! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}
17 changes: 17 additions & 0 deletions tests/source/configs/wrap_doc_comments/true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-wrap_doc_comments: true
// rustfmt-max_width: 50
/// Wrap comments
fn main() {
//! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}

fn code_block() {
//! ```rust
//! let x = 3;
//!
//! println!("x = {}", x);
//! ```
}
1 change: 1 addition & 0 deletions tests/source/doc-attrib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-normalize_doc_attributes: true

// Only doc = "" attributes should be normalized
Expand Down
1 change: 1 addition & 0 deletions tests/source/enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// Enums test

#[atrr]
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-3055/original.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui.
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-3059.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-max_width: 80

/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui.
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-3153.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// This may panic if:
/// - there are fewer than `max_header_bytes` bytes preceding the body
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-3787.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

//! URLs in items
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-4041.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
//! List:
//! - Sub list:
//! + very long #1 blah blah blah blah blah blah blah blah blah blah blah blah foo baar baxxxxxxxx long line 1231421230912i3091238192038
Expand Down
2 changes: 1 addition & 1 deletion tests/source/issue-4079.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/*!
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia
Expand Down
1 change: 1 addition & 0 deletions tests/source/issue-5023.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// A comment to test special unicode characters on boundaries
/// 是,是,是,是,是,是,是,是,是,是,是,是 it should break right here this goes to the next line
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// > For each sample received, the middleware internally maintains a sample_state relative to each DataReader. The sample_state can either be READ or NOT_READ.
fn block_quote() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// > For each sample received, the middleware internally maintains a sample_state relative to each DataReader. The sample_state can either be READ or NOT_READ.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// > For each sample received, the middleware internally maintains a sample_state relative to each DataReader. The sample_state can either be READ or NOT_READ.
fn block_quote() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
fn not_documented_with_markdown_header() {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/itemized-blocks/urls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-max_width: 79

//! CMSIS: Cortex Microcontroller Software Interface Standard
Expand Down
1 change: 1 addition & 0 deletions tests/source/itemized-blocks/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-format_code_in_doc_comments: true
// rustfmt-max_width: 50

Expand Down
1 change: 1 addition & 0 deletions tests/source/soft-wrapping.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-max_width: 80
// Soft wrapping for comments.

Expand Down
1 change: 1 addition & 0 deletions tests/source/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// A Doc comment
#[AnAttribute]
Expand Down
1 change: 1 addition & 0 deletions tests/source/unions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// A Doc comment
#[AnAttribute]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// Foo
///
Expand Down
1 change: 1 addition & 0 deletions tests/target/comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

//! Doc comment
fn test() {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/comment2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
/// and justly.
Expand Down
2 changes: 1 addition & 1 deletion tests/target/comment3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true

//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
//! and justly.
Expand Down
1 change: 1 addition & 0 deletions tests/target/configs/struct_field_align_threshold/20.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// rustfmt-struct_field_align_threshold: 20
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-error_on_line_overflow: false

struct Foo {
Expand Down
2 changes: 2 additions & 0 deletions tests/target/configs/wrap_comments/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Wrap comments

fn main() {
//! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Lorem ipsum dolor sit amet, consectetur
// adipiscing elit, sed do eiusmod tempor
// incididunt ut labore et dolore magna
Expand Down
8 changes: 8 additions & 0 deletions tests/target/configs/wrap_doc_comments/false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-wrap_doc_comments: false
// rustfmt-max_width: 50
// rustfmt-error_on_line_overflow: false
//! Wrap comments
fn main() {
//! Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}
22 changes: 22 additions & 0 deletions tests/target/configs/wrap_doc_comments/true.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// rustfmt-wrap_doc_comments: true
// rustfmt-max_width: 50
/// Wrap comments
fn main() {
//! Lorem ipsum dolor sit amet, consectetur
//! adipiscing elit, sed do eiusmod tempor
//! incididunt ut labore et dolore magna
//! aliqua. Ut enim ad minim veniam, quis
//! nostrud exercitation ullamco laboris nisi
//! ut aliquip ex ea commodo consequat.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}

fn code_block() {
//! ```rust
//! let x = 3;
//!
//! println!("x = {}", x);
//! ```
}
1 change: 1 addition & 0 deletions tests/target/doc-attrib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-normalize_doc_attributes: true

// Only doc = "" attributes should be normalized
Expand Down
1 change: 1 addition & 0 deletions tests/target/enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// Enums test

#[atrr]
Expand Down
2 changes: 1 addition & 1 deletion tests/target/issue-3055/original.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-wrap_comments: true
// rustfmt-wrap_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc
Expand Down
Loading

0 comments on commit 9849736

Please sign in to comment.