-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for guard clauses in Java 21 switch expressions #988
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this block is the only part that needs to be different between 17 and 21, I would like to avoid duplicating the rest of the method. An alternative would be to do something like this: google-java-format/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java Line 3781 in 205b85f
There could be a |
||||
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; | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth creating a shared helper to reflectively instantiate the visitor at this point: