Skip to content

Commit d157ec8

Browse files
cushongoogle-java-format Team
authored andcommitted
Handle var in record patterns
#1020 PiperOrigin-RevId: 595537430
1 parent 38de9c4 commit d157ec8

5 files changed

Lines changed: 48 additions & 16 deletions

File tree

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,14 +3475,15 @@ private static boolean expressionsAreParallel(
34753475

34763476
// General helper functions.
34773477

3478-
enum DeclarationKind {
3478+
/** Kind of declaration. */
3479+
protected enum DeclarationKind {
34793480
NONE,
34803481
FIELD,
34813482
PARAMETER
34823483
}
34833484

34843485
/** Declare one variable or variable-like thing. */
3485-
int declareOne(
3486+
protected int declareOne(
34863487
DeclarationKind kind,
34873488
Direction annotationsDirection,
34883489
Optional<ModifiersTree> modifiers,

core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.googlejavaformat.OpsBuilder;
2323
import com.google.googlejavaformat.OpsBuilder.BlankLineWanted;
2424
import com.google.googlejavaformat.java.JavaInputAstVisitor;
25-
import com.sun.source.tree.AnnotationTree;
2625
import com.sun.source.tree.BindingPatternTree;
2726
import com.sun.source.tree.BlockTree;
2827
import com.sun.source.tree.CaseLabelTree;
@@ -84,18 +83,18 @@ public Void visitBindingPattern(BindingPatternTree node, Void unused) {
8483

8584
private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) {
8685
builder.open(plusFour);
87-
if (modifiers != null) {
88-
List<AnnotationTree> annotations =
89-
visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty());
90-
visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES);
91-
}
92-
scan(type, null);
93-
builder.breakOp(" ");
94-
if (name.isEmpty()) {
95-
token("_");
96-
} else {
97-
visit(name);
98-
}
86+
declareOne(
87+
DeclarationKind.PARAMETER,
88+
Direction.HORIZONTAL,
89+
Optional.of(modifiers),
90+
type,
91+
name,
92+
/* op= */ "",
93+
/* equals= */ "",
94+
/* initializer= */ Optional.empty(),
95+
/* trailing= */ Optional.empty(),
96+
/* receiverExpression= */ Optional.empty(),
97+
/* typeWithDims= */ Optional.empty());
9998
builder.close();
10099
}
101100

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class FormatterIntegrationTest {
6161
"I880",
6262
"Unnamed",
6363
"I981",
64-
"StringTemplate")
64+
"StringTemplate",
65+
"I1020")
6566
.build();
6667

6768
@Parameters(name = "{index}: {0}")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public sealed interface A {
2+
record AA(Long a) implements A {}
3+
record AAA(String b) implements A {}
4+
static Long mySwitch(A a) {
5+
switch (a) {
6+
case AA(var aa) -> {
7+
return aa;
8+
}
9+
case AAA(var b) -> {
10+
return Long.parseLong(b);
11+
}
12+
}
13+
}
14+
}
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public sealed interface A {
2+
record AA(Long a) implements A {}
3+
4+
record AAA(String b) implements A {}
5+
6+
static Long mySwitch(A a) {
7+
switch (a) {
8+
case AA(var aa) -> {
9+
return aa;
10+
}
11+
case AAA(var b) -> {
12+
return Long.parseLong(b);
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)