Skip to content

Commit ce04d38

Browse files
authored
Add test that tickles subtree merging. (#1488)
An important optimization in the formatter is that it will format a subtree of the Piece tree separately and weave the result back into the parent Solution when possible. Part of that process is adding in any bound states, overflow characters, and costs determined in the subtree. Surprisingly, those rarely actually come into play in terms of affecting the outermost winning solution. I'm not sure exactly why, but if you just merge in the subtree solution's text and discard the bound states, overflow, and cost... all the tests still pass. But after testing on a large corpus, it turns out that in more complex real-world examples, it *is* important to copy that data back over. So I grabbed an example whose formatting was affected and added this as a sort of regression test.
1 parent 93d2068 commit ce04d38

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/tall/regression/other/flutter.unit

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,42 @@ main() {
1111
..style.width = '100%'
1212
..style.height = '100%'
1313
..classList.add(_kClassName);
14+
}
15+
>>>
16+
### When a subtree is formatted separately, the mergeSubtree() step mostly has
17+
### no effect because the subtree's cost and overflow doesn't affect the
18+
### winning solution. But it sometimes does. None of the existing tests happen
19+
### to hit a case that does, but when formatting a corpus with subtree merging
20+
### on and off, this was a relatively simple example where the behavior differs.
21+
class C {
22+
void paint(PaintingContext context, Offset offset) {
23+
final (double visualPosition, Color leftColor, Color rightColor) =
24+
switch (textDirection) {
25+
TextDirection.rtl => (
26+
1.0 - _position.value,
27+
_activeColor,
28+
trackColor,
29+
),
30+
TextDirection.ltr => (_position.value, trackColor, _activeColor),
31+
};
32+
33+
final double trackCenter = offset.dy + size.height / 2.0;
34+
final double trackLeft = offset.dx + _trackLeft;
35+
}
36+
}
37+
<<<
38+
class C {
39+
void paint(PaintingContext context, Offset offset) {
40+
final (
41+
double visualPosition,
42+
Color leftColor,
43+
Color rightColor,
44+
) = switch (textDirection) {
45+
TextDirection.rtl => (1.0 - _position.value, _activeColor, trackColor),
46+
TextDirection.ltr => (_position.value, trackColor, _activeColor),
47+
};
48+
49+
final double trackCenter = offset.dy + size.height / 2.0;
50+
final double trackLeft = offset.dx + _trackLeft;
51+
}
1452
}

0 commit comments

Comments
 (0)