-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partition wrap_comments
into normal
or doc
settings
#5943
base: master
Are you sure you want to change the base?
Changes from 1 commit
3454812
21719e2
d64fb77
0111775
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3096,11 +3096,11 @@ Note that no wrapping will happen if: | |
1. The comment is the start of a markdown header doc comment | ||
2. An URL was found in the comment | ||
|
||
- **Default value**: `false` | ||
- **Possible values**: `true`, `false` | ||
- **Default value**: `"off"` | ||
- **Possible values**: `"doc"`, `"normal"`, `"all"` (alias `true`), `"off"` (alias `false`) | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **Stable**: No (tracking issue: [#3347](https://github.com/rust-lang/rustfmt/issues/3347)) | ||
|
||
#### `false` (default): | ||
#### `"off"` (default): | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, | ||
|
@@ -3112,12 +3112,10 @@ Note that no wrapping will happen if: | |
// 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. | ||
|
||
// 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. | ||
|
||
/// # This doc comment is a very long header (it starts with a '#'). Had it not been a header it would have been wrapped. But because it is a header, it will not be. That is because wrapping a markdown header breaks it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with removing this from the example code, but I'd like to preserve this information by adding it to the note above. I'm thinking something like this: Break comments to fit on the line
Note that no wrapping will happen if:
1. The comment is the start of a markdown header doc comment. For example:
```rust
/// # This doc comment is a markdown header
```
3. An URL was found in the comment |
||
struct Foo {} | ||
``` | ||
|
||
#### `true`: | ||
#### `"all"`: | ||
|
||
```rust | ||
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, | ||
|
@@ -3133,8 +3131,6 @@ struct Foo {} | |
// commodo consequat. | ||
|
||
// 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. | ||
|
||
/// # This doc comment is a very long header (it starts with a '#'). Had it not been a header it would have been wrapped. But because it is a header, it will not be. That is because wrapping a markdown header breaks it. | ||
struct Foo {} | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,97 @@ pub enum Verbosity { | |
Quiet, | ||
} | ||
|
||
/// Which comments to wrap | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)] | ||
pub enum WrapComments { | ||
/// Don't wrap comments | ||
Off, | ||
/// Wrap all kinds of comments | ||
All, | ||
/// Only wrap doc comments | ||
Doc, | ||
/// Only wrap normal comments | ||
Normal, | ||
} | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl WrapComments { | ||
pub(crate) fn is_normal(self) -> bool { | ||
matches!(self, WrapComments::All | WrapComments::Normal) | ||
} | ||
|
||
pub(crate) fn is_doc(self) -> bool { | ||
matches!(self, WrapComments::All | WrapComments::Doc) | ||
} | ||
} | ||
|
||
impl fmt::Display for WrapComments { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
WrapComments::Off => f.write_str("Off"), | ||
WrapComments::All => f.write_str("All"), | ||
WrapComments::Doc => f.write_str("Doc"), | ||
WrapComments::Normal => f.write_str("Normal"), | ||
} | ||
} | ||
} | ||
|
||
impl super::ConfigType for WrapComments { | ||
fn doc_hint() -> String { | ||
"[Off|All|Doc|Normal]".to_owned() | ||
} | ||
|
||
fn stable_variant(&self) -> bool { | ||
true | ||
} | ||
} | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
impl std::str::FromStr for WrapComments { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.to_lowercase().as_str() { | ||
"off" | "false" => Ok(WrapComments::Off), | ||
"all" | "true" => Ok(WrapComments::All), | ||
"doc" => Ok(WrapComments::Doc), | ||
"normal" => Ok(WrapComments::Normal), | ||
_ => Err("Bad variant, expected one of: `Off` `All` `Doc` `Normal`"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think It also makes sense to mention |
||
} | ||
} | ||
} | ||
|
||
impl<'de> serde::de::Deserialize<'de> for WrapComments { | ||
fn deserialize<D>(d: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
use serde::de::Error; | ||
|
||
struct StringOrBoolVisitor; | ||
|
||
impl<'de> Visitor<'de> for StringOrBoolVisitor { | ||
type Value = String; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
formatter.write_str("string") | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<String, E> { | ||
Ok(String::from(value)) | ||
} | ||
|
||
fn visit_bool<E>(self, value: bool) -> Result<String, E> { | ||
Ok(value.to_string()) | ||
} | ||
} | ||
|
||
let s = d.deserialize_string(StringOrBoolVisitor)?; | ||
s.parse().map_err(|_| { | ||
static ALLOWED: &'static [&str] = &["Off", "All", "Doc", "Normal"]; | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
D::Error::unknown_variant(&s, ALLOWED) | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] | ||
pub struct WidthHeuristics { | ||
// Maximum width of the args of a function call before falling back | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// rustfmt-wrap_comments: all | ||
ytmimi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// rustfmt-max_width: 50 | ||
// Wrap comments | ||
|
||
fn main() { | ||
//! 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. | ||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
// ```rust | ||
// let x = 3; | ||
// | ||
// println!("x = {}", x); | ||
// ``` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// rustfmt-wrap_comments: doc | ||
// rustfmt-max_width: 50 | ||
/// Wrap comments | ||
fn main() { | ||
//! 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. | ||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
// ```rust | ||
// let x = 3; | ||
// | ||
// println!("x = {}", x); | ||
// ``` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// rustfmt-wrap_comments: normal | ||
// rustfmt-max_width: 50 | ||
// rustfmt-error_on_line_overflow: false | ||
// Wrap comments | ||
|
||
fn main() { | ||
//! 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. | ||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
//! ```rust | ||
//! let x = 3; | ||
//! | ||
//! println!("x = {}", x); | ||
//! ``` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// rustfmt-wrap_comments: off | ||
// rustfmt-max_width: 50 | ||
// rustfmt-error_on_line_overflow: false | ||
// Wrap comments | ||
|
||
fn main() { | ||
//! 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. | ||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
//! ```rust | ||
//! let x = 3; | ||
//! | ||
//! println!("x = {}", x); | ||
//! ``` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// rustfmt-wrap_comments: all | ||
// rustfmt-max_width: 50 | ||
// Wrap comments | ||
|
||
fn main() { | ||
//! 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. | ||
|
||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
// ```rust | ||
// let x = 3; | ||
// | ||
// println!("x = {}", x); | ||
// ``` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// rustfmt-wrap_comments: doc | ||
// rustfmt-max_width: 50 | ||
/// Wrap comments | ||
|
||
fn main() { | ||
//! 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. | ||
|
||
// 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. | ||
} | ||
|
||
fn code_block() { | ||
// ```rust | ||
// let x = 3; | ||
// | ||
// println!("x = {}", x); | ||
// ``` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Let's stay consistent with the capitalization.