Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b82949b

Browse files
authored
Merge pull request rust-lang#3089 from topecongiro/format-comment
Add format_doc_comments
2 parents d74947c + b2de574 commit b82949b

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

Configurations.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,56 @@ fn main() {
19791979
}
19801980
```
19811981

1982+
## `format_doc_comments`
1983+
1984+
Format doc comments.
1985+
1986+
- **Default value**: `false`
1987+
- **Possible values**: `true`, `false`
1988+
- **Stable**: No
1989+
1990+
#### `false` (default):
1991+
1992+
```rust
1993+
/// Adds one to the number given.
1994+
///
1995+
/// # Examples
1996+
///
1997+
/// ```rust
1998+
/// let five=5;
1999+
///
2000+
/// assert_eq!(
2001+
/// 6,
2002+
/// add_one(5)
2003+
/// );
2004+
/// # fn add_one(x: i32) -> i32 {
2005+
/// # x + 1
2006+
/// # }
2007+
/// ```
2008+
fn add_one(x: i32) -> i32 {
2009+
x + 1
2010+
}
2011+
```
2012+
2013+
#### `true`
2014+
2015+
```rust
2016+
/// Adds one to the number given.
2017+
///
2018+
/// # Examples
2019+
///
2020+
/// ```rust
2021+
/// let five = 5;
2022+
///
2023+
/// assert_eq!(6, add_one(5));
2024+
/// # fn add_one(x: i32) -> i32 {
2025+
/// # x + 1
2026+
/// # }
2027+
/// ```
2028+
fn add_one(x: i32) -> i32 {
2029+
x + 1
2030+
}
2031+
```
19822032

19832033
## `wrap_comments`
19842034

src/comment.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,10 @@ fn identify_comment(
333333
let rewritten_first_group =
334334
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
335335
light_rewrite_block_comment_with_bare_lines(first_group, shape, config)?
336-
} else if !config.normalize_comments() && !config.wrap_comments() {
336+
} else if !config.normalize_comments()
337+
&& !config.wrap_comments()
338+
&& !config.format_doc_comments()
339+
{
337340
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)?
338341
} else {
339342
rewrite_comment_inner(
@@ -593,7 +596,7 @@ fn rewrite_comment_inner(
593596
_ if code_block_buffer.is_empty() => String::new(),
594597
_ => {
595598
let mut config = config.clone();
596-
config.set().wrap_comments(false);
599+
config.set().format_doc_comments(false);
597600
match ::format_code_block(&code_block_buffer, &config) {
598601
Some(ref s) => trim_custom_comment_prefix(s),
599602
None => trim_custom_comment_prefix(&code_block_buffer),
@@ -1622,7 +1625,7 @@ mod test {
16221625

16231626
#[test]
16241627
#[rustfmt::skip]
1625-
fn format_comments() {
1628+
fn format_doc_comments() {
16261629
let mut wrap_normalize_config: ::config::Config = Default::default();
16271630
wrap_normalize_config.set().wrap_comments(true);
16281631
wrap_normalize_config.set().normalize_comments(true);

src/config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ create_config! {
4646

4747
// Comments. macros, and strings
4848
wrap_comments: bool, false, false, "Break comments to fit on the line";
49+
format_doc_comments: bool, false, false, "Format doc comments.";
4950
comment_width: usize, 80, false,
5051
"Maximum length of comments. No effect unless wrap_comments = true";
5152
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-wrap_comments: true
1+
// rustfmt-format_doc_comments: true
22

33
/// Foo
44
///

tests/target/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-wrap_comments: true
1+
// rustfmt-format_doc_comments: true
22

33
/// Foo
44
///

0 commit comments

Comments
 (0)