Skip to content

Commit 81e5375

Browse files
authored
Format map patterns. (#1368)
1 parent d50165d commit 81e5375

File tree

5 files changed

+120
-5
lines changed

5 files changed

+120
-5
lines changed

lib/src/ast_extensions.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ extension PatternExtensions on DartPattern {
390390
bool get canBlockSplit => switch (this) {
391391
ListPattern(:var elements, :var rightBracket) =>
392392
elements.canSplit(rightBracket),
393+
MapPattern(:var elements, :var rightBracket) =>
394+
elements.canSplit(rightBracket),
393395
_ => false,
394396
};
395397
}

lib/src/front_end/ast_node_visitor.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,18 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
11891189

11901190
@override
11911191
Piece visitMapPattern(MapPattern node) {
1192-
throw UnimplementedError();
1192+
return createCollection(
1193+
typeArguments: node.typeArguments,
1194+
node.leftBracket,
1195+
node.elements,
1196+
node.rightBracket,
1197+
);
11931198
}
11941199

11951200
@override
11961201
Piece visitMapPatternEntry(MapPatternEntry node) {
1197-
throw UnimplementedError();
1202+
return createAssignment(node.key, node.separator, node.value,
1203+
spaceBeforeOperator: false);
11981204
}
11991205

12001206
@override

lib/src/front_end/piece_factory.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,7 @@ mixin PieceFactory {
884884
///
885885
/// If [allowInnerSplit] is `true`, then a newline inside the target or
886886
/// right-hand side doesn't force splitting at the operator itself.
887-
Piece createAssignment(
888-
AstNode target, Token operator, Expression rightHandSide,
887+
Piece createAssignment(AstNode target, Token operator, AstNode rightHandSide,
889888
{bool splitBeforeOperator = false,
890889
bool includeComma = false,
891890
bool spaceBeforeOperator = true,
@@ -896,7 +895,11 @@ mixin PieceFactory {
896895
// var list = [
897896
// element,
898897
// ];
899-
allowInnerSplit |= rightHandSide.canBlockSplit;
898+
allowInnerSplit |= switch (rightHandSide) {
899+
Expression() => rightHandSide.canBlockSplit,
900+
DartPattern() => rightHandSide.canBlockSplit,
901+
_ => false
902+
};
900903

901904
if (splitBeforeOperator) {
902905
var targetPiece = nodePiece(target);

test/pattern/map.stmt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
40 columns |
2+
>>> Unsplit map.
3+
if (obj case {k: 1, m: 3, ...}) {;}
4+
<<<
5+
if (obj case {k: 1, m: 3, ...}) {
6+
;
7+
}
8+
>>> If it splits anywhere, it splits at every element.
9+
if (obj case {first: 1,second: 2,third: 3}) {;}
10+
<<<
11+
if (obj case {
12+
first: 1,
13+
second: 2,
14+
third: 3,
15+
}) {
16+
;
17+
}
18+
>>> Nested map patterns don't force outer to split.
19+
if (obj case {a: {k: 1}, m: [{k: 3}]}) {;}
20+
<<<
21+
if (obj case {a: {k: 1}, m: [{k: 3}]}) {
22+
;
23+
}
24+
>>> Tall splitting style with line comment.
25+
if (obj case {
26+
// yeah
27+
a:1,b:2,c:3,
28+
d:4,e:5,f:6,
29+
}) {;}
30+
<<<
31+
if (obj case {
32+
// yeah
33+
a: 1,
34+
b: 2,
35+
c: 3,
36+
d: 4,
37+
e: 5,
38+
f: 6,
39+
}) {
40+
;
41+
}
42+
>>> Remove trailing comma if unsplit.
43+
if (obj case {k:1,}) {;}
44+
<<<
45+
if (obj case {k: 1}) {
46+
;
47+
}
48+
>>> Remove trailing comma if unsplit, multiple.
49+
if (e case {a: 1, b: 2,}) {}
50+
<<<
51+
if (e case {a: 1, b: 2}) {}
52+
>>> Add comma to map pattern if split.
53+
if (e case {a: longPattern1, b: veryLongPattern2}) {}
54+
<<<
55+
if (e case {
56+
a: longPattern1,
57+
b: veryLongPattern2,
58+
}) {}

test/pattern/map_comment.stmt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
40 columns |
2+
>>> Indent line comment inside map.
3+
if (obj case {
4+
// c
5+
}) {;}
6+
<<<
7+
if (obj case {
8+
// c
9+
}) {
10+
;
11+
}
12+
>>> Line comment on opening line of map.
13+
if (obj case {// c
14+
}) {;}
15+
<<<
16+
if (obj case {
17+
// c
18+
}) {
19+
;
20+
}
21+
>>> Indented block comment in map.
22+
if (obj case {
23+
/* comment */
24+
}){;}
25+
<<<
26+
if (obj case {
27+
/* comment */
28+
}) {
29+
;
30+
}
31+
>>> Inline block comment in map.
32+
if (obj case { /* comment */ k: v }){;}
33+
<<<
34+
if (obj case {/* comment */ k: v}) {
35+
;
36+
}
37+
>>> Line comment between map items.
38+
if (obj case {k: 'a', // comment
39+
m: 'b'}){;}
40+
<<<
41+
if (obj case {
42+
k: 'a', // comment
43+
m: 'b',
44+
}) {
45+
;
46+
}

0 commit comments

Comments
 (0)