-
Notifications
You must be signed in to change notification settings - Fork 855
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
Suboptimal line breaks for long switch rules (with -> arrows) #880
Comments
Some overall thoughts on arrowform switch that y'all can pick apart
|
I found another suboptimal formatting that is related to this, namely if there is a line comment right after the arrow. In this case, google-java-format misses to add indentation: public class Test {
public boolean test(int i) {
return switch (i) {
case 0 -> // zero
false;
case 1 -> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".length()
== 0;
default -> // otherwise
true;
};
}
} Together, these issues (plus #876) make using arrow rules not so nice currently, so any improvement here would be highly appreciated! We would likely move over to arrow rules completely in our code base once formatting works fine. |
I have some work-in-progress improvements to switch rule handling that result in the following for these examples. This output is non-final but I think it's a step in the right direction. There are also likely more issues lurking with the new features, so reports/feedback welcome if you see cases that aren't handled well with the new features! 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 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;
};
}
} |
Thanks! |
Fixes google/google-java-format#937, google/google-java-format#880 PiperOrigin-RevId: 589140113
Fixes google/google-java-format#937, google/google-java-format#880 PiperOrigin-RevId: 589140113
For the following code, I would expect line breaks to be added at the arrows (
->
) of the rules of the switch expression:However, google-java-format 1.15.0 formats it like this:
It even prefers breaking up the string over a line break at the arrow. Please increase the priority of line breaking at the arrow. I guess it should be of higher priority than the list of cases and anything within the expression.
The text was updated successfully, but these errors were encountered: