Skip to content

Commit dfddfd0

Browse files
committed
Fix bug with comments being reordered.
In certain locations, if you had multiple inline block comments on the same line, they would get reordered. That in turn would (correctly) trip the formatter's internal "whitespace-only changes" sanity check and lead to it aborting. Fix #1667.
1 parent 71421b3 commit dfddfd0

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/src/front_end/piece_writer.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ final class PieceWriter {
398398
if (comments.isEmpty) return const [];
399399

400400
var leadingComments = <Piece>[];
401+
var afterHanging = false;
401402
for (var i = 0; i < comments.length; i++) {
402403
var comment = comments[i];
403404

@@ -411,12 +412,16 @@ final class PieceWriter {
411412

412413
var piece = commentPiece(comment, trailingWhitespace);
413414

414-
if (comments.isHanging(i)) {
415+
if (!afterHanging && comments.isHanging(i)) {
415416
// Attach it to the previous CodePiece.
416417
_previousCode!.addHangingComment(piece);
417418
} else {
418419
// Add it to the list of leading comments for the upcoming token.
419420
leadingComments.add(piece);
421+
422+
// Once we've found a single non-hanging comment, all subsequent ones
423+
// must also be non-hanging since they follow it.
424+
afterHanging = true;
420425
}
421426
}
422427

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
>>>
2+
/*Debugger:stepOver*/
3+
void main() {
4+
/*bl*/ /*sl:1*/ var data = [1, 2, 3];
5+
for (
6+
// comment forcing formatting
7+
/*sl:3*/ /*sl:5*/ /*sl:7*/ /*sl:8*/ var datapoint
8+
// comment forcing formatting
9+
in
10+
// comment forcing formatting
11+
/*sl:2*/ data
12+
// comment forcing formatting
13+
) {
14+
/*sl:4*/ /*sl:6*/ /*sl:8*/ print(datapoint);
15+
}
16+
/*sl:9 */ print('Done');
17+
}
18+
<<<
19+
/*Debugger:stepOver*/
20+
void main() {
21+
/*bl*/ /*sl:1*/
22+
var data = [1, 2, 3];
23+
for (
24+
// comment forcing formatting
25+
/*sl:3*/ /*sl:5*/ /*sl:7*/ /*sl:8*/ var datapoint
26+
// comment forcing formatting
27+
in
28+
// comment forcing formatting
29+
/*sl:2*/ data
30+
// comment forcing formatting
31+
) {
32+
/*sl:4*/ /*sl:6*/ /*sl:8*/
33+
print(datapoint);
34+
}
35+
/*sl:9 */
36+
print('Done');
37+
}
38+
>>> Minimal repro.
39+
main() {
40+
for (
41+
/* 1 */ /* 2 */ datapoint in data) {}
42+
}
43+
<<<
44+
main() {
45+
for (
46+
/* 1 */ /* 2 */ datapoint in data) {}
47+
}

0 commit comments

Comments
 (0)