Skip to content

Commit 610278d

Browse files
pkoenig10abashev
authored andcommitted
Don't break line when switch expression used in declaration assignment (palantir#1789)
(cherry picked from commit 478d4c5)
1 parent 746fb60 commit 610278d

4 files changed

Lines changed: 19 additions & 28 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
@@ -3621,15 +3621,10 @@ int declareOne(
36213621
initializer.get().accept(this, null);
36223622
builder.close();
36233623
} else {
3624-
if (builder.peekToken().get().equals("switch")) {
3625-
// TODO(fawind): Don't break switch expression assignment
3626-
builder.open(Indent.If.make(typeBreak, plusFour, ZERO));
3627-
} else {
3628-
builder.open(
3629-
Indent.If.make(typeBreak, plusFour, ZERO),
3630-
BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true),
3631-
LastLevelBreakability.ABORT);
3632-
}
3624+
builder.open(
3625+
Indent.If.make(typeBreak, plusFour, ZERO),
3626+
BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true),
3627+
LastLevelBreakability.ABORT);
36333628
{
36343629
builder.breakToFill(" ");
36353630
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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ class ExpressionSwitch {
4343
}
4444

4545
public void test1(int y) {
46-
int x =
47-
switch (y) {
48-
case 1 -> 1;
49-
case 2 -> throw new IllegalArgumentException();
50-
default -> throw new IllegalStateException();
51-
};
46+
int x = switch (y) {
47+
case 1 -> 1;
48+
case 2 -> throw new IllegalArgumentException();
49+
default -> throw new IllegalStateException();
50+
};
5251
}
5352

5453
public void test2(int y) {

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ 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

@@ -49,12 +48,11 @@ class ExpressionSwitch {
4948
}
5049

5150
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-
};
51+
int x = switch (y) {
52+
case 1 -> 1;
53+
case 2 -> throw new IllegalArgumentException();
54+
default -> throw new IllegalStateException();
55+
};
5856
}
5957

6058
public void test2(int y) {

0 commit comments

Comments
 (0)