26
26
import com .palantir .javaformat .OpsBuilder ;
27
27
import com .palantir .javaformat .java .JavaInputAstVisitor ;
28
28
import com .sun .source .tree .BindingPatternTree ;
29
+ import com .sun .source .tree .BlockTree ;
29
30
import com .sun .source .tree .CaseTree ;
30
31
import com .sun .source .tree .ClassTree ;
31
32
import com .sun .source .tree .ExpressionTree ;
32
33
import com .sun .source .tree .InstanceOfTree ;
34
+ import com .sun .source .tree .LambdaExpressionTree ;
33
35
import com .sun .source .tree .SwitchExpressionTree ;
34
36
import com .sun .source .tree .Tree ;
37
+ import com .sun .source .tree .Tree .Kind ;
35
38
import com .sun .source .tree .YieldTree ;
36
39
import com .sun .tools .javac .code .Flags ;
37
40
import com .sun .tools .javac .tree .JCTree ;
@@ -199,15 +202,17 @@ public Void visitCase(CaseTree node, Void unused) {
199
202
} else {
200
203
token ("case" , plusTwo );
201
204
builder .space ();
205
+ builder .open (plusTwo );
202
206
boolean first = true ;
203
207
for (ExpressionTree expression : node .getExpressions ()) {
204
208
if (!first ) {
205
209
token ("," );
206
- builder .space ( );
210
+ builder .breakOp ( " " );
207
211
}
208
212
scan (expression , null );
209
213
first = false ;
210
214
}
215
+ builder .close ();
211
216
}
212
217
switch (node .getCaseKind ()) {
213
218
case STATEMENT :
@@ -226,12 +231,36 @@ public Void visitCase(CaseTree node, Void unused) {
226
231
token ("-" );
227
232
token (">" );
228
233
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
+ }
230
244
builder .guessToken (";" );
231
245
break ;
232
246
default :
233
247
throw new AssertionError (node .getCaseKind ());
234
248
}
235
249
return null ;
236
250
}
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
+ }
237
266
}
0 commit comments