-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for guard clauses in Java 21 switch expressions
- Loading branch information
Showing
3 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.google.googlejavaformat.java.java21; | ||
|
||
import static com.google.common.collect.Iterables.getOnlyElement; | ||
|
||
import com.google.googlejavaformat.OpsBuilder; | ||
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor; | ||
import com.sun.source.tree.*; | ||
import java.util.List; | ||
|
||
public class Java21InputAstVisitor extends Java17InputAstVisitor { | ||
|
||
public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) { | ||
super(builder, indentMultiplier); | ||
} | ||
|
||
@Override | ||
public Void visitCase(CaseTree node, Void unused) { | ||
sync(node); | ||
markForPartialFormat(); | ||
builder.forcedBreak(); | ||
List<? extends CaseLabelTree> labels = node.getLabels(); | ||
boolean isDefault = | ||
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL"); | ||
if (isDefault) { | ||
token("default", plusTwo); | ||
} else { | ||
token("case", plusTwo); | ||
builder.open(labels.size() > 1 ? plusFour : ZERO); | ||
builder.space(); | ||
boolean first = true; | ||
for (Tree expression : labels) { | ||
if (!first) { | ||
token(","); | ||
builder.breakOp(" "); | ||
} | ||
scan(expression, null); | ||
first = false; | ||
} | ||
builder.close(); | ||
} | ||
if (node.getGuard() != null) { | ||
builder.space(); | ||
token("when"); | ||
builder.space(); | ||
scan(node.getGuard(), null); | ||
} | ||
switch (node.getCaseKind()) { | ||
case STATEMENT: | ||
token(":"); | ||
builder.open(plusTwo); | ||
visitStatements(node.getStatements()); | ||
builder.close(); | ||
break; | ||
case RULE: | ||
builder.space(); | ||
token("-"); | ||
token(">"); | ||
builder.space(); | ||
if (node.getBody().getKind() == Tree.Kind.BLOCK) { | ||
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks. | ||
visitBlock( | ||
(BlockTree) node.getBody(), | ||
CollapseEmptyOrNot.YES, | ||
AllowLeadingBlankLine.NO, | ||
AllowTrailingBlankLine.NO); | ||
} else { | ||
scan(node.getBody(), null); | ||
} | ||
builder.guessToken(";"); | ||
break; | ||
default: | ||
throw new AssertionError(node.getCaseKind()); | ||
} | ||
return null; | ||
} | ||
} |