Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,41 +1080,47 @@ fn light_rewrite_comment(
.join(&format!("\n{}", offset.to_string(config)))
}

/// Trims comment characters and possibly a single space from the left of a string.
/// Does not trim all whitespace. If a single space is trimmed from the left of the string,
/// this function returns true.
/// Trims the beginning of a comment's opener or line start, leaving the rest untouched.
/// If at least one whitespace is trimmed, the second element of the tuple is true.
/// Will only ever trim one whitespace unless a custom comment style is used.
///
/// **NOTE**: this function assumes the beginning of the line is trimmed.
fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a str, bool) {
if line.starts_with("//! ")
|| line.starts_with("/// ")
|| line.starts_with("/*! ")
|| line.starts_with("/** ")
{
(&line[4..], true)
} else if let CommentStyle::Custom(opener) = *style {
if let Some(stripped) = line.strip_prefix(opener) {
(stripped, true)
} else {
(&line[opener.trim_end().len()..], false)
let opener = style.opener();
match style {
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
if let Some(line) = line.strip_prefix(opener) {
(line, true)
} else if let Some(line) = line.strip_prefix(opener.trim_end()) {
(line, false)
} else {
(line, false)
}
}
CommentStyle::SingleBullet | CommentStyle::DoubleBullet | CommentStyle::Exclamation => {
if let Some(line) = line.strip_prefix(opener) {
(line, true)
} else if let Some(line) = line.strip_prefix(opener.trim_end()) {
(line, false)
} else if let Some(line) = line.strip_prefix(style.line_start()) {
Comment thread
Ripper53 marked this conversation as resolved.
(line, true)
} else if let Some(line) = line.strip_prefix(style.line_start().trim_start()) {
(line, true)
} else if let Some(line) = line.strip_prefix(style.line_start().trim()) {
(line, false)
} else {
(line, false)
}
}
CommentStyle::Custom(_) => {
if let Some(line) = line.strip_prefix(opener) {
(line, opener.ends_with(' '))
} else if let Some(line) = line.strip_prefix(opener.trim_end()) {
(line, false)
} else {
(line, false)
}
}
} else if line.starts_with("/* ")
|| line.starts_with("// ")
|| line.starts_with("//!")
|| line.starts_with("///")
|| line.starts_with("** ")
|| line.starts_with("/*!")
|| (line.starts_with("/**") && !line.starts_with("/**/"))
{
(&line[3..], line.chars().nth(2).unwrap() == ' ')
} else if line.starts_with("/*")
|| line.starts_with("* ")
|| line.starts_with("//")
|| line.starts_with("**")
{
(&line[2..], line.chars().nth(1).unwrap() == ' ')
} else if let Some(stripped) = line.strip_prefix('*') {
(stripped, false)
} else {
(line, line.starts_with(' '))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// rustfmt-normalize_comments: true

/*!
* ```
* // foo
* ```
*/

/**
* ```
* // bar
* ```
*/
struct Bar;

/*
* ```
* // baz
* ```
*/

/*
*
* boop
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// rustfmt-normalize_comments: true

/*!
```
// foo
/// BAR
```
*/

/**
// MEOW
```
// bar
```
*/
struct Bar;

/*
```
// baz
/// FOO
```
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rustfmt-normalize_comments: true

//! ```
//! // foo
//! ```

/// ```
/// // bar
/// ```
struct Bar;

// ```
// // baz
// ```

// boop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// rustfmt-normalize_comments: true

//! ```
//! // foo
//! /// BAR
//! ```
/// // MEOW
/// ```
/// // bar
/// ```
struct Bar;

// ```
// // baz
// /// FOO
// ```
Loading