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

Commit f23e6aa

Browse files
committed
fix the identification of a block comment.
Block comments like below were not properly supported: /* something here but it doesn't start with a star */ because of the line that didn't start with a star.
1 parent 7c1ad96 commit f23e6aa

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed

src/comment.rs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ impl<'a> CommentStyle<'a> {
9696
pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) {
9797
(self.opener(), self.closer(), self.line_start())
9898
}
99-
100-
pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool {
101-
match *self {
102-
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
103-
line.trim_left().starts_with(self.line_start().trim_left())
104-
|| comment_style(line, normalize_comments) == *self
105-
}
106-
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
107-
line.trim_left().starts_with(self.closer().trim_left())
108-
|| line.trim_left().starts_with(self.line_start().trim_left())
109-
|| comment_style(line, normalize_comments) == *self
110-
}
111-
CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()),
112-
}
113-
}
11499
}
115100

116101
fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle {
@@ -273,19 +258,56 @@ fn identify_comment(
273258
is_doc_comment: bool,
274259
) -> Option<String> {
275260
let style = comment_style(orig, false);
276-
let first_group = orig
277-
.lines()
278-
.take_while(|l| style.line_with_same_comment_style(l, false))
279-
.collect::<Vec<_>>()
280-
.join("\n");
281-
let rest = orig
282-
.lines()
283-
.skip(first_group.lines().count())
284-
.collect::<Vec<_>>()
285-
.join("\n");
261+
let mut first_group_ending = 0;
262+
263+
fn compute_len(orig: &str, line: &str) -> usize {
264+
if orig.len() > line.len() {
265+
if orig.as_bytes()[line.len()] == b'\r' {
266+
line.len() + 2
267+
} else {
268+
line.len() + 1
269+
}
270+
} else {
271+
line.len()
272+
}
273+
}
274+
275+
match style {
276+
CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => {
277+
let line_start = style.line_start().trim_left();
278+
for line in orig.lines() {
279+
if line.trim_left().starts_with(line_start) || comment_style(line, false) == style {
280+
first_group_ending += compute_len(&orig[first_group_ending..], line);
281+
} else {
282+
break;
283+
}
284+
}
285+
}
286+
CommentStyle::Custom(opener) => {
287+
let trimmed_opener = opener.trim_right();
288+
for line in orig.lines() {
289+
if line.trim_left().starts_with(trimmed_opener) {
290+
first_group_ending += compute_len(&orig[first_group_ending..], line);
291+
} else {
292+
break;
293+
}
294+
}
295+
}
296+
// for a block comment, search for the closing symbol
297+
CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => {
298+
let closer = style.closer().trim_left();
299+
for line in orig.lines() {
300+
first_group_ending += compute_len(&orig[first_group_ending..], line);
301+
if line.trim_left().ends_with(closer) {
302+
break;
303+
}
304+
}
305+
}
306+
}
286307

308+
let (first_group, rest) = orig.split_at(first_group_ending);
287309
let first_group_str = rewrite_comment_inner(
288-
&first_group,
310+
first_group,
289311
block_style,
290312
style,
291313
shape,
@@ -295,7 +317,7 @@ fn identify_comment(
295317
if rest.is_empty() {
296318
Some(first_group_str)
297319
} else {
298-
identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
320+
identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| {
299321
format!(
300322
"{}\n{}{}",
301323
first_group_str,

tests/source/issue-539.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-normalize_comments: true
2+
/*
3+
FIXME (#3300): Should allow items to be anonymous. Right now
4+
we just use dummy names for anon items.
5+
*/

tests/source/issue-683.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-normalize_comments: true
2+
/*
3+
* FIXME (#3300): Should allow items to be anonymous. Right now
4+
* we just use dummy names for anon items.
5+
*/

tests/target/issue-539.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// rustfmt-normalize_comments: true
2+
// FIXME (#3300): Should allow items to be anonymous. Right now
3+
// we just use dummy names for anon items.

tests/target/issue-683.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// rustfmt-normalize_comments: true
2+
// FIXME (#3300): Should allow items to be anonymous. Right now
3+
// we just use dummy names for anon items.

0 commit comments

Comments
 (0)