Skip to content

Commit 531b2d9

Browse files
authored
Merge pull request #3535 from xiongmao86/issue3417
Try to solve issue 3417.
2 parents 0101e2b + a9932f6 commit 531b2d9

22 files changed

+91
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- Change option `format_doc_comment` to `format_code_in_doc_comment`.
56
- `use_small_heuristics` changed to be an enum and stabilised. Configuration
67
options are now ready for 1.0.
78

Configurations.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1978,9 +1978,9 @@ fn main() {
19781978
}
19791979
```
19801980

1981-
## `format_doc_comments`
1981+
## `format_code_in_doc_comments`
19821982

1983-
Format doc comments.
1983+
Format code snippet included in doc comments.
19841984

19851985
- **Default value**: `false`
19861986
- **Possible values**: `true`, `false`

src/comment.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn identify_comment(
353353
trim_left_preserve_layout(first_group, shape.indent, config)?
354354
} else if !config.normalize_comments()
355355
&& !config.wrap_comments()
356-
&& !config.format_doc_comments()
356+
&& !config.format_code_in_doc_comments()
357357
{
358358
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
359359
} else {
@@ -656,9 +656,16 @@ impl<'a> CommentRewrite<'a> {
656656
_ => {
657657
let mut config = self.fmt.config.clone();
658658
config.set().wrap_comments(false);
659-
match crate::format_code_block(&self.code_block_buffer, &config) {
660-
Some(ref s) => trim_custom_comment_prefix(&s.snippet),
661-
None => trim_custom_comment_prefix(&self.code_block_buffer),
659+
if config.format_code_in_doc_comments() {
660+
if let Some(s) =
661+
crate::format_code_block(&self.code_block_buffer, &config)
662+
{
663+
trim_custom_comment_prefix(&s.snippet)
664+
} else {
665+
trim_custom_comment_prefix(&self.code_block_buffer)
666+
}
667+
} else {
668+
trim_custom_comment_prefix(&self.code_block_buffer)
662669
}
663670
}
664671
};

src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ create_config! {
4040

4141
// Comments. macros, and strings
4242
wrap_comments: bool, false, false, "Break comments to fit on the line";
43-
format_doc_comments: bool, false, false, "Format doc comments.";
43+
format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments.";
4444
comment_width: usize, 80, false,
4545
"Maximum length of comments. No effect unless wrap_comments = true";
4646
normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";

tests/source/doc-comment-with-example.rs

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

33
/// Foo
44
///

tests/source/invalid-rust-code-in-doc-comment.rs

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

33
/// ```rust
44
/// if (true) { … }

tests/source/issue-2520.rs

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

34
//! ```rust
45
//! println!( "hello, world" );

tests/source/issue-2523.rs

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

34
// Do not unindent macro calls in comment with unformattable syntax.
45
//! ```rust

tests/source/issue-3055/original.rs

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

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

tests/source/itemized-blocks/no_wrap.rs

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

34
//! This is a list:
45
//! * Outer

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-format_code_in_doc_comments: true
23
// rustfmt-max_width: 50
34

45
//! This is a list:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-normalize_doc_attributes: true
2+
3+
/// Foo
4+
///
5+
/// # Example
6+
/// ```
7+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
8+
/// # #![cfg_attr(not(dox), no_std)]
9+
/// fn foo() { }
10+
/// ```
11+
///
12+
fn foo() {}
13+
14+
#[doc = "Bar documents"]
15+
fn bar() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// Foo
4+
///
5+
/// # Example
6+
/// ```
7+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
8+
/// # #![cfg_attr(not(dox), no_std)]
9+
/// fn foo() { }
10+
/// ```
11+
///
12+
fn foo() {}
13+
14+
/// A long commment for wrapping
15+
/// This is a long long long long long long long long long long long long long long long long long long long long sentence.
16+
fn bar() {}

tests/target/doc-comment-with-example.rs

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

33
/// Foo
44
///

tests/target/invalid-rust-code-in-doc-comment.rs

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

33
/// ```rust
44
/// if (true) { … }

tests/target/issue-2520.rs

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

34
//! ```rust
45
//! println!("hello, world");

tests/target/issue-2523.rs

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

34
// Do not unindent macro calls in comment with unformattable syntax.
45
//! ```rust

tests/target/issue-3055/original.rs

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

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

tests/target/itemized-blocks/no_wrap.rs

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

34
//! This is a list:
45
//! * Outer

tests/target/itemized-blocks/wrap.rs

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

45
//! This is a list:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-normalize_doc_attributes: true
2+
3+
/// Foo
4+
///
5+
/// # Example
6+
/// ```
7+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
8+
/// # #![cfg_attr(not(dox), no_std)]
9+
/// fn foo() { }
10+
/// ```
11+
///
12+
fn foo() {}
13+
14+
///Bar documents
15+
fn bar() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// Foo
4+
///
5+
/// # Example
6+
/// ```
7+
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
8+
/// # #![cfg_attr(not(dox), no_std)]
9+
/// fn foo() { }
10+
/// ```
11+
fn foo() {}
12+
13+
/// A long commment for wrapping
14+
/// This is a long long long long long long long long long long long long long
15+
/// long long long long long long long sentence.
16+
fn bar() {}

0 commit comments

Comments
 (0)