Skip to content

Commit cecee09

Browse files
Fix wrapping of comments sometimes making too long lines
In the case where `comment_width` is less than `max_width` and we want to wrap right on `comment_width`: `rustfmt` would consider it's wrapping length to be exactly `comment_width`, ignoring the length of a trailing indent. The fix is essentially how similar formatting code behaves, see e.g. `shape::Shape::comment`. This involved rewriting some existing tests, without impacting what they're meant to be asserting on. This emphasises that this patch _is not_ backwards compatible: while I believe the change to be correct (some tests had comments longer than `comment_width` that weren't being formatted). So there's a trade-off to be made between correctness and stability, since `comment_width` is still not stabilised this may be acceptable. Issue: 6801
1 parent de42a17 commit cecee09

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

src/missed_spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'a> FmtVisitor<'a> {
263263
};
264264

265265
let comment_width = ::std::cmp::min(
266-
self.config.comment_width(),
266+
self.config.comment_width() - self.block_indent.width(),
267267
self.config.max_width() - self.block_indent.width(),
268268
);
269269
let comment_shape = Shape::legacy(comment_width, comment_indent);

tests/source/issue_6801.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn foo() {
4+
// this is a comment that could be broken right on max_comment and should break before that
5+
6+
()
7+
}

tests/target/issue_6801.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn foo() {
4+
// this is a comment that could be broken right on max_comment and should
5+
// break before that
6+
7+
()
8+
}

tests/target/trailing_comments/hard_tabs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn lorem_ipsum() {
1818
// nunc. Mauris consequat, enim vitae venenatis sollicitudin, dolor orci
1919
// bibendum enim, a sagittis nulla nunc quis elit. Phasellus augue. Nunc
2020
// suscipit, magna tincidunt lacinia faucibus, lacus tellus ornare purus, a
21-
// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus.
22-
// In hac habitasse platea dictumst. Vivamus a orci at nulla tristique
23-
// condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit
24-
// amet, ipsum. In pharetra sagittis nunc.
21+
// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor
22+
// faucibus. In hac habitasse platea dictumst. Vivamus a orci at nulla
23+
// tristique condimentum. Donec arcu quam, dictum accumsan, convallis
24+
// accumsan, cursus sit amet, ipsum. In pharetra sagittis nunc.
2525
let b = baz();
2626

2727
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]

tests/target/trailing_comments/soft_tabs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn foo() {
1818
// nunc. Mauris consequat, enim vitae venenatis sollicitudin, dolor orci
1919
// bibendum enim, a sagittis nulla nunc quis elit. Phasellus augue. Nunc
2020
// suscipit, magna tincidunt lacinia faucibus, lacus tellus ornare purus, a
21-
// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus.
22-
// In hac habitasse platea dictumst. Vivamus a orci at nulla tristique
23-
// condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit
24-
// amet, ipsum. In pharetra sagittis nunc.
21+
// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor
22+
// faucibus. In hac habitasse platea dictumst. Vivamus a orci at nulla
23+
// tristique condimentum. Donec arcu quam, dictum accumsan, convallis
24+
// accumsan, cursus sit amet, ipsum. In pharetra sagittis nunc.
2525
let b = baz();
2626

2727
let normalized = self.ctfont.all_traits().normalized_weight(); // [-1.0, 1.0]

tests/target/unicode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ fn foo() {
44
let s = "this line goes to 100: ͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶ";
55
let s = 42;
66

7-
// a comment of length 80, with the starting sigil: ҘҘҘҘҘҘҘҘҘҘ ҘҘҘҘҘҘҘҘҘҘҘҘҘҘ
7+
// a comment of length 80, with the starting sigil: ҘҘҘҘҘҘҘҘҘҘ
8+
// ҘҘҘҘҘҘҘҘҘҘҘҘҘҘ
89
let s = 42;
910
}
1011

0 commit comments

Comments
 (0)