Skip to content

Commit 4aebeb3

Browse files
authored
Merge pull request #1184 from dart-lang/patterns
Merge patterns branch into master
2 parents 4a801b5 + be551c0 commit 4aebeb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3967
-308
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 2.2.5-dev
22

3+
* Format patterns and related features.
4+
* Handle `sync*` and `async*` functions with `=>` bodies.
5+
* Allow switch statements where all case bodies are on the same line as the
6+
case when they all fit.
7+
* Fix bug where parameter metadata wouldn't always split when it should.
8+
* Don't split after `<` in collection literals.
39
* Format record expressions and record type annotations.
410
* Format class modifiers `base`, `final`, `interface`, `mixin`, and `sealed`
511
* Better indentation of multiline function types inside type argument lists.

benchmark/after.dart.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,114 @@ class BacktrackingSolver {
171171
: type = type,
172172
sources = sources,
173173
cache = new PubspecCache(type, sources) {
174+
// A fairly large switch statement.
175+
switch (region) {
176+
case Region.everywhere:
177+
return 0.45;
178+
case Region.n:
179+
return lerpDouble(pos.y, 0, height, min, max);
180+
case Region.ne:
181+
var distance = math.max(width - pos.x - 1, pos.y);
182+
var range = math.min(width, height);
183+
return lerpDouble(distance, 0, range, min, max);
184+
case Region.e:
185+
return lerpDouble(pos.x, 0, width, min, max);
186+
case Region.se:
187+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
188+
var range = math.min(width, height);
189+
return lerpDouble(distance, 0, range, min, max);
190+
case Region.s:
191+
return lerpDouble(pos.y, 0, height, max, min);
192+
case Region.sw:
193+
var distance = math.max(pos.x, height - pos.y - 1);
194+
var range = math.min(width, height);
195+
return lerpDouble(distance, 0, range, min, max);
196+
case Region.w:
197+
return lerpDouble(pos.x, 0, width, max, min);
198+
case Region.nw:
199+
var distance = math.max(pos.x, pos.y);
200+
var range = math.min(width, height);
201+
return lerpDouble(distance, 0, range, min, max);
202+
case Region.everywhere:
203+
return 0.45;
204+
case Region.n:
205+
return lerpDouble(pos.y, 0, height, min, max);
206+
case Region.ne:
207+
var distance = math.max(width - pos.x - 1, pos.y);
208+
var range = math.min(width, height);
209+
return lerpDouble(distance, 0, range, min, max);
210+
case Region.e:
211+
return lerpDouble(pos.x, 0, width, min, max);
212+
case Region.se:
213+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
214+
var range = math.min(width, height);
215+
return lerpDouble(distance, 0, range, min, max);
216+
case Region.s:
217+
return lerpDouble(pos.y, 0, height, max, min);
218+
case Region.sw:
219+
var distance = math.max(pos.x, height - pos.y - 1);
220+
var range = math.min(width, height);
221+
return lerpDouble(distance, 0, range, min, max);
222+
case Region.w:
223+
return lerpDouble(pos.x, 0, width, max, min);
224+
case Region.nw:
225+
var distance = math.max(pos.x, pos.y);
226+
var range = math.min(width, height);
227+
return lerpDouble(distance, 0, range, min, max);
228+
case Region.everywhere:
229+
return 0.45;
230+
case Region.n:
231+
return lerpDouble(pos.y, 0, height, min, max);
232+
case Region.ne:
233+
var distance = math.max(width - pos.x - 1, pos.y);
234+
var range = math.min(width, height);
235+
return lerpDouble(distance, 0, range, min, max);
236+
case Region.e:
237+
return lerpDouble(pos.x, 0, width, min, max);
238+
case Region.se:
239+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
240+
var range = math.min(width, height);
241+
return lerpDouble(distance, 0, range, min, max);
242+
case Region.s:
243+
return lerpDouble(pos.y, 0, height, max, min);
244+
case Region.sw:
245+
var distance = math.max(pos.x, height - pos.y - 1);
246+
var range = math.min(width, height);
247+
return lerpDouble(distance, 0, range, min, max);
248+
case Region.w:
249+
return lerpDouble(pos.x, 0, width, max, min);
250+
case Region.nw:
251+
var distance = math.max(pos.x, pos.y);
252+
var range = math.min(width, height);
253+
return lerpDouble(distance, 0, range, min, max);
254+
case Region.everywhere:
255+
return 0.45;
256+
case Region.n:
257+
return lerpDouble(pos.y, 0, height, min, max);
258+
case Region.ne:
259+
var distance = math.max(width - pos.x - 1, pos.y);
260+
var range = math.min(width, height);
261+
return lerpDouble(distance, 0, range, min, max);
262+
case Region.e:
263+
return lerpDouble(pos.x, 0, width, min, max);
264+
case Region.se:
265+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
266+
var range = math.min(width, height);
267+
return lerpDouble(distance, 0, range, min, max);
268+
case Region.s:
269+
return lerpDouble(pos.y, 0, height, max, min);
270+
case Region.sw:
271+
var distance = math.max(pos.x, height - pos.y - 1);
272+
var range = math.min(width, height);
273+
return lerpDouble(distance, 0, range, min, max);
274+
case Region.w:
275+
return lerpDouble(pos.x, 0, width, max, min);
276+
case Region.nw:
277+
var distance = math.max(pos.x, pos.y);
278+
var range = math.min(width, height);
279+
return lerpDouble(distance, 0, range, min, max);
280+
}
281+
174282
for (var package in useLatest) {
175283
_forceLatest.add(package);
176284
}

benchmark/before.dart.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,114 @@ class BacktrackingSolver {
171171
: type = type,
172172
sources = sources,
173173
cache = new PubspecCache(type, sources) {
174+
// A fairly large switch statement.
175+
switch (region) {
176+
case Region.everywhere:
177+
return 0.45;
178+
case Region.n:
179+
return lerpDouble(pos.y, 0, height, min, max);
180+
case Region.ne:
181+
var distance = math.max(width - pos.x - 1, pos.y);
182+
var range = math.min(width, height);
183+
return lerpDouble(distance, 0, range, min, max);
184+
case Region.e:
185+
return lerpDouble(pos.x, 0, width, min, max);
186+
case Region.se:
187+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
188+
var range = math.min(width, height);
189+
return lerpDouble(distance, 0, range, min, max);
190+
case Region.s:
191+
return lerpDouble(pos.y, 0, height, max, min);
192+
case Region.sw:
193+
var distance = math.max(pos.x, height - pos.y - 1);
194+
var range = math.min(width, height);
195+
return lerpDouble(distance, 0, range, min, max);
196+
case Region.w:
197+
return lerpDouble(pos.x, 0, width, max, min);
198+
case Region.nw:
199+
var distance = math.max(pos.x, pos.y);
200+
var range = math.min(width, height);
201+
return lerpDouble(distance, 0, range, min, max);
202+
case Region.everywhere:
203+
return 0.45;
204+
case Region.n:
205+
return lerpDouble(pos.y, 0, height, min, max);
206+
case Region.ne:
207+
var distance = math.max(width - pos.x - 1, pos.y);
208+
var range = math.min(width, height);
209+
return lerpDouble(distance, 0, range, min, max);
210+
case Region.e:
211+
return lerpDouble(pos.x, 0, width, min, max);
212+
case Region.se:
213+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
214+
var range = math.min(width, height);
215+
return lerpDouble(distance, 0, range, min, max);
216+
case Region.s:
217+
return lerpDouble(pos.y, 0, height, max, min);
218+
case Region.sw:
219+
var distance = math.max(pos.x, height - pos.y - 1);
220+
var range = math.min(width, height);
221+
return lerpDouble(distance, 0, range, min, max);
222+
case Region.w:
223+
return lerpDouble(pos.x, 0, width, max, min);
224+
case Region.nw:
225+
var distance = math.max(pos.x, pos.y);
226+
var range = math.min(width, height);
227+
return lerpDouble(distance, 0, range, min, max);
228+
case Region.everywhere:
229+
return 0.45;
230+
case Region.n:
231+
return lerpDouble(pos.y, 0, height, min, max);
232+
case Region.ne:
233+
var distance = math.max(width - pos.x - 1, pos.y);
234+
var range = math.min(width, height);
235+
return lerpDouble(distance, 0, range, min, max);
236+
case Region.e:
237+
return lerpDouble(pos.x, 0, width, min, max);
238+
case Region.se:
239+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
240+
var range = math.min(width, height);
241+
return lerpDouble(distance, 0, range, min, max);
242+
case Region.s:
243+
return lerpDouble(pos.y, 0, height, max, min);
244+
case Region.sw:
245+
var distance = math.max(pos.x, height - pos.y - 1);
246+
var range = math.min(width, height);
247+
return lerpDouble(distance, 0, range, min, max);
248+
case Region.w:
249+
return lerpDouble(pos.x, 0, width, max, min);
250+
case Region.nw:
251+
var distance = math.max(pos.x, pos.y);
252+
var range = math.min(width, height);
253+
return lerpDouble(distance, 0, range, min, max);
254+
case Region.everywhere:
255+
return 0.45;
256+
case Region.n:
257+
return lerpDouble(pos.y, 0, height, min, max);
258+
case Region.ne:
259+
var distance = math.max(width - pos.x - 1, pos.y);
260+
var range = math.min(width, height);
261+
return lerpDouble(distance, 0, range, min, max);
262+
case Region.e:
263+
return lerpDouble(pos.x, 0, width, min, max);
264+
case Region.se:
265+
var distance = math.max(width - pos.x - 1, height - pos.y - 1);
266+
var range = math.min(width, height);
267+
return lerpDouble(distance, 0, range, min, max);
268+
case Region.s:
269+
return lerpDouble(pos.y, 0, height, max, min);
270+
case Region.sw:
271+
var distance = math.max(pos.x, height - pos.y - 1);
272+
var range = math.min(width, height);
273+
return lerpDouble(distance, 0, range, min, max);
274+
case Region.w:
275+
return lerpDouble(pos.x, 0, width, max, min);
276+
case Region.nw:
277+
var distance = math.max(pos.x, pos.y);
278+
var range = math.min(width, height);
279+
return lerpDouble(distance, 0, range, min, max);
280+
}
281+
174282
for (var package in useLatest) {
175283
_forceLatest.add(package);
176284
}

lib/src/argument_list_visitor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ class ArgumentSublist {
476476
}
477477

478478
if (argument is NamedExpression) {
479-
visitor.visitNamedArgument(argument, rule as NamedRule);
479+
visitor.visitNamedNode(argument.name.label.token, argument.name.colon,
480+
argument.expression, rule as NamedRule);
480481
} else {
481482
visitor.visit(argument);
482483
}

lib/src/chunk.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ class Chunk extends Selection {
114114

115115
/// Whether this chunk marks the end of a range of chunks that can be line
116116
/// split independently of the following chunks.
117-
///
118-
/// You must call markDivide() before accessing this.
119117
bool get canDivide => _canDivide;
120-
late final bool _canDivide;
118+
bool _canDivide = true;
121119

122120
/// The number of characters in this chunk when unsplit.
123121
int get length => (_spaceWhenUnsplit ? 1 : 0) + _text.length;
@@ -177,9 +175,12 @@ class Chunk extends Selection {
177175
/// that [Rule].
178176
bool indentBlock(int Function(Rule) getValue) => false;
179177

180-
// Mark whether this chunk can divide the range of chunks.
181-
void markDivide(bool canDivide) {
182-
_canDivide = canDivide;
178+
/// Prevent the line splitter from diving at this chunk.
179+
///
180+
/// This should be called on any chunk where line splitting choices before
181+
/// and after this chunk relate to each other.
182+
void preventDivide() {
183+
_canDivide = false;
183184
}
184185

185186
@override

0 commit comments

Comments
 (0)