Skip to content

Commit 8559543

Browse files
authored
Format the remaining simple patterns. (#1167)
That means unary and primary patterns, but excludes all of the delimited ones: list, map, record, object and the corresponding constants.
1 parent b5ad63e commit 8559543

File tree

4 files changed

+282
-2
lines changed

4 files changed

+282
-2
lines changed

lib/src/source_visitor.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ class SourceVisitor extends ThrowingAstVisitor {
601601
builder.unnest();
602602
}
603603

604+
@override
605+
void visitCastPattern(CastPattern node) {
606+
builder.startSpan();
607+
builder.nestExpression();
608+
visit(node.pattern);
609+
soloSplit();
610+
token(node.asToken);
611+
space();
612+
visit(node.type);
613+
builder.unnest();
614+
builder.endSpan();
615+
}
616+
604617
@override
605618
void visitCatchClause(CatchClause node) {
606619
token(node.onKeyword, after: space);
@@ -950,6 +963,13 @@ class SourceVisitor extends ThrowingAstVisitor {
950963
token(node.name);
951964
}
952965

966+
@override
967+
void visitDeclaredVariablePattern(DeclaredVariablePattern node) {
968+
modifier(node.keyword);
969+
visit(node.type, after: soloSplit);
970+
token(node.name);
971+
}
972+
953973
@override
954974
void visitDefaultFormalParameter(DefaultFormalParameter node) {
955975
visit(node.parameter);
@@ -2299,6 +2319,12 @@ class SourceVisitor extends ThrowingAstVisitor {
22992319
token(node.operator);
23002320
}
23012321

2322+
@override
2323+
void visitPostfixPattern(PostfixPattern node) {
2324+
visit(node.operand);
2325+
token(node.operator);
2326+
}
2327+
23022328
@override
23032329
void visitPrefixedIdentifier(PrefixedIdentifier node) {
23042330
CallChainVisitor(this, node).visit();
@@ -3443,7 +3469,7 @@ class SourceVisitor extends ThrowingAstVisitor {
34433469
token(caseClause.caseKeyword);
34443470
space();
34453471
builder.startBlockArgumentNesting();
3446-
builder.nestExpression();
3472+
builder.nestExpression(now: true);
34473473
visit(caseClause.guardedPattern.pattern);
34483474
builder.unnest();
34493475
builder.endBlockArgumentNesting();

test/comments/patterns.stmt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,58 @@ if (obj
6767
) {
6868
;
6969
}
70+
>>> before "as"
71+
if (obj case constant // c
72+
as Type) {;}
73+
<<<
74+
if (obj
75+
case constant // c
76+
as Type) {
77+
;
78+
}
79+
>>> after "as"
80+
if (obj case constant as // c
81+
Type) {;}
82+
<<<
83+
if (obj
84+
case constant as // c
85+
Type) {
86+
;
87+
}
88+
>>> before null-check (looks weird, but user should move comment)
89+
if (obj case pattern // c
90+
?) {;}
91+
<<<
92+
if (obj
93+
case pattern // c
94+
?) {
95+
;
96+
}
97+
>>> after null-check (looks weird, but user should move comment)
98+
if (obj case pattern? // c
99+
) {;}
100+
<<<
101+
if (obj case pattern? // c
102+
) {
103+
;
104+
}
105+
>>> before null-assert (looks weird, but user should move comment)
106+
if (obj case pattern // c
107+
!) {;}
108+
<<<
109+
if (obj
110+
case pattern // c
111+
!) {
112+
;
113+
}
114+
>>> after null-assert (looks weird, but user should move comment)
115+
if (obj case pattern! // c
116+
) {;}
117+
<<<
118+
if (obj case pattern! // c
119+
) {
120+
;
121+
}
70122
>>> inside parenthesized
71123
if (obj case ( // c
72124
pattern)) {;}
@@ -75,4 +127,59 @@ if (obj
75127
case ( // c
76128
pattern)) {
77129
;
130+
}
131+
>>> in qualified name
132+
if (obj case qualified // c
133+
.name) {;}
134+
<<<
135+
if (obj
136+
case qualified // c
137+
.name) {
138+
;
139+
}
140+
>>> in prefixed qualified name
141+
if (obj case qualified // c
142+
.prefixed // c
143+
.name) {;}
144+
<<<
145+
if (obj
146+
case qualified // c
147+
.prefixed // c
148+
.name) {
149+
;
150+
}
151+
>>> before "var"
152+
if (obj case // c
153+
var x) {;}
154+
<<<
155+
if (obj
156+
case // c
157+
var x) {
158+
;
159+
}
160+
>>> after "var"
161+
if (obj case var // c
162+
x) {;}
163+
<<<
164+
if (obj
165+
case var // c
166+
x) {
167+
;
168+
}
169+
>>> after variable (looks weird, but user should move comment)
170+
if (obj case var x // c
171+
) {;}
172+
<<<
173+
if (obj case var x // c
174+
) {
175+
;
176+
}
177+
>>> after type
178+
if (obj case List<int> // c
179+
x) {;}
180+
<<<
181+
if (obj
182+
case List<int> // c
183+
x) {
184+
;
78185
}

test/splitting/patterns.stmt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,77 @@ if (object
3838
case != veryLongConstant +
3939
expressionThatSplits) {
4040
;
41+
}
42+
>>> split in cast
43+
if (object case veryLongConstant as VeryLongType) {;}
44+
<<<
45+
if (object
46+
case veryLongConstant
47+
as VeryLongType) {
48+
;
49+
}
50+
>>> split in qualified name
51+
if (object case veryLongPrefix.longIdentifierName) {;}
52+
<<<
53+
if (object
54+
case veryLongPrefix
55+
.longIdentifierName) {
56+
;
57+
}
58+
>>> prefer to split in first part prefixed qualified name
59+
if (object case longPrefix.longType.longIdentifierName) {;}
60+
<<<
61+
if (object
62+
case longPrefix
63+
.longType.longIdentifierName) {
64+
;
65+
}
66+
>>> split just at second part of prefixed qualified name
67+
if (object case longPrefix.veryLongType.longIdentifierName) {;}
68+
<<<
69+
if (object
70+
case longPrefix.veryLongType
71+
.longIdentifierName) {
72+
;
73+
}
74+
>>> split both parts of prefixed qualified name
75+
if (object case veryLongPrefixIdentifier.veryLongType.longIdentifierName) {;}
76+
<<<
77+
if (object
78+
case veryLongPrefixIdentifier
79+
.veryLongType
80+
.longIdentifierName) {
81+
;
82+
}
83+
>>> no split after "var"
84+
if (obj case var thisIsReallyQuiteAVeryLongVariableName) {;}
85+
<<<
86+
if (obj
87+
case var thisIsReallyQuiteAVeryLongVariableName) {
88+
;
89+
}
90+
>>> no split after "final"
91+
if (obj case final thisIsReallyQuiteAVeryLongVariableName) {;}
92+
<<<
93+
if (obj
94+
case final thisIsReallyQuiteAVeryLongVariableName) {
95+
;
96+
}
97+
>>> no split between "final" and type
98+
if (obj case final ThisIsReallyQuiteAVeryLongTypeName variable) {;}
99+
<<<
100+
if (obj
101+
case final ThisIsReallyQuiteAVeryLongTypeName
102+
variable) {
103+
;
104+
}
105+
>>> split between type and name
106+
if (obj case SomeLongTypeName longVariableName) {
107+
;
108+
}
109+
<<<
110+
if (obj
111+
case SomeLongTypeName
112+
longVariableName) {
113+
;
41114
}

test/whitespace/patterns.stmt

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,78 @@ switch (obj) {
3030
>>> relational as subpattern
3131
if (o case > 1 && < 2 && ( == 3 )) {}
3232
<<<
33-
if (o case > 1 && < 2 && (== 3)) {}
33+
if (o case > 1 && < 2 && (== 3)) {}
34+
>>> cast
35+
if (o case 1 as List < int > ? ) {}
36+
<<<
37+
if (o case 1 as List<int>?) {}
38+
>>> null-check
39+
if (o case pattern ? ) {}
40+
<<<
41+
if (o case pattern?) {}
42+
>>> null-assert
43+
if (o case pattern ! ) {}
44+
<<<
45+
if (o case pattern!) {}
46+
>>> simple constants
47+
switch (obj) {
48+
case true :
49+
case false :
50+
case null :
51+
case 123 :
52+
case 12.34 :
53+
case - 123 :
54+
case - 12.34 :
55+
case 'string' :
56+
case 's$tr${ ing }' :
57+
case #symbol :
58+
ok;
59+
}
60+
<<<
61+
switch (obj) {
62+
case true:
63+
case false:
64+
case null:
65+
case 123:
66+
case 12.34:
67+
case -123:
68+
case -12.34:
69+
case 'string':
70+
case 's$tr${ing}':
71+
case #symbol:
72+
ok;
73+
}
74+
>>> identifiers
75+
switch (obj) {
76+
case _ :
77+
case name :
78+
case qualified . name :
79+
case prefixed . qualified . name :
80+
ok;
81+
}
82+
<<<
83+
switch (obj) {
84+
case _:
85+
case name:
86+
case qualified.name:
87+
case prefixed.qualified.name:
88+
ok;
89+
}
90+
>>> variables
91+
switch (obj) {
92+
case int ? _ :
93+
case List < String > name :
94+
case var name :
95+
case final name :
96+
case final ( int , String ) name :
97+
ok;
98+
}
99+
<<<
100+
switch (obj) {
101+
case int? _:
102+
case List<String> name:
103+
case var name:
104+
case final name:
105+
case final (int, String) name:
106+
ok;
107+
}

0 commit comments

Comments
 (0)