Skip to content

Commit 83d464c

Browse files
authored
Fix comments after default switch case expressions (#1420)
Fix comments after default switch case expressions
1 parent 537ae3d commit 83d464c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,7 @@ public Void visitSwitch(SwitchTree node, Void unused) {
18541854
}
18551855

18561856
protected void visitSwitch(ExpressionTree expression, List<? extends CaseTree> cases) {
1857+
builder.open(ZERO);
18571858
token("switch");
18581859
builder.space();
18591860
token("(");
@@ -1874,7 +1875,8 @@ protected void visitSwitch(ExpressionTree expression, List<? extends CaseTree> c
18741875
builder.close();
18751876
builder.forcedBreak();
18761877
builder.blankLineWanted(BlankLineWanted.NO);
1877-
token("}", plusFour);
1878+
token("}", plusTwo);
1879+
builder.close();
18781880
}
18791881

18801882
@Override

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/ExpressionSwitch.input

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,20 @@ class ExpressionSwitch {
5858
case 2 -> throw new IllegalArgumentException();
5959
default -> throw new IllegalStateException();};
6060
}
61+
62+
63+
public void testWithComments(int y) {
64+
int x;
65+
x = switch (y) {
66+
case 1 ->
67+
1;
68+
// after case 1
69+
case 2 ->
70+
throw new IllegalArgumentException();
71+
// after case 2
72+
default ->
73+
throw new IllegalStateException();
74+
// after default case
75+
};
76+
}
6177
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ class ExpressionSwitch {
6565
default -> throw new IllegalStateException();
6666
};
6767
}
68+
69+
public void testWithComments(int y) {
70+
int x;
71+
x = switch (y) {
72+
case 1 -> 1;
73+
// after case 1
74+
case 2 -> throw new IllegalArgumentException();
75+
// after case 2
76+
default -> throw new IllegalStateException();
77+
// after default case
78+
};
79+
}
6880
}

0 commit comments

Comments
 (0)