Skip to content

Commit a468fb1

Browse files
authored
Format patterns in variables, assignments, and for loops. (#1173)
* Format object patterns, const constructors, and constant expressions. The latter two were already implemented as part of keeping the existing switch tests passing, but this adds tests for their splitting and other behavior. For object patterns, I had to decide whether to split them like collections (like other patterns) or like argument lists (which they mirror). I went collection-like because they will appear mixed with other patterns. Also, if we migrate to a consistent flutter-like style at some point, it will be one less thing to change. Let me know what you think. * Format patterns in variables, assignments, and for loops.
1 parent 480b449 commit a468fb1

13 files changed

+470
-37
lines changed

lib/src/source_visitor.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ class SourceVisitor extends ThrowingAstVisitor {
323323
});
324324
}
325325

326+
@override
327+
void visitAssignedVariablePattern(AssignedVariablePattern node) {
328+
token(node.name);
329+
}
330+
326331
@override
327332
void visitAssignmentExpression(AssignmentExpression node) {
328333
builder.nestExpression();
@@ -1571,6 +1576,16 @@ class SourceVisitor extends ThrowingAstVisitor {
15711576
_visitForEachPartsFromIn(node);
15721577
}
15731578

1579+
@override
1580+
void visitForEachPartsWithPattern(ForEachPartsWithPattern node) {
1581+
builder.startBlockArgumentNesting();
1582+
token(node.keyword);
1583+
space();
1584+
visit(node.pattern);
1585+
builder.endBlockArgumentNesting();
1586+
_visitForEachPartsFromIn(node);
1587+
}
1588+
15741589
@override
15751590
void visitForPartsWithDeclarations(ForPartsWithDeclarations node) {
15761591
// Nest split variables more so they aren't at the same level
@@ -1601,6 +1616,14 @@ class SourceVisitor extends ThrowingAstVisitor {
16011616
_visitForPartsFromLeftSeparator(node);
16021617
}
16031618

1619+
@override
1620+
void visitForPartsWithPattern(ForPartsWithPattern node) {
1621+
builder.startBlockArgumentNesting();
1622+
visit(node.variables);
1623+
builder.endBlockArgumentNesting();
1624+
_visitForPartsFromLeftSeparator(node);
1625+
}
1626+
16041627
void _visitForPartsFromLeftSeparator(ForParts node) {
16051628
token(node.leftSeparator);
16061629

@@ -2370,6 +2393,30 @@ class SourceVisitor extends ThrowingAstVisitor {
23702393
});
23712394
}
23722395

2396+
@override
2397+
void visitPatternAssignment(PatternAssignment node) {
2398+
visit(node.pattern);
2399+
_visitAssignment(node.equals, node.expression);
2400+
}
2401+
2402+
@override
2403+
void visitPatternVariableDeclaration(PatternVariableDeclaration node) {
2404+
visitMetadata(node.metadata);
2405+
builder.nestExpression();
2406+
token(node.keyword);
2407+
space();
2408+
visit(node.pattern);
2409+
_visitAssignment(node.equals, node.expression);
2410+
builder.unnest();
2411+
}
2412+
2413+
@override
2414+
void visitPatternVariableDeclarationStatement(
2415+
PatternVariableDeclarationStatement node) {
2416+
visit(node.declaration);
2417+
token(node.semicolon);
2418+
}
2419+
23732420
@override
23742421
void visitPostfixExpression(PostfixExpression node) {
23752422
visit(node.operand);

test/comments/expressions.stmt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,18 @@ someVeryLongFunctionName(argumentName: true
100100
/* comment */);
101101
<<<
102102
someVeryLongFunctionName(
103-
argumentName: true /* comment */);
103+
argumentName: true /* comment */);
104+
>>> in delimited pattern in pattern assignment
105+
(x,// c
106+
y) = o;
107+
<<<
108+
(
109+
x, // c
110+
y
111+
) = o;
112+
>>> in infix pattern in pattern assignment
113+
(x &&// c
114+
y) = o;
115+
<<<
116+
(x && // c
117+
y) = o;

test/comments/statements.stmt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
40 columns |
2-
>>> inline after "var"
3-
var /* int */ x;
4-
<<<
5-
var /* int */ x;
6-
>>> trailing line comment
7-
var x; // x
8-
<<<
9-
var x; // x
10-
>>> multiple variable declaration list
11-
var x /* X */, y;
12-
<<<
13-
var x /* X */, y;
142
>>> continue with line comment
153
while (true) {
164
continue // comment
@@ -37,13 +25,6 @@ do // comment
3725
{
3826
;
3927
} while (true);
40-
>>> always place newline after multi-line block comment
41-
/*
42-
*/ var i = value;
43-
<<<
44-
/*
45-
*/
46-
var i = value;
4728
>>> remove blank lines before beginning of block
4829
while (true) {
4930

test/comments/variables.stmt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
40 columns |
2+
>>> inline after "var"
3+
var /* int */ x;
4+
<<<
5+
var /* int */ x;
6+
>>> trailing line comment
7+
var x; // x
8+
<<<
9+
var x; // x
10+
>>> multiple variable declaration list
11+
var x /* X */, y;
12+
<<<
13+
var x /* X */, y;
14+
>>> always place newline after multi-line block comment
15+
/*
16+
*/ var i = value;
17+
<<<
18+
/*
19+
*/
20+
var i = value;
21+
>>> after keyword in pattern variable
22+
var // c
23+
(x, y) = o;
24+
<<<
25+
var // c
26+
(x, y) = o;
27+
>>> in delimited pattern in pattern variable
28+
var (x,// c
29+
y) = o;
30+
<<<
31+
var (
32+
x, // c
33+
y
34+
) = o;
35+
>>> in infix pattern in pattern assignment
36+
var (x &&// c
37+
y) = o;
38+
<<<
39+
var (x && // c
40+
y) = o;

test/splitting/assignments.stmt

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,108 @@ variableName =
3535
variableName = argument * argument + argument;
3636
<<<
3737
variableName =
38-
argument * argument + argument;
38+
argument * argument + argument;
39+
>>> prefer to split at "=" instead of pattern
40+
(longIdentifier && anotherOne) = longValue;
41+
<<<
42+
(longIdentifier && anotherOne) =
43+
longValue;
44+
>>> split in infix pattern
45+
(veryLongIdentifier && anotherAlsoLongOne) = value;
46+
<<<
47+
(veryLongIdentifier &&
48+
anotherAlsoLongOne) = value;
49+
>>> split in list pattern
50+
[first, second, third, fourth, fifth, sixth] = value;
51+
<<<
52+
[
53+
first,
54+
second,
55+
third,
56+
fourth,
57+
fifth,
58+
sixth
59+
] = value;
60+
>>> split in map pattern
61+
{first: second, third: fourth, fifth: sixth} = value;
62+
<<<
63+
{
64+
first: second,
65+
third: fourth,
66+
fifth: sixth
67+
} = value;
68+
>>> split in record pattern
69+
(first, second, third, fourth, fifth, sixth) = value;
70+
<<<
71+
(
72+
first,
73+
second,
74+
third,
75+
fourth,
76+
fifth,
77+
sixth
78+
) = value;
79+
>>> split in object pattern
80+
Foo(:first, :second, :third, :fourth, :fifth) = value;
81+
<<<
82+
Foo(
83+
:first,
84+
:second,
85+
:third,
86+
:fourth,
87+
:fifth
88+
) = value;
89+
>>> split in value
90+
(first, second, third) = longValueExpression + anotherOperand + aThirdOperand;
91+
<<<
92+
(first, second, third) =
93+
longValueExpression +
94+
anotherOperand +
95+
aThirdOperand;
96+
>>> expression split in both
97+
(veryLongIdentifier && anotherAlsoLongOne) = longValueExpression + anotherOperand + aThirdOperand;
98+
<<<
99+
(veryLongIdentifier &&
100+
anotherAlsoLongOne) =
101+
longValueExpression +
102+
anotherOperand +
103+
aThirdOperand;
104+
>>> collection-like split in both
105+
(first, second, third, fourth, fifth, sixth) = (first, second, third, fourth, fifth);
106+
<<<
107+
(
108+
first,
109+
second,
110+
third,
111+
fourth,
112+
fifth,
113+
sixth
114+
) = (
115+
first,
116+
second,
117+
third,
118+
fourth,
119+
fifth
120+
);
121+
>>> expression split in pattern, collection-like in value
122+
(veryLongIdentifier && anotherAlsoLongOne) = (first, second, third, fourth, fifth);
123+
<<<
124+
(veryLongIdentifier &&
125+
anotherAlsoLongOne) = (
126+
first,
127+
second,
128+
third,
129+
fourth,
130+
fifth
131+
);
132+
>>> expression split in pattern, collection-like in value
133+
(veryLongIdentifier && anotherAlsoLongOne) = (first, second, third, fourth, fifth);
134+
<<<
135+
(veryLongIdentifier &&
136+
anotherAlsoLongOne) = (
137+
first,
138+
second,
139+
third,
140+
fourth,
141+
fifth
142+
);

test/splitting/list_collection_for.stmt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,26 @@ var list = [
180180
incrementExpression +
181181
thatDoesNotFit)
182182
body
183+
];
184+
>>> pattern for-in expression split in pattern
185+
var list = [
186+
for (var (longIdentifier && anotherLongOne) in obj) element
187+
];
188+
<<<
189+
var list = [
190+
for (var (longIdentifier &&
191+
anotherLongOne) in obj)
192+
element
193+
];
194+
>>> pattern for-in expression split in pattern
195+
var list = [
196+
for (var (longIdentifier && anotherLongOne) = obj; cond; inc) element
197+
];
198+
<<<
199+
var list = [
200+
for (var (longIdentifier &&
201+
anotherLongOne) = obj;
202+
cond;
203+
inc)
204+
element
183205
];

test/splitting/loops.stmt

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,86 @@ while (condition) something(i);
110110
while (condition) somethingMuchLonger(i);
111111
<<<
112112
while (condition)
113-
somethingMuchLonger(i);
113+
somethingMuchLonger(i);
114+
>>> pattern for-in expression split in pattern
115+
for (var (longIdentifier && anotherLongOne) in obj) {;}
116+
<<<
117+
for (var (longIdentifier &&
118+
anotherLongOne) in obj) {
119+
;
120+
}
121+
>>> pattern for-in block split in pattern
122+
for (var [longIdentifier, anotherLongOne] in obj) {;}
123+
<<<
124+
for (var [
125+
longIdentifier,
126+
anotherLongOne
127+
] in obj) {
128+
;
129+
}
130+
>>> pattern for-in split in value
131+
for (var (first, second, third) in longValueExpression + anotherOperand +
132+
aThirdOperand) {;}
133+
<<<
134+
for (var (first, second, third)
135+
in longValueExpression +
136+
anotherOperand +
137+
aThirdOperand) {
138+
;
139+
}
140+
>>> pattern for-in split in both
141+
for (var (longIdentifier && anotherAlsoLongOne) in longValueExpression +
142+
anotherOperand + aThirdOperand) {;}
143+
<<<
144+
for (var (longIdentifier &&
145+
anotherAlsoLongOne)
146+
in longValueExpression +
147+
anotherOperand +
148+
aThirdOperand) {
149+
;
150+
}
151+
>>> pattern for expression split in pattern
152+
for (var (longIdentifier && anotherLongOne) = obj; cond; inc) {;}
153+
<<<
154+
for (var (longIdentifier &&
155+
anotherLongOne) = obj;
156+
cond;
157+
inc) {
158+
;
159+
}
160+
>>> pattern for-in block split in pattern
161+
for (var [longIdentifier, anotherLongOne] = obj; cond; inc) {;}
162+
<<<
163+
for (var [
164+
longIdentifier,
165+
anotherLongOne
166+
] = obj;
167+
cond;
168+
inc) {
169+
;
170+
}
171+
>>> pattern for-in split in value
172+
for (var (first, second, third) = longValueExpression + anotherOperand +
173+
aThirdOperand; cond; inc) {;}
174+
<<<
175+
for (var (first, second, third) =
176+
longValueExpression +
177+
anotherOperand +
178+
aThirdOperand;
179+
cond;
180+
inc) {
181+
;
182+
}
183+
>>> pattern for-in split in both
184+
for (var (longIdentifier && anotherAlsoLongOne) = longValueExpression +
185+
anotherOperand + aThirdOperand; cond; inc) {;}
186+
<<<
187+
for (var (longIdentifier &&
188+
anotherAlsoLongOne) =
189+
longValueExpression +
190+
anotherOperand +
191+
aThirdOperand;
192+
cond;
193+
inc) {
194+
;
195+
}

0 commit comments

Comments
 (0)