From 610278d5b8a676fdda99b29d66e404654830ec63 Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Wed, 23 Sep 2026 14:14:41 -0400 Subject: [PATCH 1/2] Don't break line when switch expression used in declaration assignment (#1789) (cherry picked from commit 478d4c58eb5d71e87ccb10952ad817c28abfda89) --- .../javaformat/java/JavaInputAstVisitor.java | 13 ++++------- .../java/java14/Java14InputAstVisitor.java | 1 - .../java/testdata/ExpressionSwitch.input | 11 +++++----- .../java/testdata/ExpressionSwitch.output | 22 +++++++++---------- 4 files changed, 19 insertions(+), 28 deletions(-) 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..329ad0131 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 @@ -43,12 +43,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) { 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..b6a770e59 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,12 +20,11 @@ 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; } @@ -49,12 +48,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) { From 522fab5495f298242c47343c8c08885ad4931adf Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Fri, 25 Sep 2026 11:07:00 +0300 Subject: [PATCH 2/2] Keep an input in the old shape in the switch expression golden The upstream commit rewrote the input of test1 into the new shape, so both inputs of the golden that declare a variable with a switch expression already started from the target form, and the golden checked re-indentation and stability only. Nothing started from "=", a line break and the switch, which is what the change moves. test1's input goes back to that shape, as it was before; its expected output does not change. A field initialized with a switch expression, written in the same old shape, is added as well, since no golden covered a field. --- .../java/testdata/ExpressionSwitch.input | 18 +++++++++++++----- .../java/testdata/ExpressionSwitch.output | 6 ++++++ 2 files changed, 19 insertions(+), 5 deletions(-) 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 329ad0131..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, @@ -43,11 +50,12 @@ 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) { 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 b6a770e59..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 @@ -28,6 +28,12 @@ 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,