diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index ea967b3d8..8914c8ea2 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -3475,14 +3475,15 @@ private static boolean expressionsAreParallel( // General helper functions. - enum DeclarationKind { + /** Kind of declaration. */ + protected enum DeclarationKind { NONE, FIELD, PARAMETER } /** Declare one variable or variable-like thing. */ - int declareOne( + protected int declareOne( DeclarationKind kind, Direction annotationsDirection, Optional modifiers, 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 6818f4a03..a7b5dbc44 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 @@ -22,7 +22,6 @@ import com.google.googlejavaformat.OpsBuilder; import com.google.googlejavaformat.OpsBuilder.BlankLineWanted; import com.google.googlejavaformat.java.JavaInputAstVisitor; -import com.sun.source.tree.AnnotationTree; import com.sun.source.tree.BindingPatternTree; import com.sun.source.tree.BlockTree; import com.sun.source.tree.CaseLabelTree; @@ -84,18 +83,18 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) { private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) { builder.open(plusFour); - if (modifiers != null) { - List annotations = - visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()); - visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES); - } - scan(type, null); - builder.breakOp(" "); - if (name.isEmpty()) { - token("_"); - } else { - visit(name); - } + declareOne( + DeclarationKind.PARAMETER, + Direction.HORIZONTAL, + Optional.of(modifiers), + type, + name, + /* op= */ "", + /* equals= */ "", + /* initializer= */ Optional.empty(), + /* trailing= */ Optional.empty(), + /* receiverExpression= */ Optional.empty(), + /* typeWithDims= */ Optional.empty()); builder.close(); } diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index b31e7bb15..5fb356750 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -61,7 +61,8 @@ public class FormatterIntegrationTest { "I880", "Unnamed", "I981", - "StringTemplate") + "StringTemplate", + "I1020") .build(); @Parameters(name = "{index}: {0}") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.input new file mode 100644 index 000000000..8ff084265 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.input @@ -0,0 +1,15 @@ +public sealed interface A { + record AA(Long a) implements A {} + record AAA(String b) implements A {} + static Long mySwitch(A a) { + switch (a) { + case AA(var aa) -> { + return aa; + } + case AAA(var b) -> { + return Long.parseLong(b); + } + } + } +} + diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.output new file mode 100644 index 000000000..21e4a5156 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.output @@ -0,0 +1,16 @@ +public sealed interface A { + record AA(Long a) implements A {} + + record AAA(String b) implements A {} + + static Long mySwitch(A a) { + switch (a) { + case AA(var aa) -> { + return aa; + } + case AAA(var b) -> { + return Long.parseLong(b); + } + } + } +}