Skip to content

Commit

Permalink
Initial support for pattern matching in switches
Browse files Browse the repository at this point in the history
  • Loading branch information
cushon authored and eholmer1 committed Oct 21, 2024
1 parent 065c379 commit 2f58013
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ConstantCaseLabelTree;
import com.sun.source.tree.DeconstructionPatternTree;
import com.sun.source.tree.DefaultCaseLabelTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.PatternCaseLabelTree;
import com.sun.source.tree.PatternTree;

/**
* Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in
Expand All @@ -33,4 +38,42 @@ public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
protected ExpressionTree getGuard(final CaseTree node) {
return node.getGuard();
}

@Override
public Void visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused) {
token("default");
return null;
}

@Override
public Void visitPatternCaseLabel(PatternCaseLabelTree node, Void unused) {
scan(node.getPattern(), null);
return null;
}

@Override
public Void visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid) {
scan(node.getConstantExpression(), null);
return null;
}

@Override
public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unused) {
scan(node.getDeconstructor(), null);
builder.open(plusFour);
token("(");
builder.breakOp();
boolean first = true;
for (PatternTree pattern : node.getNestedPatterns()) {
if (!first) {
token(",");
builder.breakOp(" ");
}
first = false;
scan(pattern, null);
}
builder.close();
token(")");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,18 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
}

private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
builder.open(plusFour);
if (modifiers != null) {
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
}
scan(type, null);
builder.breakOp(" ");
visit(name);
if (name.isEmpty()) {
token("_");
} else {
visit(name);
}
builder.close();
}

@Override
Expand Down Expand Up @@ -259,6 +265,11 @@ public Void visitCase(CaseTree node, Void unused) {
labels = node.getExpressions();
isDefault = labels.isEmpty();
}
builder.open(
node.getCaseKind().equals(CaseTree.CaseKind.RULE)
&& !node.getBody().getKind().equals(Tree.Kind.BLOCK)
? plusFour
: ZERO);
if (isDefault) {
token("default", plusTwo);
} else {
Expand Down Expand Up @@ -301,15 +312,16 @@ public Void visitCase(CaseTree node, Void unused) {
builder.space();
token("-");
token(">");
builder.space();
if (node.getBody().getKind() == BLOCK) {
builder.space();
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
visitBlock(
(BlockTree) node.getBody(),
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.NO,
AllowTrailingBlankLine.NO);
} else {
builder.breakOp(" ");
scan(node.getBody(), null);
}
builder.guessToken(";");
Expand All @@ -318,6 +330,7 @@ public Void visitCase(CaseTree node, Void unused) {
default:
throw new IllegalArgumentException(node.getCaseKind().name());
}
builder.close();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import com.palantir.javaformat.OpsBuilder;
import com.palantir.javaformat.java.java14.Java14InputAstVisitor;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ConstantCaseLabelTree;
import com.sun.source.tree.DeconstructionPatternTree;
import com.sun.source.tree.DefaultCaseLabelTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.PatternCaseLabelTree;
import com.sun.source.tree.PatternTree;

/**
* Extends {@link Java14InputAstVisitor} with support for AST nodes that were added or modified in
* Java 21.
Expand All @@ -33,4 +39,42 @@ public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
protected ExpressionTree getGuard(final CaseTree node) {
return node.getGuard();
}

@Override
public Void visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused) {
token("default");
return null;
}

@Override
public Void visitPatternCaseLabel(PatternCaseLabelTree node, Void unused) {
scan(node.getPattern(), null);
return null;
}

@Override
public Void visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid) {
scan(node.getConstantExpression(), null);
return null;
}

@Override
public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unused) {
scan(node.getDeconstructor(), null);
builder.open(plusFour);
token("(");
builder.breakOp();
boolean first = true;
for (PatternTree pattern : node.getNestedPatterns()) {
if (!first) {
token(",");
builder.breakOp(" ");
}
first = false;
scan(pattern, null);
}
builder.close();
token(")");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class FileBasedTests {
.putAll(15, "I603")
.putAll(16, "I588")
.putAll(17, "I683", "I684", "I696")
.putAll(21, "SwitchGuardClause")
.putAll(21, "SwitchGuardClause", "SwitchRecord", "SwitchDouble", "SwitchUnderscore", "I880")
.build();

private final Class<?> testClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class I880 {
public String f(int i) {
return switch (i) {
case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -> "looooooooooooooooooooooooooooooooooooooooong expression";
default -> "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong expression";
};
}

public boolean test(int i) {
return switch (i) {
case 0 -> // zero
false;
case 1 -> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".length()
== 0;
default -> // otherwise
true;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class I880 {
public String f(int i) {
return switch (i) {
case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -> "looooooooooooooooooooooooooooooooooooooooong expression";
default -> "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong expression";
};
}

public boolean test(int i) {
return switch (i) {
case 0 -> // zero
false;
case 1 -> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".length() == 0;
default -> // otherwise
true;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SwitchDouble {
void x(Object o) {
switch (o) {
case null, default:
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SwitchDouble {
void x(Object o) {
switch (o) {
case null, default:
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
record SwitchRecord(int i) {
int x(Object o) {
return switch (o) {
case SwitchRecord(int i) -> i;
default -> 0;
};
}
int f(Object o) {
return switch (o) {
case SwitchRecord(int one, int two, int three, int four, int five, int six, int seven, int eight, int nine) -> nine;
default -> 0;
};
}
int g(Object o) {
return switch (o) {
case SwitchRecord(int one, int two, int three, int four, int five, int six, int seven, int eight, int nine) ->
System.err.println("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
default -> 0;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
record SwitchRecord(int i) {
int x(Object o) {
return switch (o) {
case SwitchRecord(int i) -> i;
default -> 0;
};
}

int f(Object o) {
return switch (o) {
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int nine) -> nine;
default -> 0;
};
}

int g(Object o) {
return switch (o) {
case SwitchRecord(
int one,
int two,
int three,
int four,
int five,
int six,
int seven,
int eight,
int nine) -> System.err.println(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
+ " incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis"
+ " nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo"
+ " consequat.");
default -> 0;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class SwitchUnderscore {
void x(Object o) {
switch (o) {
case String _:
default:
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class SwitchUnderscore {
void x(Object o) {
switch (o) {
case String _:
default:
}
}
}

0 comments on commit 2f58013

Please sign in to comment.