diff --git a/src/comment.rs b/src/comment.rs index d979bf5c4b77..e9d91818d0dd 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -96,21 +96,6 @@ impl<'a> CommentStyle<'a> { pub fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) { (self.opener(), self.closer(), self.line_start()) } - - pub fn line_with_same_comment_style(&self, line: &str, normalize_comments: bool) -> bool { - match *self { - CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => { - line.trim_left().starts_with(self.line_start().trim_left()) - || comment_style(line, normalize_comments) == *self - } - CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => { - line.trim_left().starts_with(self.closer().trim_left()) - || line.trim_left().starts_with(self.line_start().trim_left()) - || comment_style(line, normalize_comments) == *self - } - CommentStyle::Custom(opener) => line.trim_left().starts_with(opener.trim_right()), - } - } } fn comment_style(orig: &str, normalize_comments: bool) -> CommentStyle { @@ -273,19 +258,56 @@ fn identify_comment( is_doc_comment: bool, ) -> Option { let style = comment_style(orig, false); - let first_group = orig - .lines() - .take_while(|l| style.line_with_same_comment_style(l, false)) - .collect::>() - .join("\n"); - let rest = orig - .lines() - .skip(first_group.lines().count()) - .collect::>() - .join("\n"); + let mut first_group_ending = 0; + fn compute_len(orig: &str, line: &str) -> usize { + if orig.len() > line.len() { + if orig.as_bytes()[line.len()] == b'\r' { + line.len() + 2 + } else { + line.len() + 1 + } + } else { + line.len() + } + } + + match style { + CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => { + let line_start = style.line_start().trim_left(); + for line in orig.lines() { + if line.trim_left().starts_with(line_start) || comment_style(line, false) == style { + first_group_ending += compute_len(&orig[first_group_ending..], line); + } else { + break; + } + } + } + CommentStyle::Custom(opener) => { + let trimmed_opener = opener.trim_right(); + for line in orig.lines() { + if line.trim_left().starts_with(trimmed_opener) { + first_group_ending += compute_len(&orig[first_group_ending..], line); + } else { + break; + } + } + } + // for a block comment, search for the closing symbol + CommentStyle::DoubleBullet | CommentStyle::SingleBullet | CommentStyle::Exclamation => { + let closer = style.closer().trim_left(); + for line in orig.lines() { + first_group_ending += compute_len(&orig[first_group_ending..], line); + if line.trim_left().ends_with(closer) { + break; + } + } + } + } + + let (first_group, rest) = orig.split_at(first_group_ending); let first_group_str = rewrite_comment_inner( - &first_group, + first_group, block_style, style, shape, @@ -295,7 +317,7 @@ fn identify_comment( if rest.is_empty() { Some(first_group_str) } else { - identify_comment(&rest, block_style, shape, config, is_doc_comment).map(|rest_str| { + identify_comment(rest, block_style, shape, config, is_doc_comment).map(|rest_str| { format!( "{}\n{}{}", first_group_str, diff --git a/tests/source/issue-539.rs b/tests/source/issue-539.rs new file mode 100644 index 000000000000..d70682e3bee4 --- /dev/null +++ b/tests/source/issue-539.rs @@ -0,0 +1,5 @@ +// rustfmt-normalize_comments: true +/* + FIXME (#3300): Should allow items to be anonymous. Right now + we just use dummy names for anon items. + */ diff --git a/tests/source/issue-683.rs b/tests/source/issue-683.rs new file mode 100644 index 000000000000..fd99015ea51c --- /dev/null +++ b/tests/source/issue-683.rs @@ -0,0 +1,5 @@ +// rustfmt-normalize_comments: true +/* + * FIXME (#3300): Should allow items to be anonymous. Right now + * we just use dummy names for anon items. + */ diff --git a/tests/target/issue-539.rs b/tests/target/issue-539.rs new file mode 100644 index 000000000000..adeb33555fb5 --- /dev/null +++ b/tests/target/issue-539.rs @@ -0,0 +1,3 @@ +// rustfmt-normalize_comments: true +// FIXME (#3300): Should allow items to be anonymous. Right now +// we just use dummy names for anon items. diff --git a/tests/target/issue-683.rs b/tests/target/issue-683.rs new file mode 100644 index 000000000000..adeb33555fb5 --- /dev/null +++ b/tests/target/issue-683.rs @@ -0,0 +1,3 @@ +// rustfmt-normalize_comments: true +// FIXME (#3300): Should allow items to be anonymous. Right now +// we just use dummy names for anon items.