Skip to content

Commit 216d051

Browse files
authored
Fix line breaks in switch expression (#394)
Fix line breaks in switch expression
1 parent 6686a27 commit 216d051

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

changelog/@unreleased/pr-394.v2.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Fix line breaks in switch expression
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/394

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ boolean isYes() {
187187
}
188188

189189
/** Whether to collapse empty blocks. */
190-
enum CollapseEmptyOrNot {
190+
protected enum CollapseEmptyOrNot {
191191
YES,
192192
NO;
193193

@@ -201,7 +201,7 @@ boolean isYes() {
201201
}
202202

203203
/** Whether to allow leading blank lines in blocks. */
204-
enum AllowLeadingBlankLine {
204+
protected enum AllowLeadingBlankLine {
205205
YES,
206206
NO;
207207

@@ -211,7 +211,7 @@ static AllowLeadingBlankLine valueOf(boolean b) {
211211
}
212212

213213
/** Whether to allow trailing blank lines in blocks. */
214-
enum AllowTrailingBlankLine {
214+
protected enum AllowTrailingBlankLine {
215215
YES,
216216
NO;
217217

@@ -1218,6 +1218,11 @@ public Void visitLabeledStatement(LabeledStatementTree node, Void unused) {
12181218
public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
12191219
sync(node);
12201220
boolean statementBody = node.getBodyKind() == LambdaExpressionTree.BodyKind.STATEMENT;
1221+
visitLambdaExpression(node, statementBody);
1222+
return null;
1223+
}
1224+
1225+
protected void visitLambdaExpression(LambdaExpressionTree node, boolean statementBody) {
12211226
boolean parens = builder.peekToken().equals(Optional.of("("));
12221227
builder.open("lambda arguments", parens ? plusFour : ZERO);
12231228
if (parens) {
@@ -1266,7 +1271,6 @@ public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
12661271
scan(node.getBody(), null);
12671272
}
12681273
builder.close();
1269-
return null;
12701274
}
12711275

12721276
@Override
@@ -2108,7 +2112,7 @@ void visitAnnotations(List<? extends AnnotationTree> annotations, BreakOrNot bre
21082112
}
21092113

21102114
/** Helper method for blocks. */
2111-
private void visitBlock(
2115+
protected void visitBlock(
21122116
BlockTree node,
21132117
CollapseEmptyOrNot collapseEmptyOrNot,
21142118
AllowLeadingBlankLine allowLeadingBlankLine,

palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
import com.palantir.javaformat.OpsBuilder;
2727
import com.palantir.javaformat.java.JavaInputAstVisitor;
2828
import com.sun.source.tree.BindingPatternTree;
29+
import com.sun.source.tree.BlockTree;
2930
import com.sun.source.tree.CaseTree;
3031
import com.sun.source.tree.ClassTree;
3132
import com.sun.source.tree.ExpressionTree;
3233
import com.sun.source.tree.InstanceOfTree;
34+
import com.sun.source.tree.LambdaExpressionTree;
3335
import com.sun.source.tree.SwitchExpressionTree;
3436
import com.sun.source.tree.Tree;
37+
import com.sun.source.tree.Tree.Kind;
3538
import com.sun.source.tree.YieldTree;
3639
import com.sun.tools.javac.code.Flags;
3740
import com.sun.tools.javac.tree.JCTree;
@@ -199,15 +202,17 @@ public Void visitCase(CaseTree node, Void unused) {
199202
} else {
200203
token("case", plusTwo);
201204
builder.space();
205+
builder.open(plusTwo);
202206
boolean first = true;
203207
for (ExpressionTree expression : node.getExpressions()) {
204208
if (!first) {
205209
token(",");
206-
builder.space();
210+
builder.breakOp(" ");
207211
}
208212
scan(expression, null);
209213
first = false;
210214
}
215+
builder.close();
211216
}
212217
switch (node.getCaseKind()) {
213218
case STATEMENT:
@@ -226,12 +231,36 @@ public Void visitCase(CaseTree node, Void unused) {
226231
token("-");
227232
token(">");
228233
builder.space();
229-
scan(node.getBody(), null);
234+
if (node.getBody().getKind() == BLOCK) {
235+
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
236+
visitBlock(
237+
(BlockTree) node.getBody(),
238+
CollapseEmptyOrNot.YES,
239+
AllowLeadingBlankLine.NO,
240+
AllowTrailingBlankLine.NO);
241+
} else {
242+
scan(node.getBody(), null);
243+
}
230244
builder.guessToken(";");
231245
break;
232246
default:
233247
throw new AssertionError(node.getCaseKind());
234248
}
235249
return null;
236250
}
251+
252+
/**
253+
* TODO(fwindheuser): Collapse with
254+
* {@link JavaInputAstVisitor#visitLambdaExpression(LambdaExpressionTree, Void)}} after dropping Java 11
255+
* compatibility.
256+
*/
257+
@Override
258+
public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
259+
sync(node);
260+
// Also format switch expressions as statement body instead of inlining them
261+
boolean statementBody = node.getBodyKind() == LambdaExpressionTree.BodyKind.STATEMENT
262+
|| node.getBody().getKind() == Kind.SWITCH_EXPRESSION;
263+
visitLambdaExpression(node, statementBody);
264+
return null;
265+
}
237266
}

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,23 @@ class ExpressionSwitch {
2222
};
2323
return val;
2424
}
25-
}
25+
26+
int wrapping(Wrapping w) {
27+
switch (w) {
28+
case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE,
29+
THIS_IS_A_VERY_LONG_ENUM_VALUE_TWO,
30+
THIS_IS_A_VERY_LONG_ENUM_VALUE_THREE -> {}
31+
}
32+
33+
switch (w) {
34+
case CASE_A, CASE_B, CASE_C -> {}
35+
}
36+
}
37+
38+
Supplier<Integer> lambda(int k) {
39+
return () -> switch (k) {
40+
case 0 -> true;
41+
default -> false;
42+
};
43+
}
44+
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,23 @@ class ExpressionSwitch {
2828
};
2929
return val;
3030
}
31+
32+
int wrapping(Wrapping w) {
33+
switch (w) {
34+
case THIS_IS_A_VERY_LONG_ENUM_VALUE_ONE,
35+
THIS_IS_A_VERY_LONG_ENUM_VALUE_TWO,
36+
THIS_IS_A_VERY_LONG_ENUM_VALUE_THREE -> {}
37+
}
38+
39+
switch (w) {
40+
case CASE_A, CASE_B, CASE_C -> {}
41+
}
42+
}
43+
44+
Supplier<Integer> lambda(int k) {
45+
return () -> switch (k) {
46+
case 0 -> true;
47+
default -> false;
48+
};
49+
}
3150
}

0 commit comments

Comments
 (0)