File tree Expand file tree Collapse file tree 3 files changed +60
-4
lines changed Expand file tree Collapse file tree 3 files changed +60
-4
lines changed Original file line number Diff line number Diff line change @@ -945,10 +945,25 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
945
945
b.token (ifStatement.ifKeyword);
946
946
b.space ();
947
947
b.token (ifStatement.leftParenthesis);
948
- b.add (buildPiece ((b) {
949
- b.visit (ifStatement.expression);
950
- b.visit (ifStatement.caseClause, spaceBefore: true );
951
- }));
948
+
949
+ // If the condition needs to split, we prefer splitting before the
950
+ // `case` keyword, like:
951
+ //
952
+ // if (obj
953
+ // case 123456789012345678901234567890) {
954
+ // body;
955
+ // }
956
+ var expressionPiece = nodePiece (ifStatement.expression);
957
+ if (ifStatement.caseClause case var caseClause? ) {
958
+ var caseClausePiece = nodePiece (caseClause);
959
+ b.add (AssignPiece (
960
+ expressionPiece,
961
+ caseClausePiece,
962
+ ));
963
+ } else {
964
+ b.add (expressionPiece);
965
+ }
966
+
952
967
b.token (ifStatement.rightParenthesis);
953
968
b.space ();
954
969
});
Original file line number Diff line number Diff line change @@ -5,3 +5,17 @@ if (obj case true) {;}
5
5
if (obj case true) {
6
6
;
7
7
}
8
+ >>> Split long expression before case.
9
+ if (thisIsReallyQuiteAVeryLongVariableName case 1) {;}
10
+ <<<
11
+ if (thisIsReallyQuiteAVeryLongVariableName
12
+ case 1) {
13
+ ;
14
+ }
15
+ >>> Split long case clause before case.
16
+ if (obj case 123456789012345678901234567890) {;}
17
+ <<<
18
+ if (obj
19
+ case 123456789012345678901234567890) {
20
+ ;
21
+ }
Original file line number Diff line number Diff line change
1
+ 40 columns |
2
+ >>> Line comment before case keyword.
3
+ if (obj // comment
4
+ case true) {;}
5
+ <<<
6
+ if (obj // comment
7
+ case true) {
8
+ ;
9
+ }
10
+ >>> Line comment after case keyword.
11
+ if (obj case // comment
12
+ true) {;}
13
+ <<<
14
+ if (obj
15
+ case // comment
16
+ true) {
17
+ ;
18
+ }
19
+ >>> Line comment after case clause.
20
+ if (obj case true // comment
21
+ ) {;}
22
+ <<<
23
+ if (obj
24
+ case true // comment
25
+ ) {
26
+ ;
27
+ }
You can’t perform that action at this time.
0 commit comments