Commit 5a93630
authored
Tweak the heuristics on which expressions allow block formatting. (#1408)
Tweak the heuristics on which expressions allow block formatting.
I was trying out the new formatting on Flutter and noticed a few issues:
1. Immediately-invoked functions in asserts are common.
Prior to this change, these were not block format candidates, giving
you:
```dart
assert(
() {
// Some slow computation...
}(),
'Message.',
);
```
With this change, they're treated like (uninvoked) function expressions:
```dart
assert(() {
// Some slow computation...
}(), 'Message.');
```
2. Formatting large deep widget trees looks weird.
Prior to this change, a function call inside an argument list can be
treated like a block argument even if there are other arguments. That
tends to pack things in strangely in widget code, like:
```dart
SizedBox(height: 1000.0, width: double.infinity, child: Column(
children: <Widget>[Scrollbar(key: key1, child: SizedBox(
height: 300.0,
width: double.infinity,
child: SingleChildScrollView(key: innerKey, child: const SizedBox(key: Key(
'Inner scrollable',
), height: 1000.0, width: double.infinity)),
))],
));
```
With this change, a function or method call inside an argument list is
only block formatted if there are no other arguments. In other words,
you can block format a function call if the outer call is a pure wrapper
around the call, but not otherwise. That leads to:
```dart
SizedBox(
height: 1000.0,
width: double.infinity,
child: Column(children: <Widget>[Scrollbar(
key: key1,
child: SizedBox(
height: 300.0,
width: double.infinity,
child: SingleChildScrollView(
key: innerKey,
child: const SizedBox(
key: Key('Inner scrollable'),
height: 1000.0,
width: double.infinity,
),
),
),
)]),
);
```
I think that looks a lot nicer.
3. Deep, complex argument lists are combinatorially slow.
We have an optimization to eagerly force an argument list to split if
its contents clearly won't fit. But that optimization ignores the size
of a potential block argument since the contents of that can be wrapped
across multiple lines without forcing the outer argument list to split.
A consequence of that is that big deeply nested function calls end up
slow if it happens that each one only has a single block-formattable
call. That turns out to be very common in Flutter code where you have a
deep chain of nested widgets along with a bunch of other arguments.
The above change to disallow block formatting function calls if there
are any other arguments conveniently fixes that performance issue in
most cases. (Though I expect we will want to do more work here to handle
pathological cases.)1 parent 6b2b31e commit 5a93630
File tree
12 files changed
+405
-111
lines changed- benchmark/case
- lib/src
- front_end
- piece
- test
- invocation
- statement
12 files changed
+405
-111
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
149 | 151 | | |
150 | 152 | | |
151 | 153 | | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
158 | 162 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | 163 | | |
167 | | - | |
168 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
169 | 167 | | |
170 | 168 | | |
171 | 169 | | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
176 | 181 | | |
177 | 182 | | |
178 | 183 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
185 | 193 | | |
186 | 194 | | |
187 | 195 | | |
188 | | - | |
189 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
190 | 199 | | |
191 | 200 | | |
192 | 201 | | |
193 | 202 | | |
194 | | - | |
195 | | - | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
196 | 206 | | |
197 | 207 | | |
198 | | - | |
199 | | - | |
| 208 | + | |
| 209 | + | |
200 | 210 | | |
201 | | - | |
202 | | - | |
203 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
204 | 214 | | |
205 | 215 | | |
206 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
55 | 65 | | |
56 | 66 | | |
57 | 67 | | |
| |||
147 | 157 | | |
148 | 158 | | |
149 | 159 | | |
150 | | - | |
151 | | - | |
152 | | - | |
| 160 | + | |
| 161 | + | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| |||
411 | 420 | | |
412 | 421 | | |
413 | 422 | | |
414 | | - | |
| 423 | + | |
| 424 | + | |
415 | 425 | | |
416 | 426 | | |
417 | 427 | | |
418 | 428 | | |
419 | 429 | | |
420 | 430 | | |
421 | | - | |
422 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
423 | 435 | | |
424 | 436 | | |
425 | 437 | | |
| |||
428 | 440 | | |
429 | 441 | | |
430 | 442 | | |
431 | | - | |
| 443 | + | |
432 | 444 | | |
433 | 445 | | |
434 | 446 | | |
| |||
441 | 453 | | |
442 | 454 | | |
443 | 455 | | |
444 | | - | |
| 456 | + | |
445 | 457 | | |
446 | 458 | | |
447 | 459 | | |
| |||
452 | 464 | | |
453 | 465 | | |
454 | 466 | | |
455 | | - | |
456 | | - | |
457 | | - | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
458 | 476 | | |
459 | 477 | | |
460 | 478 | | |
| |||
0 commit comments