Skip to content

Commit 55c967b

Browse files
authored
Prefer splitting beforecase in an if-case. (#1358)
1 parent e45c447 commit 55c967b

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,25 @@ class AstNodeVisitor extends ThrowingAstVisitor<Piece> with PieceFactory {
945945
b.token(ifStatement.ifKeyword);
946946
b.space();
947947
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+
952967
b.token(ifStatement.rightParenthesis);
953968
b.space();
954969
});

test/statement/if_case.stmt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ if (obj case true) {;}
55
if (obj case true) {
66
;
77
}
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+
}

test/statement/if_case_comment.stmt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)