Skip to content

Commit

Permalink
Handle 'any' patterns
Browse files Browse the repository at this point in the history
Fixes #1037

PiperOrigin-RevId: 604505236
  • Loading branch information
cushon authored and google-java-format Team committed Feb 12, 2024
1 parent 1ae69f9 commit ebccc2c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.googlejavaformat.java.java21;

import com.google.errorprone.annotations.Keep;
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor;
import com.sun.source.tree.CaseTree;
Expand All @@ -24,6 +25,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;

/**
Expand Down Expand Up @@ -107,4 +110,21 @@ 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);
}
}

@Keep
private void visitJcAnyPattern(JCTree.JCAnyPattern tree) {
token("_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public class FormatterIntegrationTest {
"Unnamed",
"I981",
"StringTemplate",
"I1020")
"I1020",
"I1037")
.build();

@Parameters(name = "{index}: {0}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit ebccc2c

Please sign in to comment.