From 2b4696cbaa6c3fe0c7378b58744263657c33b4fb Mon Sep 17 00:00:00 2001 From: Claudia Rogoz Date: Thu, 25 Sep 2025 18:59:44 +0200 Subject: [PATCH 1/2] fix comment after default case --- .../javaformat/java/JavaInputAstVisitor.java | 4 +++- .../javaformat/java/testdata/ExpressionSwitch.input | 13 +++++++++++++ .../java/testdata/ExpressionSwitch.output | 12 ++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index cd04d09ab..27c85851c 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -1854,6 +1854,7 @@ public Void visitSwitch(SwitchTree node, Void unused) { } protected void visitSwitch(ExpressionTree expression, List cases) { + builder.open(ZERO); token("switch"); builder.space(); token("("); @@ -1874,7 +1875,8 @@ protected void visitSwitch(ExpressionTree expression, List c builder.close(); builder.forcedBreak(); builder.blankLineWanted(BlankLineWanted.NO); - token("}", plusFour); + token("}", plusTwo); + builder.close(); } @Override diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input index 9599cfc4c..366ef1ac4 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input @@ -58,4 +58,17 @@ class ExpressionSwitch { case 2 -> throw new IllegalArgumentException(); default -> throw new IllegalStateException();}; } + + + public void testWithComments(int y) { + int x; + x = switch (y) { + case 1 -> 1; + // after case 1 + case 2 -> throw new IllegalArgumentException(); + // after case 2 + default -> throw new IllegalStateException(); + // after default case + }; + } } diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output index e8f80b1b2..ee3d792aa 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.output @@ -65,4 +65,16 @@ class ExpressionSwitch { default -> throw new IllegalStateException(); }; } + + public void testWithComments(int y) { + int x; + x = switch (y) { + case 1 -> 1; + // after case 1 + case 2 -> throw new IllegalArgumentException(); + // after case 2 + default -> throw new IllegalStateException(); + // after default case + }; + } } From 78dc77067779470ce601187fe323b617c8d1c7b9 Mon Sep 17 00:00:00 2001 From: Claudia Rogoz Date: Mon, 29 Sep 2025 12:24:25 +0200 Subject: [PATCH 2/2] wrongly format the expression input --- .../java/testdata/ExpressionSwitch.input | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input index 366ef1ac4..2958ebf50 100644 --- a/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input +++ b/palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input @@ -63,12 +63,15 @@ class ExpressionSwitch { public void testWithComments(int y) { int x; x = switch (y) { - case 1 -> 1; - // after case 1 - case 2 -> throw new IllegalArgumentException(); - // after case 2 - default -> throw new IllegalStateException(); - // after default case - }; + case 1 -> +1; +// after case 1 + case 2 -> +throw new IllegalArgumentException(); +// after case 2 + default -> +throw new IllegalStateException(); +// after default case +}; } }