-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for pattern matching in switches
Fixes google/google-java-format#937, google/google-java-format#880 PiperOrigin-RevId: 589140113
- Loading branch information
Showing
12 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I880.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/I880.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...r-java-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchDouble.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...-java-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchDouble.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...r-java-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchRecord.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...-java-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchRecord.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...va-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchUnderscore.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...a-format/src/test/resources/com/palantir/javaformat/java/testdata/SwitchUnderscore.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
} | ||
} | ||
} |