Skip to content

Commit

Permalink
Merge pull request rust-lang#3535 from xiongmao86/issue3417
Browse files Browse the repository at this point in the history
Try to solve issue 3417.
  • Loading branch information
topecongiro authored May 16, 2019
2 parents 0101e2b + a9932f6 commit 531b2d9
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

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

Expand Down
4 changes: 2 additions & 2 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1978,9 +1978,9 @@ fn main() {
}
```

## `format_doc_comments`
## `format_code_in_doc_comments`

Format doc comments.
Format code snippet included in doc comments.

- **Default value**: `false`
- **Possible values**: `true`, `false`
Expand Down
15 changes: 11 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn identify_comment(
trim_left_preserve_layout(first_group, shape.indent, config)?
} else if !config.normalize_comments()
&& !config.wrap_comments()
&& !config.format_doc_comments()
&& !config.format_code_in_doc_comments()
{
light_rewrite_comment(first_group, shape.indent, config, is_doc_comment)
} else {
Expand Down Expand Up @@ -656,9 +656,16 @@ impl<'a> CommentRewrite<'a> {
_ => {
let mut config = self.fmt.config.clone();
config.set().wrap_comments(false);
match crate::format_code_block(&self.code_block_buffer, &config) {
Some(ref s) => trim_custom_comment_prefix(&s.snippet),
None => trim_custom_comment_prefix(&self.code_block_buffer),
if config.format_code_in_doc_comments() {
if let Some(s) =
crate::format_code_block(&self.code_block_buffer, &config)
{
trim_custom_comment_prefix(&s.snippet)
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
}
} else {
trim_custom_comment_prefix(&self.code_block_buffer)
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ create_config! {

// Comments. macros, and strings
wrap_comments: bool, false, false, "Break comments to fit on the line";
format_doc_comments: bool, false, false, "Format doc comments.";
format_code_in_doc_comments: bool, false, false, "Format the code snippet in doc comments.";
comment_width: usize, 80, false,
"Maximum length of comments. No effect unless wrap_comments = true";
normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";
Expand Down
2 changes: 1 addition & 1 deletion tests/source/doc-comment-with-example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-format_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// Foo
///
Expand Down
2 changes: 1 addition & 1 deletion tests/source/invalid-rust-code-in-doc-comment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-format_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// ```rust
/// if (true) { … }
Expand Down
1 change: 1 addition & 0 deletions tests/source/issue-2520.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

//! ```rust
//! println!( "hello, world" );
Expand Down
1 change: 1 addition & 0 deletions tests/source/issue-2523.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

// Do not unindent macro calls in comment with unformattable syntax.
//! ```rust
Expand Down
1 change: 1 addition & 0 deletions tests/source/issue-3055/original.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_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
1 change: 1 addition & 0 deletions tests/source/itemized-blocks/no_wrap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

//! This is a list:
//! * Outer
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-format_code_in_doc_comments: true
// rustfmt-max_width: 50

//! This is a list:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-normalize_doc_attributes: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() { }
/// ```
///
fn foo() {}

#[doc = "Bar documents"]
fn bar() {}
16 changes: 16 additions & 0 deletions tests/source/wrap_comments_should_not_imply_format_doc_comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-wrap_comments: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() { }
/// ```
///
fn foo() {}

/// A long commment for wrapping
/// This is a long long long long long long long long long long long long long long long long long long long long sentence.
fn bar() {}
2 changes: 1 addition & 1 deletion tests/target/doc-comment-with-example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-format_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// Foo
///
Expand Down
2 changes: 1 addition & 1 deletion tests/target/invalid-rust-code-in-doc-comment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-format_doc_comments: true
// rustfmt-format_code_in_doc_comments: true

/// ```rust
/// if (true) { … }
Expand Down
1 change: 1 addition & 0 deletions tests/target/issue-2520.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

//! ```rust
//! println!("hello, world");
Expand Down
1 change: 1 addition & 0 deletions tests/target/issue-2523.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

// Do not unindent macro calls in comment with unformattable syntax.
//! ```rust
Expand Down
1 change: 1 addition & 0 deletions tests/target/issue-3055/original.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_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
1 change: 1 addition & 0 deletions tests/target/itemized-blocks/no_wrap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-normalize_comments: true
// rustfmt-format_code_in_doc_comments: true

//! This is a list:
//! * Outer
Expand Down
1 change: 1 addition & 0 deletions tests/target/itemized-blocks/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// rustfmt-wrap_comments: true
// rustfmt-format_code_in_doc_comments: true
// rustfmt-max_width: 50

//! This is a list:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-normalize_doc_attributes: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() { }
/// ```
///
fn foo() {}

///Bar documents
fn bar() {}
16 changes: 16 additions & 0 deletions tests/target/wrap_comments_should_not_imply_format_doc_comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-wrap_comments: true

/// Foo
///
/// # Example
/// ```
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
/// # #![cfg_attr(not(dox), no_std)]
/// fn foo() { }
/// ```
fn foo() {}

/// A long commment for wrapping
/// This is a long long long long long long long long long long long long long
/// long long long long long long long sentence.
fn bar() {}

0 comments on commit 531b2d9

Please sign in to comment.