Skip to content

Commit

Permalink
Handle var in record patterns
Browse files Browse the repository at this point in the history
#1020

PiperOrigin-RevId: 595517489
  • Loading branch information
cushon authored and google-java-format Team committed Jan 4, 2024
1 parent 38de9c4 commit 7754f5f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModifiersTree> modifiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AnnotationTree> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public class FormatterIntegrationTest {
"I880",
"Unnamed",
"I981",
"StringTemplate")
"StringTemplate",
"I1020")
.build();

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

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}

0 comments on commit 7754f5f

Please sign in to comment.