Skip to content

Commit

Permalink
Wrap multiple values in switch expression case
Browse files Browse the repository at this point in the history
Closes #540

* Wrap multiple values covered in a switch expression case if they exceed the max line length.
  ```diff
  switch (e) {
  -  case SOME_RATHER_LONG_NAME_1, SOME_RATHER_LONG_NAME_2, SOME_RATHER_LONG_NAME_3 -> {}
  +  case SOME_RATHER_LONG_NAME_1,
  +    SOME_RATHER_LONG_NAME_2,
  +    SOME_RATHER_LONG_NAME_3 -> {}
  }
  ```
* Also collapse empty case blocks in switch expressions.
  ```diff
   switch (e) {
  -  case CASE_A -> {
  -  }
  +  case CASE_A -> {}
   }
  ```

Fixes #558

COPYBARA_INTEGRATE_REVIEW=#558 from fawind:fw/wrap-case-arguments ff60675
PiperOrigin-RevId: 369513714
  • Loading branch information
fawind authored and google-java-format Team committed Apr 20, 2021
1 parent 32b63aa commit 72672b0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
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 -> {}
};
}
}

0 comments on commit 72672b0

Please sign in to comment.