Skip to content

Commit b1b0481

Browse files
authored
Format declared variable pattern. (#1361)
* Format declared variable patterns. * Capitalize test descriptions. * Add _indentInValue in AssignPiece and update tests. * Clean up visitor and also handle long names with no types. * Make TODO a regular comment.
1 parent 2f2cfe1 commit b1b0481

File tree

8 files changed

+114
-5
lines changed

8 files changed

+114
-5
lines changed

lib/src/back_end/code_writer.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ class CodeWriter {
172172
/// piece to [indent], relative to the indentation of the surrounding piece.
173173
///
174174
/// Replaces any previous indentation set by this piece.
175+
///
176+
// TODO(tall): Add another API that adds/subtracts existing indentation.
175177
void setIndent(int indent) {
176178
_options.indent = _pieceOptions[_pieceOptions.length - 2].indent + indent;
177179
}

lib/src/front_end/ast_node_visitor.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,15 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
452452

453453
@override
454454
Piece visitDeclaredVariablePattern(DeclaredVariablePattern node) {
455-
throw UnimplementedError();
455+
var header = buildPiece((b) {
456+
b.modifier(node.keyword);
457+
b.visit(node.type);
458+
});
459+
return VariablePiece(
460+
header,
461+
[tokenPiece(node.name)],
462+
hasType: node.type != null,
463+
);
456464
}
457465

458466
@override
@@ -959,6 +967,7 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
959967
b.add(AssignPiece(
960968
expressionPiece,
961969
caseClausePiece,
970+
indentInValue: true,
962971
));
963972
} else {
964973
b.add(expressionPiece);

lib/src/piece/assign.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,20 @@ class AssignPiece extends Piece {
4848
/// split at the assignment operator.
4949
final bool _allowInnerSplit;
5050

51-
AssignPiece(this.target, this.value, {bool allowInnerSplit = false})
52-
: _allowInnerSplit = allowInnerSplit;
51+
/// Whether there's an extra indent needed in the [value] piece when it
52+
/// splits, like:
53+
//
54+
// if (obj
55+
// case SomeLongTypeName
56+
// longVariableName) {
57+
// ;
58+
// }
59+
final bool _indentInValue;
60+
61+
AssignPiece(this.target, this.value,
62+
{bool allowInnerSplit = false, bool indentInValue = false})
63+
: _allowInnerSplit = allowInnerSplit,
64+
_indentInValue = indentInValue;
5365

5466
// TODO(tall): The old formatter allows the first operand of a split
5567
// conditional expression to be on the same line as the `=`, as in:
@@ -102,6 +114,9 @@ class AssignPiece extends Piece {
102114

103115
writer.format(target);
104116
writer.splitIf(state == _atOperator);
117+
if (_indentInValue) {
118+
writer.setIndent(Indent.expression * 2);
119+
}
105120
writer.format(value);
106121
}
107122

test/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ declaration/ - Typedef, class, enum, extension, mixin, and member declarations.
6969
directories below.
7070
expression/ - Expressions and collection elements.
7171
invocation/ - Function and member invocations.
72+
pattern/ - Patterns.
7273
selection/ - Test preserving selection information.
7374
statement/ - Statements.
7475
top_level/ - Top-level directives.

test/pattern/declared_variable.stmt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
40 columns |
2+
>>> No split after "var".
3+
if (obj case var thisIsReallyQuiteAVeryLongVariableName) {;}
4+
<<<
5+
if (obj
6+
case var thisIsReallyQuiteAVeryLongVariableName) {
7+
;
8+
}
9+
>>> No split after "final".
10+
if (obj case final thisIsReallyQuiteAVeryLongVariableName) {;}
11+
<<<
12+
if (obj
13+
case final thisIsReallyQuiteAVeryLongVariableName) {
14+
;
15+
}
16+
>>> No split between "final" and type.
17+
if (obj case final ThisIsReallyQuiteAVeryLongTypeName variable) {;}
18+
<<<
19+
if (obj
20+
case final ThisIsReallyQuiteAVeryLongTypeName
21+
variable) {
22+
;
23+
}
24+
>>> Split between type and name.
25+
if (obj case SomeLongTypeName longVariableName) {
26+
;
27+
}
28+
<<<
29+
if (obj
30+
case SomeLongTypeName
31+
longVariableName) {
32+
;
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
40 columns |
2+
>>> Line comment before "var".
3+
if (obj case // c
4+
var x) {;}
5+
<<<
6+
if (obj
7+
case // c
8+
var x) {
9+
;
10+
}
11+
>>> Line comment after "var".
12+
if (obj case var // c
13+
x) {;}
14+
<<<
15+
if (obj
16+
case var // c
17+
x) {
18+
;
19+
}
20+
>>> Line comment after variable (looks weird, but user should move comment).
21+
if (obj case var x // c
22+
) {;}
23+
<<<
24+
if (obj
25+
case var x // c
26+
) {
27+
;
28+
}
29+
>>> Line comment after type.
30+
if (obj case List<int> // c
31+
x) {;}
32+
<<<
33+
if (obj
34+
case List<int> // c
35+
x) {
36+
;
37+
}
38+
>>> Line comment after type, before long name.
39+
if (obj
40+
case final // c
41+
thisIsReallyQuiteAVeryLongVariableName) {
42+
;
43+
}
44+
<<<
45+
if (obj case final // c
46+
thisIsReallyQuiteAVeryLongVariableName) {
47+
;
48+
}

test/statement/if_case_comment.stmt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ true) {;}
1313
<<<
1414
if (obj
1515
case // comment
16-
true) {
16+
true) {
1717
;
1818
}
1919
>>> Line comment after case clause.
@@ -22,6 +22,6 @@ if (obj case true // comment
2222
<<<
2323
if (obj
2424
case true // comment
25-
) {
25+
) {
2626
;
2727
}

test/tall_format_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main() async {
1414
await testDirectory('expression', tall: true);
1515
await testDirectory('function', tall: true);
1616
await testDirectory('invocation', tall: true);
17+
await testDirectory('pattern', tall: true);
1718
await testDirectory('selection', tall: true);
1819
await testDirectory('statement', tall: true);
1920
await testDirectory('top_level', tall: true);

0 commit comments

Comments
 (0)