-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
Improve formatting of method will lot of parameters and with exception #286
Comments
I agree with you. I am struggling with one case: how would you format the following ? public void test() throws Exception1, Exception2, Exception3, Exception4, Exception5, Exception6 {} Option 1: public void test()
throws Exception1, Exception2, Exception3, Exception4, Exception5, Exception6 {} Option 2: public void test() throws
Exception1, Exception2, Exception3, Exception4, Exception5, Exception6 {} Option 3: public void test() throws Exception1,
Exception2,
Exception3,
Exception4,
Exception5,
Exception6 {} Option 4: public void test() throws
Exception1,
Exception2,
Exception3,
Exception4,
Exception5,
Exception6 {} Option 5: public void test() throws Exception1, Exception2, Exception3, Exception4, Exception5, Exception6 {} |
Does this formatting looks good ? class T {
public void test(String one, String one, String one, String one, String one, String one) {
}
public void test(String one, String one, String one, String one, String one, String one) throws Exception, Exception, Exception {
}
public void test(String one, String one, String one, String one, String one, String one) throws Exception {
}
public void test(String one, String one) throws Exception, Exception, Exception, Exception {
}
public void test() throws Exception1, Exception2, Exception3, Exception4, Exception5, Exception6 {
}
public void test(String one, String one) throws Exception {
}
} into class T {
public void test(
String one,
String one,
String one,
String one,
String one,
String one
) {}
public void test(
String one,
String one,
String one,
String one,
String one,
String one
) throws Exception, Exception, Exception {}
public void test(
String one,
String one,
String one,
String one,
String one,
String one
) throws Exception {}
public void test(String one, String one) throws
Exception,
Exception,
Exception,
Exception {}
public void test() throws
Exception1,
Exception2,
Exception3,
Exception4,
Exception5,
Exception6 {}
public void test(String one, String one) throws Exception {}
} |
Outside perspective: Black (the Python formatter) does it like the prettier 0.4.0 example, so even if it doesn’t look great, it has precedence in the “code formatting” community. |
Just to confirm. The
|
In my proposal, it would have a simple indent. I don't think it's a problem if we the same indent level for body and |
👍 the Option B style proposed here looks good to me #286 (comment) |
How about this:
|
Hi @digorydoo, thank you for your feedbacks ! I definitively need to look at this. I'll try to work on this next week |
I'm sure this discussion has happened before, but I'm not finding it anywhere. Given this:
Why can't this be done?
Same thing with method parameters.
Essentially this: prettier/prettier#6573 but for Java |
@MichielBugherJelli suggestion looks pretty good to me. Why keep every element to next line once reached print width. A smart wrap respecting print width value would be perfect. |
Prettier is an opinionated formatter, and it seems like one of those opinions is that either all elements in a group should line break, or none should (and prettier-java inherits this behavior from prettier since it's a prettier plugin). The example above might look tidy because all of the arguments are the same type and happen to align nicely, but it quickly becomes text soup when that's not the case, for example: public void doRemoteFetch(RemoteDataFetcher remoteDataFetcher,
JsonDeserializer jsonDeserializer, CustomHeadersProvider customHeadersProvider,
ResponseSignatureVerifier responseSignatureVerifier, ErrorHandlingStrategy errorHandlingStrategy
)
vs.
public void doRemoteFetch(
RemoteDataFetcher remoteDataFetcher,
JsonDeserializer jsonDeserializer,
CustomHeadersProvider customHeadersProvider,
ResponseSignatureVerifier responseSignatureVerifier,
ErrorHandlingStrategy errorHandlingStrategy
) But like I said before, this style is just an opinion so there's not much use in debating which way is objectively "right". However, this opinion is pretty much at the core of prettier and unlikely to change. If you don't like prettier's opinions, google-java-format might be of interest, it's another formatter that's a bit less opinionated and also has IDE plugins available. |
Source:
Formatted with prettier 0.4.0:
I'm not really sure what the result should be, but the position of
)
after the last parameter is a bit strange in regards to the exception.Maybe:
The text was updated successfully, but these errors were encountered: