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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 11 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

0 commit comments

Comments
 (0)