diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 0fd3569d8..3f4c64249 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -3621,15 +3621,10 @@ int declareOne( initializer.get().accept(this, null); builder.close(); } else { - if (builder.peekToken().get().equals("switch")) { - // TODO(fawind): Don't break switch expression assignment - builder.open(Indent.If.make(typeBreak, plusFour, ZERO)); - } else { - builder.open( - Indent.If.make(typeBreak, plusFour, ZERO), - BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true), - LastLevelBreakability.ABORT); - } + builder.open( + Indent.If.make(typeBreak, plusFour, ZERO), + BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(true), + LastLevelBreakability.ABORT); { builder.breakToFill(" "); scan(initializer.get(), null); diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java b/open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java index 4566c0fbc..5a8f9938e 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java @@ -336,7 +336,6 @@ public Void visitCase(CaseTree node, Void unused) { builder.close(); } builder.guessToken(";"); - builder.forcedBreak(minusTwo); break; default: throw new IllegalArgumentException(node.getCaseKind().name()); diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input index 2958ebf50..bb439589c 100644 --- a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input @@ -23,6 +23,13 @@ class ExpressionSwitch { return val; } + static final int FIELD = + switch (KIND) { + case 0 -> 1; + case 1 -> 2; + default -> throw new IllegalStateException(); + }; + int wrapping(Wrapping w) { switch (w) { case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE, diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output index ee3d792aa..5ef1ffadd 100644 --- a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output @@ -20,15 +20,20 @@ class ExpressionSwitch { } int assignment(int j) { - boolean val = - switch (j) { - case 0 -> true; - case 1 -> false; - default -> false; - }; + boolean val = switch (j) { + case 0 -> true; + case 1 -> false; + default -> false; + }; return val; } + static final int FIELD = switch (KIND) { + case 0 -> 1; + case 1 -> 2; + default -> throw new IllegalStateException(); + }; + int wrapping(Wrapping w) { switch (w) { case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE, @@ -49,12 +54,11 @@ class ExpressionSwitch { } public void test1(int y) { - int x = - switch (y) { - case 1 -> 1; - case 2 -> throw new IllegalArgumentException(); - default -> throw new IllegalStateException(); - }; + int x = switch (y) { + case 1 -> 1; + case 2 -> throw new IllegalArgumentException(); + default -> throw new IllegalStateException(); + }; } public void test2(int y) {