Skip to content
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

Wrap multiple values in switch expression case #558

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -177,7 +177,7 @@ boolean isYes() {
}

/** Whether to collapse empty blocks. */
enum CollapseEmptyOrNot {
protected enum CollapseEmptyOrNot {
YES,
NO;

Expand All @@ -191,7 +191,7 @@ boolean isYes() {
}

/** Whether to allow leading blank lines in blocks. */
enum AllowLeadingBlankLine {
protected enum AllowLeadingBlankLine {
YES,
NO;

Expand All @@ -201,7 +201,7 @@ static AllowLeadingBlankLine valueOf(boolean b) {
}

/** Whether to allow trailing blank lines in blocks. */
enum AllowTrailingBlankLine {
protected enum AllowTrailingBlankLine {
YES,
NO;

Expand Down Expand Up @@ -2097,7 +2097,7 @@ void visitAnnotations(
}

/** Helper method for blocks. */
private void visitBlock(
protected void visitBlock(
BlockTree node,
CollapseEmptyOrNot collapseEmptyOrNot,
AllowLeadingBlankLine allowLeadingBlankLine,
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.JavaInputAstVisitor;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
Expand Down Expand Up @@ -214,16 +215,18 @@ public Void visitCase(CaseTree node, Void unused) {
token("default", plusTwo);
} else {
token("case", plusTwo);
builder.open(plusFour);
builder.space();
boolean first = true;
for (ExpressionTree expression : node.getExpressions()) {
if (!first) {
token(",");
builder.space();
builder.breakOp(" ");
}
scan(expression, null);
first = false;
}
builder.close();
}
switch (node.getCaseKind()) {
case STATEMENT:
Expand All @@ -237,7 +240,16 @@ public Void visitCase(CaseTree node, Void unused) {
token("-");
token(">");
builder.space();
scan(node.getBody(), null);
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:
Expand Down
Expand Up @@ -25,4 +25,17 @@ class ExpressionSwitch {
default -> System.out.println("default");
}
}

String breakLongCaseArgs(MyEnum e) {
return switch (e) {
case SOME_RATHER_LONG_NAME_1, SOME_RATHER_LONG_NAME_2, SOME_RATHER_LONG_NAME_3, SOME_RATHER_LONG_NAME_4, SOME_RATHER_LONG_NAME_5, SOME_RATHER_LONG_NAME_6, SOME_RATHER_LONG_NAME_7 -> {}
case SOME_RATHER_LONG_NAME_8 -> {}
};
}

String dontBreakShortCaseArgs(MyEnum e) {
return switch (e) {
case CASE_A, CASE_B -> {}
};
}
}
Expand Up @@ -31,4 +31,23 @@ class ExpressionSwitch {
default -> System.out.println("default");
}
}

String breakLongCaseArgs(MyEnum e) {
return switch (e) {
case SOME_RATHER_LONG_NAME_1,
SOME_RATHER_LONG_NAME_2,
SOME_RATHER_LONG_NAME_3,
SOME_RATHER_LONG_NAME_4,
SOME_RATHER_LONG_NAME_5,
SOME_RATHER_LONG_NAME_6,
SOME_RATHER_LONG_NAME_7 -> {}
case SOME_RATHER_LONG_NAME_8 -> {}
};
}

String dontBreakShortCaseArgs(MyEnum e) {
return switch (e) {
case CASE_A, CASE_B -> {}
};
}
}