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..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 @@ -58,4 +58,20 @@ 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 + }; + } }