From 9d7e35ee27bd698142e0a85afb1b6c6f2ae36696 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Mon, 5 Feb 2024 19:27:10 -0800 Subject: [PATCH] Handle 'any' patterns Fixes https://github.com/google/google-java-format/issues/1037 PiperOrigin-RevId: 604505236 --- .../java/java21/Java21InputAstVisitor.java | 18 ++++++++++++++++++ .../java/FormatterIntegrationTest.java | 3 ++- .../googlejavaformat/java/testdata/I1037.input | 11 +++++++++++ .../java/testdata/I1037.output | 11 +++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java index 8ab04e665..2192a32a9 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java @@ -24,6 +24,8 @@ import com.sun.source.tree.PatternCaseLabelTree; import com.sun.source.tree.PatternTree; import com.sun.source.tree.StringTemplateTree; +import com.sun.source.tree.Tree; +import com.sun.tools.javac.tree.JCTree; import javax.lang.model.element.Name; /** @@ -107,4 +109,20 @@ protected void variableName(Name name) { visit(name); } } + + @Override + public Void scan(Tree tree, Void unused) { + // Pre-visit AST for preview features, since com.sun.source.tree.AnyPattern can't be + // accessed directly without --enable-preview. + if (tree instanceof JCTree.JCAnyPattern) { + visitJcAnyPattern((JCTree.JCAnyPattern) tree); + return null; + } else { + return super.scan(tree, unused); + } + } + + private void visitJcAnyPattern(JCTree.JCAnyPattern unused) { + token("_"); + } } 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 26493b022..15a6e75f9 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -62,7 +62,8 @@ public class FormatterIntegrationTest { "Unnamed", "I981", "StringTemplate", - "I1020") + "I1020", + "I1037") .build(); @Parameters(name = "{index}: {0}") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input new file mode 100644 index 000000000..ed39459dd --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.input @@ -0,0 +1,11 @@ +public sealed interface A { + record AA(Long a) implements A {} + + static Long mySwitch(A a) { + switch (a) { + case AA(_) -> { + return 1L; + } + } + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output new file mode 100644 index 000000000..ed39459dd --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1037.output @@ -0,0 +1,11 @@ +public sealed interface A { + record AA(Long a) implements A {} + + static Long mySwitch(A a) { + switch (a) { + case AA(_) -> { + return 1L; + } + } + } +}