From 7754f5fadb1e421e82705bd8ca15fdc51e4f74db Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 3 Jan 2024 15:21:03 -0800 Subject: [PATCH] Handle `var` in record patterns https://github.com/google/google-java-format/issues/1020 PiperOrigin-RevId: 595517489 --- .../java/JavaInputAstVisitor.java | 5 ++-- .../java/java17/Java17InputAstVisitor.java | 25 +++++++++---------- .../java/FormatterIntegrationTest.java | 3 ++- .../java/testdata/I1020.input | 15 +++++++++++ .../java/testdata/I1020.output | 16 ++++++++++++ 5 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1020.output 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); + } + } + } +}