-
-
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
fix: throws rendering #384
Conversation
Updated test output to match current behavior so the tests pass, here's the diff: There's still an extra newline but only when the throws clause is really long |
@@ -511,7 +506,8 @@ class ClassesPrettierVisitor { | |||
|
|||
throws(ctx) { | |||
const exceptionTypeList = this.visit(ctx.exceptionTypeList); | |||
return join(" ", [ctx.Throws[0], exceptionTypeList]); | |||
const throwsDeclaration = join(" ", [ctx.Throws[0], exceptionTypeList]); | |||
return group(indent(rejectAndConcat([softline, throwsDeclaration]))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add an extra indent here, so that the throws + exceptionTypeList is not at the same level of the rest of the body, WDYT ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, would that be the only place in prettier-java where a double-indent is used? There is a similar issue with a really long class declaration, for example:
public class MyReallyLongClassName
extends SomeOtherReallyLongClass
implements SomeReallyLongInterface {
private Field field;
public Field someMethodWithAReallyLongName()
throws IOException {
return field;
}
}
I wonder if the fix in both cases could be to force a newline if the class signature or throws clause linebreaks? This would look like:
public class MyReallyLongClassName
extends SomeOtherReallyLongClass
implements SomeReallyLongInterface {
private Field field;
public Field someMethodWithAReallyLongName()
throws IOException {
return field;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other places where we could add some double indents indeed.
It might indeed be better to add an extra blank line, but it is more difficult to implement, IMO, as the formatting of the class body is not in the same node as the inheritance/interface declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be easier if class bodies always started with an empty line? (basically #271)
I guess that wouldn't help the method case though, since you wouldn't want methods to unconditionally start with an empty line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also getting back to the original suggestion, double indent (or continuation indent) is pretty common in Java formatters but doesn't seem to be idiomatic in prettier as far as I can tell (this issue didn't seem to go anywhere prettier/prettier#5865).
If we introduced the idea of continuation indent, I think we'd want to apply it consistently. Off the top of my this would include class signatures:
public class MyReallyLongClassName
->extends SomeOtherReallyLongClass
->implements SomeReallyLongInterface {
Method signatures:
public void myMethod(
->String argOne
->String argTwo
->String argThree
) {}
Method call chains:
return someCollectionType
->.stream()
->.filter(x -> x != null)
->.collect(Collectors.toList());
And probably a bunch of other statements as well. This is definitely one way to go, but would be a pretty massive formatting change at this point. Might be better to move discussion to an issue if that's under consideration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Nooo, not in method call chains - those are exactly the type of code that often becomes confusing because there's a continuation on top, and a chained call below, but they look the same. Make split expressions use the continuation indent, and chained calls the "normal", +1, indent...)
Thank you for this @jhaber! One minor comment and we can merge it :) |
Thanks! There is still still an extraneous newline when the throws clause is really long, for example: I have a hunch I need to use |
Any more thoughts on this PR? We've been formatting all of our code with this change since April and haven't noticed any undesirable side effects. |
👋 bump on this PR. We're continuing to apply it on top of each prettier-java release for our internal use, but it is a bit of a maintenance burden (and also everyone else doesn't get to benefit 😄 ) |
I'll think I merge it next week. I will be on holidays so I'll finally have some time :) |
@Shaolans @Hawkurane : can one of you have a quick look and merge this plz ? |
Sorry it took so long @jhaber. I'm merging this. Thank you a lot ! |
This is a modified version of #305 that makes a more minor change. Based on #286 it seems like there's not a clear answer on how formatting should work when methods throw lots of exceptions. However, the current behavior where there's an extra newline is basically a bug, for example:
This PR attempts to fix the bug, while punting on the question of a more complete behavior change.
The tests should give a good idea of the behavior I was going for. Unfortunately, my prettier fu is pretty weak so a few of the tests are failing and I'm having a hard time getting things just right