diff --git a/src/comment.rs b/src/comment.rs index 43e8e64a7a9..f9875d6e4b1 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -94,7 +94,7 @@ impl<'a> CommentStyle<'a> { } } - pub(crate) fn line_start(&self) -> &'a str { + pub(crate) fn comment_prefix(&self) -> &'a str { match *self { CommentStyle::DoubleSlash => "// ", CommentStyle::TripleSlash => "/// ", @@ -107,7 +107,7 @@ impl<'a> CommentStyle<'a> { } pub(crate) fn to_str_tuplet(&self) -> (&'a str, &'a str, &'a str) { - (self.opener(), self.closer(), self.line_start()) + (self.opener(), self.closer(), self.comment_prefix()) } } @@ -306,7 +306,7 @@ fn identify_comment( let (has_bare_lines, first_group_ending) = match style { CommentStyle::DoubleSlash | CommentStyle::TripleSlash | CommentStyle::Doc => { - let line_start = style.line_start().trim_start(); + let line_start = style.comment_prefix().trim_start(); consume_same_line_comments(style, orig, line_start) } CommentStyle::Custom(opener) => { @@ -583,7 +583,7 @@ struct CommentRewrite<'a> { opener: String, closer: String, - line_start: String, + comment_prefix: String, style: CommentStyle<'a>, } @@ -633,7 +633,7 @@ impl<'a> CommentRewrite<'a> { opener: opener.to_owned(), closer: closer.to_owned(), - line_start: line_start.to_owned(), + comment_prefix: line_start.to_owned(), style, }; cr.result.push_str(opener); @@ -883,7 +883,8 @@ impl<'a> CommentRewrite<'a> { self.fmt.shape = if self.is_prev_line_multi_line { // 1 = " " let offset = 1 + last_line_width(&self.result, self.fmt.config.tab_spaces()) - - self.line_start.len(); + - self.comment_prefix.len() + - self.fmt_indent.width(); Shape { width: self.max_width.saturating_sub(offset), indent: self.fmt_indent, diff --git a/tests/source/comment_wrapping.rs b/tests/source/comment_wrapping.rs new file mode 100644 index 00000000000..ba892a256d2 --- /dev/null +++ b/tests/source/comment_wrapping.rs @@ -0,0 +1,18 @@ +// rustfmt-wrap_comments: true +// rustfmt-comment_width: 100 + +fn f() { + // Note: off-by-one bug: the 100 width wide line should not be formatted + // https://github.com/rust-lang/rustfmt/issues/7167 + foo( + // this comment is 100 characters wide! The last word should fit on the following line -- aa + // This line is 92 characters wide ------------------------------------------------- + 1, + ); + + // this comment should be left alone + // this comment is 100 characters wide! -------------------------------------------------------- + + // this comment is 101 characters wide! The last word should fit on the following line ------- aa + // This line is 92 characters wide ---------------------------------------------------- +} diff --git a/tests/target/comment_wrapping.rs b/tests/target/comment_wrapping.rs new file mode 100644 index 00000000000..bab45fb32fa --- /dev/null +++ b/tests/target/comment_wrapping.rs @@ -0,0 +1,18 @@ +// rustfmt-wrap_comments: true +// rustfmt-comment_width: 100 + +fn f() { + // Note: off-by-one bug: the 100 width wide line should not be formatted + // https://github.com/rust-lang/rustfmt/issues/7167 + foo( + // this comment is 100 characters wide! The last word should fit on the following line -- + // aa This line is 92 characters wide ------------------------------------------------- + 1, + ); + + // this comment should be left alone + // this comment is 100 characters wide! -------------------------------------------------------- + + // this comment is 101 characters wide! The last word should fit on the following line ------- + // aa This line is 92 characters wide ---------------------------------------------------- +}