Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unnamed variables #3626

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/delombok/lombok/delombok/PrettyPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2021 The Project Lombok Authors.
* Copyright (C) 2016-2024 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -263,6 +263,14 @@ private boolean suppress(JCTree tree) {
return false;
}

private void print(Name name) {
if (name.isEmpty()) {
print("_");
} else {
print(name.toString());
}
}

private void print(CharSequence s) {
boolean align = needsAlign;
if (needsNewLine && !onNewLine) println();
Expand Down Expand Up @@ -1722,6 +1730,8 @@ public void visitTypeBoundKind(TypeBoundKind tree) {
printPatternCaseLabel(tree);
} else if (className.endsWith("$JCRecordPattern")) { // Introduced in JDK19
printRecordPattern(tree);
} else if (className.endsWith("$JCAnyPattern")) { // Introduced in JDK22
print("_");
} else {
throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree);
}
Expand Down
70 changes: 70 additions & 0 deletions test/pretty/resource/after/UnnamedVariables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// version 22:
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;

record Box<T>(T content) {
}

record Pair<T, U>(T first, U second) {
}

public class UnnamedVariables {
void enhancedLoop() {
for (String _ : Arrays.asList("")) {
}
}

void initializationInForLoop() {
for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) {
}
}

void assignment() {
var _ = 1;
}

void catchBlock() {
try {
} catch (Exception _) {
}
}

void tryWithResources() {
try (var _ = new Scanner("")) {
}
}

void lambda() {
Stream.of(1).forEach(_ -> {
});
}

Object switchStatement(Object o) {
return switch (o) {
case String _, Integer _ -> o;
case Long _ -> o;
default -> o;
};
}

void recordPattern(Object o) {
if (o instanceof Pair(Box _, Box(Integer _))) {
}
if (o instanceof Box(String _)) {
}
if (o instanceof Box(var _)) {
}
if (o instanceof Box(_)) {
}
}

Object recordPatternSwitch(Object o) {
return switch (o) {
case Box(String _), Box(Integer _) -> o;
case Box(Long _) -> o;
case Box(_) -> o;
default -> o;
};
}
}
70 changes: 70 additions & 0 deletions test/pretty/resource/before/UnnamedVariables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// version 22:
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;

record Box<T>(T content) {
}

record Pair<T, U>(T first, U second) {
}

public class UnnamedVariables {
void enhancedLoop() {
for (String _ : Arrays.asList("")) {
}
}

void initializationInForLoop() {
for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) {
}
}

void assignment() {
var _ = 1;
}

void catchBlock() {
try {
} catch (Exception _) {
}
}

void tryWithResources() {
try (var _ = new Scanner("")) {
}
}

void lambda() {
Stream.of(1).forEach(_ -> {
});
}

Object switchStatement(Object o) {
return switch (o) {
case String _, Integer _ -> o;
case Long _ -> o;
default -> o;
};
}

void recordPattern(Object o) {
if (o instanceof Pair(Box _, Box(Integer _))) {
}
if (o instanceof Box(String _)) {
}
if (o instanceof Box(var _)) {
}
if (o instanceof Box(_)) {
}
}

Object recordPatternSwitch(Object o) {
return switch (o) {
case Box(String _), Box(Integer _) -> o;
case Box(Long _) -> o;
case Box(_) -> o;
default -> o;
};
}
}
Loading