Skip to content

Commit 9849736

Browse files
committed
Split wrap_doc_comments out of wrap_comments
1 parent 1135e56 commit 9849736

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+168
-36
lines changed

Diff for: Configurations.md

+45
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,51 @@ See also [`brace_style`](#brace_style), [`control_brace_style`](#control_brace_s
30743074

30753075
Break comments to fit on the line
30763076

3077+
Note that no wrapping will happen if:
3078+
1. An URL was found in the comment
3079+
3080+
- **Default value**: `false`
3081+
- **Possible values**: `true`, `false`
3082+
- **Stable**: No (tracking issue: [#3347](https://github.com/rust-lang/rustfmt/issues/3347))
3083+
3084+
#### `false` (default):
3085+
3086+
```rust
3087+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
3088+
// sed do eiusmod tempor incididunt ut labore et dolore
3089+
// magna aliqua. Ut enim ad minim veniam, quis nostrud
3090+
// exercitation ullamco laboris nisi ut aliquip ex ea
3091+
// commodo consequat.
3092+
3093+
// 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.
3094+
3095+
// 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.
3096+
struct Foo {}
3097+
```
3098+
3099+
#### `true`:
3100+
3101+
```rust
3102+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
3103+
// sed do eiusmod tempor incididunt ut labore et dolore
3104+
// magna aliqua. Ut enim ad minim veniam, quis nostrud
3105+
// exercitation ullamco laboris nisi ut aliquip ex ea
3106+
// commodo consequat.
3107+
3108+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit,
3109+
// sed do eiusmod tempor incididunt ut labore et dolore
3110+
// magna aliqua. Ut enim ad minim veniam, quis nostrud
3111+
// exercitation ullamco laboris nisi ut aliquip ex ea
3112+
// commodo consequat.
3113+
3114+
// 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.
3115+
struct Foo {}
3116+
```
3117+
3118+
## `wrap_doc_comments`
3119+
3120+
Break comments to fit on the line
3121+
30773122
Note that no wrapping will happen if:
30783123
1. The comment is the start of a markdown header doc comment
30793124
2. An URL was found in the comment

Diff for: src/comment.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,11 @@ fn identify_comment(
364364
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
365365
trim_left_preserve_layout(first_group, shape.indent, config)?
366366
} else if !config.normalize_comments()
367-
&& !config.wrap_comments()
368-
&& !(
369-
// `format_code_in_doc_comments` should only take effect on doc comments,
370-
// so we only consider it when this comment block is a doc comment block.
371-
is_doc_comment && config.format_code_in_doc_comments()
372-
)
367+
&& (if is_doc_comment {
368+
!config.wrap_doc_comments() && !config.format_code_in_doc_comments()
369+
} else {
370+
!config.wrap_comments()
371+
})
373372
{
374373
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
375374
} else {
@@ -774,6 +773,7 @@ impl<'a> CommentRewrite<'a> {
774773
{
775774
let mut config = self.fmt.config.clone();
776775
config.set().wrap_comments(false);
776+
config.set().wrap_doc_comments(false);
777777
let comment_max_width = config
778778
.doc_comment_code_block_width()
779779
.min(config.max_width());
@@ -805,11 +805,17 @@ impl<'a> CommentRewrite<'a> {
805805
return false;
806806
}
807807

808+
let config_wrap_comments = if is_doc_comment {
809+
self.fmt.config.wrap_doc_comments()
810+
} else {
811+
self.fmt.config.wrap_comments()
812+
};
813+
808814
self.code_block_attr = None;
809815
self.item_block = None;
810816
if let Some(stripped) = line.strip_prefix("```") {
811817
self.code_block_attr = Some(CodeBlockAttribute::new(stripped))
812-
} else if self.fmt.config.wrap_comments() {
818+
} else if config_wrap_comments {
813819
if let Some(ib) = ItemizedBlock::new(line) {
814820
self.item_block = Some(ib);
815821
return false;
@@ -848,7 +854,7 @@ impl<'a> CommentRewrite<'a> {
848854
// 4) No URLS were found in the comment
849855
// If this changes, the documentation in ../Configurations.md#wrap_comments
850856
// should be changed accordingly.
851-
let should_wrap_comment = self.fmt.config.wrap_comments()
857+
let should_wrap_comment = config_wrap_comments
852858
&& !is_markdown_header_doc_comment
853859
&& unicode_str_width(line) > self.fmt.shape.width
854860
&& !has_url(line)
@@ -1907,7 +1913,7 @@ mod test {
19071913

19081914
#[test]
19091915
#[rustfmt::skip]
1910-
fn format_doc_comments() {
1916+
fn format_comments() {
19111917
let mut wrap_normalize_config: crate::config::Config = Default::default();
19121918
wrap_normalize_config.set().wrap_comments(true);
19131919
wrap_normalize_config.set().normalize_comments(true);

Diff for: src/config/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ create_config! {
6363
over multiple lines.";
6464

6565
// Comments. macros, and strings
66-
wrap_comments: bool, false, false, "Break comments to fit on the line";
66+
wrap_comments: bool, false, false, "Break non-doc comments to fit on the line";
67+
wrap_doc_comments: bool, false, false, "Break doc comments to fit on the line";
6768
format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments.";
6869
doc_comment_code_block_width: usize, 100, false, "Maximum width for code snippets in doc \
6970
comments. No effect unless format_code_in_doc_comments = true";
@@ -624,6 +625,7 @@ chain_width = 60
624625
single_line_if_else_max_width = 50
625626
single_line_let_else_max_width = 50
626627
wrap_comments = false
628+
wrap_doc_comments = false
627629
format_code_in_doc_comments = false
628630
doc_comment_code_block_width = 100
629631
comment_width = 80

Diff for: tests/source/comment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-wrap_doc_comments: true
34

45
//! Doc comment
56
fn test() {

Diff for: tests/source/comment2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
44
pub mod foo {}

Diff for: tests/source/comment3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly and justly.
44

Diff for: tests/source/configs/struct_field_align_threshold/20.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// rustfmt-struct_field_align_threshold: 20
22
// rustfmt-normalize_comments: true
33
// rustfmt-wrap_comments: true
4+
// rustfmt-wrap_doc_comments: true
45
// rustfmt-error_on_line_overflow: false
56

67
struct Foo {

Diff for: tests/source/configs/wrap_comments/true.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Wrap comments
44

55
fn main() {
6+
//! 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.
7+
68
// 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.
79
}
810

Diff for: tests/source/configs/wrap_doc_comments/false.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_doc_comments: false
2+
// rustfmt-max_width: 50
3+
// rustfmt-error_on_line_overflow: false
4+
//! Wrap comments
5+
6+
fn main() {
7+
//! 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.
8+
}

Diff for: tests/source/configs/wrap_doc_comments/true.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-wrap_doc_comments: true
2+
// rustfmt-max_width: 50
3+
/// Wrap comments
4+
5+
fn main() {
6+
//! 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.
7+
8+
// 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.
9+
}
10+
11+
fn code_block() {
12+
//! ```rust
13+
//! let x = 3;
14+
//!
15+
//! println!("x = {}", x);
16+
//! ```
17+
}

Diff for: tests/source/doc-attrib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// rustfmt-normalize_doc_attributes: true
34

45
// Only doc = "" attributes should be normalized

Diff for: tests/source/enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// Enums test
34

45
#[atrr]

Diff for: tests/source/issue-3055/original.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22
// rustfmt-format_code_in_doc_comments: true
33

44
/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui.

Diff for: tests/source/issue-3059.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22
// rustfmt-max_width: 80
33

44
/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui.

Diff for: tests/source/issue-3153.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// This may panic if:
44
/// - there are fewer than `max_header_bytes` bytes preceding the body

Diff for: tests/source/issue-3787.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
//! URLs in items
44
//! * [This is a link with a very loooooooooooooooooooooooooooooooooooooooooong URL.](https://example.com/This/is/a/link/with/a/very/loooooooooooooooooooooooooooooooooooooooooong/URL)

Diff for: tests/source/issue-4041.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22
//! List:
33
//! - Sub list:
44
//! + very long #1 blah blah blah blah blah blah blah blah blah blah blah blah foo baar baxxxxxxxx long line 1231421230912i3091238192038

Diff for: tests/source/issue-4079.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/*!
44
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia

Diff for: tests/source/issue-5023.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23

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

33
/// > 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.
44
fn block_quote() {}

Diff for: tests/source/issue-5157/nested_itemized_markdown_blockquote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// > 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.
44
///
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// > 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.
44
fn block_quote() {}

Diff for: tests/source/issue-5238/markdown_header_wrap_comments_true.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23

34
/// no markdown header so rustfmt should wrap this comment when `format_code_in_doc_comments = true` and `wrap_comments = true`
45
fn not_documented_with_markdown_header() {

Diff for: tests/source/itemized-blocks/urls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22
// rustfmt-max_width: 79
33

44
//! CMSIS: Cortex Microcontroller Software Interface Standard

Diff for: tests/source/itemized-blocks/wrap.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// rustfmt-format_code_in_doc_comments: true
34
// rustfmt-max_width: 50
45

Diff for: tests/source/soft-wrapping.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// rustfmt-max_width: 80
34
// Soft wrapping for comments.
45

Diff for: tests/source/structs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-wrap_doc_comments: true
34

45
/// A Doc comment
56
#[AnAttribute]

Diff for: tests/source/unions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-wrap_doc_comments: true
34

45
/// A Doc comment
56
#[AnAttribute]

Diff for: tests/source/wrap_comments_should_not_imply_format_doc_comments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// Foo
44
///

Diff for: tests/target/comment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-wrap_doc_comments: true
34

45
//! Doc comment
56
fn test() {

Diff for: tests/target/comment2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
/// This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
44
/// and justly.

Diff for: tests/target/comment3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22

33
//! This is a long line that angers rustfmt. Rustfmt shall deal with it swiftly
44
//! and justly.

Diff for: tests/target/configs/struct_field_align_threshold/20.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// rustfmt-struct_field_align_threshold: 20
22
// rustfmt-normalize_comments: true
33
// rustfmt-wrap_comments: true
4+
// rustfmt-wrap_doc_comments: true
45
// rustfmt-error_on_line_overflow: false
56

67
struct Foo {

Diff for: tests/target/configs/wrap_comments/true.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Wrap comments
44

55
fn main() {
6+
//! 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.
7+
68
// Lorem ipsum dolor sit amet, consectetur
79
// adipiscing elit, sed do eiusmod tempor
810
// incididunt ut labore et dolore magna

Diff for: tests/target/configs/wrap_doc_comments/false.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_doc_comments: false
2+
// rustfmt-max_width: 50
3+
// rustfmt-error_on_line_overflow: false
4+
//! Wrap comments
5+
6+
fn main() {
7+
//! 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.
8+
}

Diff for: tests/target/configs/wrap_doc_comments/true.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// rustfmt-wrap_doc_comments: true
2+
// rustfmt-max_width: 50
3+
/// Wrap comments
4+
5+
fn main() {
6+
//! Lorem ipsum dolor sit amet, consectetur
7+
//! adipiscing elit, sed do eiusmod tempor
8+
//! incididunt ut labore et dolore magna
9+
//! aliqua. Ut enim ad minim veniam, quis
10+
//! nostrud exercitation ullamco laboris nisi
11+
//! ut aliquip ex ea commodo consequat.
12+
13+
// 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.
14+
}
15+
16+
fn code_block() {
17+
//! ```rust
18+
//! let x = 3;
19+
//!
20+
//! println!("x = {}", x);
21+
//! ```
22+
}

Diff for: tests/target/doc-attrib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// rustfmt-normalize_doc_attributes: true
34

45
// Only doc = "" attributes should be normalized

Diff for: tests/target/enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-wrap_comments: true
2+
// rustfmt-wrap_doc_comments: true
23
// Enums test
34

45
#[atrr]

Diff for: tests/target/issue-3055/original.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// rustfmt-wrap_comments: true
1+
// rustfmt-wrap_doc_comments: true
22
// rustfmt-format_code_in_doc_comments: true
33

44
/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc

0 commit comments

Comments
 (0)