Skip to content

Commit a740aab

Browse files
authored
Remove support for compact switch statements. (#1191)
I'm still interested in supporting this, but it causes a lot of churn. That means it really needs to go through the full dart style change process. Given how many other things are in the air with Dart 3.0, it seems like now is probably not the ideal time for that. So, for now, I think we should keep the old formatting where case bodies always go on their own line.
1 parent 83523b3 commit a740aab

File tree

8 files changed

+480
-105
lines changed

8 files changed

+480
-105
lines changed

CHANGELOG.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# 2.3.0-dev
22

3+
## New language features
4+
35
* Format patterns and related features.
6+
* Format record expressions and record type annotations.
7+
* Format class modifiers `base`, `final`, `interface`, `mixin`, and `sealed`.
8+
* Format unnamed libraries.
9+
10+
## Bug fixes and style changes
11+
412
* Handle `sync*` and `async*` functions with `=>` bodies.
5-
* Allow switch statements where all case bodies are on the same line as the
6-
case when they all fit.
713
* Fix bug where parameter metadata wouldn't always split when it should.
814
* Don't split after `<` in collection literals.
9-
* Format record expressions and record type annotations.
10-
* Format class modifiers `base`, `final`, `interface`, `mixin`, and `sealed`
1115
* Better indentation of multiline function types inside type argument lists.
16+
17+
## Internal changes
18+
1219
* Use typed `_visitFunctionOrMethodDeclaration` instead of dynamically typed.
1320
* Fix metadata test to not fail when record syntax makes whitespace between
1421
metadata annotation names and `(` significant ([sdk#50769][]).

lib/src/source_visitor.dart

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,40 +2792,10 @@ class SourceVisitor extends ThrowingAstVisitor {
27922792
_visitSwitchValue(node.switchKeyword, node.leftParenthesis, node.expression,
27932793
node.rightParenthesis);
27942794
_beginBody(node.leftBracket);
2795-
2796-
// If all of the case bodies are small, it looks nice if they go on the same
2797-
// line as `case`, like:
2798-
//
2799-
// switch (obj) {
2800-
// case 1: print('one');
2801-
// case 2:
2802-
// case 3: print('two or three');
2803-
// }
2804-
//
2805-
// But it looks bad if some cases are inline and others split:
2806-
//
2807-
// switch (obj) {
2808-
// case 1: print('one');
2809-
// case 2:
2810-
// print('two');
2811-
// print('two again');
2812-
// case 3: print('two or three');
2813-
// }
2814-
//
2815-
// So we use a single rule for all cases. If any case splits, because it has
2816-
// multiple statements, or there is a split in the pattern or body, then
2817-
// they all split.
2818-
var caseRule = SplitContainingRule();
2819-
caseRule.disableSplitOnInnerRules();
2820-
builder.startLazyRule(caseRule);
2821-
28222795
for (var member in node.members) {
28232796
_visitLabels(member.labels);
28242797
token(member.keyword);
28252798

2826-
// We want a split in the pattern or bodies to force the cases to split.
2827-
caseRule.enableSplitOnInnerRules();
2828-
28292799
if (member is SwitchCase) {
28302800
space();
28312801
visit(member.expression);
@@ -2858,24 +2828,16 @@ class SourceVisitor extends ThrowingAstVisitor {
28582828

28592829
if (member.statements.isNotEmpty) {
28602830
builder.indent();
2861-
split();
2831+
newline();
28622832
visitNodes(member.statements, between: oneOrTwoNewlines);
28632833
builder.unindent();
2864-
2865-
// We don't want the split between cases to force them to split.
2866-
caseRule.disableSplitOnInnerRules();
2867-
oneOrTwoNewlines(preventDivide: true);
2834+
oneOrTwoNewlines();
28682835
} else {
2869-
// We don't want the split between cases to force them to split.
2870-
caseRule.disableSplitOnInnerRules();
2871-
28722836
// Don't preserve blank lines between empty cases.
2873-
builder.writeNewline(preventDivide: true);
2837+
builder.writeNewline();
28742838
}
28752839
}
28762840

2877-
builder.endRule();
2878-
28792841
newline();
28802842
_endBody(node.rightBracket, forceSplit: true);
28812843
}
@@ -4150,9 +4112,8 @@ class SourceVisitor extends ThrowingAstVisitor {
41504112
/// Allow either one or two newlines to be emitted before the next
41514113
/// non-whitespace token based on whether any blank lines exist in the source
41524114
/// between the last token and the next one.
4153-
void oneOrTwoNewlines({bool preventDivide = false}) {
4154-
builder.writeNewline(
4155-
isDouble: _linesBeforeNextToken > 1, preventDivide: preventDivide);
4115+
void oneOrTwoNewlines() {
4116+
builder.writeNewline(isDouble: _linesBeforeNextToken > 1);
41564117
}
41574118

41584119
/// The number of newlines between the last written token and the next one to

test/comments/switch.stmt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ switch (n) {
2727

2828
// comment
2929

30-
case 1: body;
30+
case 1:
31+
body;
3132

3233
// comment
3334
}
@@ -39,9 +40,11 @@ switch (n) {
3940
}
4041
<<<
4142
switch (n) {
42-
case 0: zero;
43+
case 0:
44+
zero;
4345
// comment
44-
case 1: one;
46+
case 1:
47+
one;
4548
}
4649
>>> line comment at end of statement does not force split
4750
switch (n) {
@@ -51,9 +54,12 @@ switch (n) {
5154
}
5255
<<<
5356
switch (n) {
54-
case 0: zero; // comment
55-
case 1: one; // comment
56-
case 2: two; // comment
57+
case 0:
58+
zero; // comment
59+
case 1:
60+
one; // comment
61+
case 2:
62+
two; // comment
5763
}
5864
>>> line comment indentation
5965
switch (n) {
@@ -66,9 +72,11 @@ switch (n) {
6672
<<<
6773
switch (n) {
6874
// before first
69-
case 0: zero;
75+
case 0:
76+
zero;
7077
// between
71-
case 1: one;
78+
case 1:
79+
one;
7280
// after last
7381
}
7482
>>> line comment in empty cases
@@ -87,21 +95,6 @@ switch (n) {
8795
case 2:
8896
// comment 2
8997
}
90-
>>> bodies all split or don't together even with comment in the middle
91-
switch (n) {
92-
case 0: longBodyExpression + thatForcesSplit;
93-
// comment
94-
case 1: c;
95-
}
96-
<<<
97-
switch (n) {
98-
case 0:
99-
longBodyExpression +
100-
thatForcesSplit;
101-
// comment
102-
case 1:
103-
c;
104-
}
10598
>>> keeps one blank line around case comments in switch expression
10699
e = switch (n) {
107100

test/regression/1100/1181.stmt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ switch (e) {
66
}
77
<<<
88
switch (e) {
9-
case E.e1: break;
9+
case E.e1:
10+
break;
1011
case E.e2:
1112
}

0 commit comments

Comments
 (0)