Skip to content

Commit

Permalink
Allow breaks before pattern guards
Browse files Browse the repository at this point in the history
Previously there were never breaks before `when` pattern guards, which produced surprising results.

There's possible future work here around evaluating how many level of continuation indents to take to differentiate between syntactic levels, but I think this is an improvement on the status quo.

PiperOrigin-RevId: 696193971
  • Loading branch information
cushon authored and google-java-format Team committed Nov 13, 2024
1 parent 453ad08 commit 7e5fb80
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
}

private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
builder.open(plusFour);
declareOne(
DeclarationKind.PARAMETER,
Direction.HORIZONTAL,
Expand All @@ -95,7 +94,6 @@ private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name)
/* trailing= */ Optional.empty(),
/* receiverExpression= */ Optional.empty(),
/* typeWithDims= */ Optional.empty());
builder.close();
}

@Override
Expand Down Expand Up @@ -227,16 +225,12 @@ public Void visitCase(CaseTree node, Void unused) {
List<? extends CaseLabelTree> labels = node.getLabels();
boolean isDefault =
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
builder.open(
node.getCaseKind().equals(CaseTree.CaseKind.RULE)
&& !node.getBody().getKind().equals(Tree.Kind.BLOCK)
? plusFour
: ZERO);
builder.open(node.getCaseKind().equals(CaseTree.CaseKind.RULE) ? plusFour : ZERO);
if (isDefault) {
token("default", ZERO);
} else {
token("case", ZERO);
builder.open(labels.size() > 1 ? plusFour : ZERO);
builder.open(ZERO);
builder.space();
boolean afterFirstToken = false;
for (Tree expression : labels) {
Expand All @@ -252,7 +246,7 @@ public Void visitCase(CaseTree node, Void unused) {

final ExpressionTree guard = getGuard(node);
if (guard != null) {
builder.space();
builder.breakToFill(" ");
token("when");
builder.space();
scan(guard, null);
Expand All @@ -264,12 +258,14 @@ public Void visitCase(CaseTree node, Void unused) {
builder.open(plusTwo);
visitStatements(node.getStatements());
builder.close();
builder.close();
break;
case RULE:
builder.space();
token("-");
token(">");
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
builder.close();
builder.space();
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
visitBlock(
Expand All @@ -280,13 +276,13 @@ public Void visitCase(CaseTree node, Void unused) {
} else {
builder.breakOp(" ");
scan(node.getBody(), null);
builder.close();
}
builder.guessToken(";");
break;
default:
throw new AssertionError(node.getCaseKind());
}
builder.close();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,42 @@ class SwitchGuardClause {
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName,
TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName)
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName,
TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName)
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,63 @@ class SwitchGuardClause {

{
switch (o) {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryLongName when variableWithVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryLongName
when variableWithVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
case TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName,
TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName)
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> {
System.err.println();
}
default -> {}
}
switch (o) {
case TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName,
TypeWithVeryVeryVeryVeryLongName
variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName)
when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName
.methodWithVeryVeryVeryVeryLongNameReturnThis()
.methodWithVeryVeryVeryVeryLongNameReturnBoolean() ->
System.err.println();
Expand Down

0 comments on commit 7e5fb80

Please sign in to comment.