Skip to content

Commit 98a80c6

Browse files
authored
Merge pull request #76 from openjavaformat/switch-expression-declaration
Start a switch expression on the line of the declaration it initializes
2 parents 8cc4259 + 522fab5 commit 98a80c6

4 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,15 +3654,10 @@ int declareOne(
36543654
initializer.get().accept(this, null);
36553655
builder.close();
36563656
} else {
3657-
if (builder.peekToken().get().equals("switch")) {
3658-
// TODO(fawind): Don't break switch expression assignment
3659-
builder.open(Indent.If.make(typeBreak, plusFour, ZERO));
3660-
} else {
3661-
builder.open(
3662-
Indent.If.make(typeBreak, plusFour, ZERO),
3663-
BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true),
3664-
LastLevelBreakability.ABORT);
3665-
}
3657+
builder.open(
3658+
Indent.If.make(typeBreak, plusFour, ZERO),
3659+
BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true),
3660+
LastLevelBreakability.ABORT);
36663661
{
36673662
builder.breakToFill(" ");
36683663
scan(initializer.get(), null);

‎open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ public Void visitCase(CaseTree node, Void unused) {
336336
builder.close();
337337
}
338338
builder.guessToken(";");
339-
builder.forcedBreak(minusTwo);
340339
break;
341340
default:
342341
throw new IllegalArgumentException(node.getCaseKind().name());

‎open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class ExpressionSwitch {
2323
return val;
2424
}
2525

26+
static final int FIELD =
27+
switch (KIND) {
28+
case 0 -> 1;
29+
case 1 -> 2;
30+
default -> throw new IllegalStateException();
31+
};
32+
2633
int wrapping(Wrapping w) {
2734
switch (w) {
2835
case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE,

‎open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ class ExpressionSwitch {
2020
}
2121

2222
int assignment(int j) {
23-
boolean val =
24-
switch (j) {
25-
case 0 -> true;
26-
case 1 -> false;
27-
default -> false;
28-
};
23+
boolean val = switch (j) {
24+
case 0 -> true;
25+
case 1 -> false;
26+
default -> false;
27+
};
2928
return val;
3029
}
3130

31+
static final int FIELD = switch (KIND) {
32+
case 0 -> 1;
33+
case 1 -> 2;
34+
default -> throw new IllegalStateException();
35+
};
36+
3237
int wrapping(Wrapping w) {
3338
switch (w) {
3439
case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE,
@@ -49,12 +54,11 @@ class ExpressionSwitch {
4954
}
5055

5156
public void test1(int y) {
52-
int x =
53-
switch (y) {
54-
case 1 -> 1;
55-
case 2 -> throw new IllegalArgumentException();
56-
default -> throw new IllegalStateException();
57-
};
57+
int x = switch (y) {
58+
case 1 -> 1;
59+
case 2 -> throw new IllegalArgumentException();
60+
default -> throw new IllegalStateException();
61+
};
5862
}
5963

6064
public void test2(int y) {

0 commit comments

Comments
 (0)