diff --git a/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java index a0037edb7..315523585 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java @@ -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, @@ -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 @@ -227,16 +225,12 @@ public Void visitCase(CaseTree node, Void unused) { List 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) { @@ -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); @@ -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( @@ -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; } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input index 36eb21b16..0f4b485b8 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input @@ -27,7 +27,7 @@ class SwitchGuardClause { } switch (o) { case TypeWithVeryVeryVeryVeryLongName variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName - when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName + when variableWithVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongName .methodWithVeryVeryVeryVeryLongNameReturnThis() .methodWithVeryVeryVeryVeryLongNameReturnBoolean() -> { System.err.println(); @@ -35,18 +35,34 @@ class SwitchGuardClause { 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() -> diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output index 1a81422c4..ac0961d03 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output @@ -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();