-
Notifications
You must be signed in to change notification settings - Fork 81
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
Crash when comment separate imports #291
Comments
We copied GJF's behavior here; has it changed? |
google-java-format does not crash when comments are in the middle of the import list; I don't know if that is a recent change. The java formatter seems to attach the comment to the import before the comment (which seems backwards to me), and does not change the comment if imports are deleted before or after it. |
Looks like a recent change then. Our current local version crashes:
We can try to fix it, and try to smartly attach a comment to an import, but I think it's a bit problematic: It's unclear which import owns the comment:
Is comment 2 & 1 associated import decided by a newline? And that happens when a user tried to comment a group of imports:
The comment will no longer work for the group after us sorting it. |
In the past, I've found that a good way to approach these decisions is to think about what invariants/goals one wants in a tool. I'd propose the following (incomplete) list, which seems to cover all the concerns presented:
Today, we can say that (4) is satisfied, since currently @bethcutler proposes a behaviour that satisfies the entire list, so that would seem like an improvement. The question is only, whether we agree about the invariants and their definitions. Of course, there could be more invariants, that I missed, which would constrain the correct behaviour even more. One potential question about (2) is "what constitutes input information". It could be argued that rearranging comments deletes something, by changing the association between a comment and an import (or range). A solution there is to define away the problem, by saying that |
Thoughts -
Suggestion:
What do you think? |
If we want to special case commented imports, I think it would be friendlier to sort them along with live imports. But I'm also not sold on why these comments are useful. Extra imports (AFAIK) aren't a build error, and adding imports is a one-click operation in an IDE (text editor users probably aren't running
I don't like that this behaviour is:
|
Let's step back -
On the contrary, they (commented-out imports) aren't useful, and therefore can be moved around, or even deleted.
That... might not always be true in large enough projects :) |
I think this is where we disagree :) With my present experience, I'm willing to declare comments on imports as zero-information. Not only is this convenient for the
Haha, sorry. I must have let the Google bubble get to me here. Our Kotlin+Java projects are often 100k bazel targets in size, and imports are still one-click. |
Me and @cgrushko chatted today and we think the proposed solution from @bethcutler is 👍. So we will just go ahead with approving that. Thanks! |
leads to error:
We think it is not ideal for the formatter to crash on legal, compilable code. It's also not good to remove comments, or to attach them to the next line (what if the next line is an unused import that would be removed during formatting?)
We propose sorting all the comments within the import list to the top of the import list. It may not be exactly what the user intended, but it's not bad. What do you think?
The text was updated successfully, but these errors were encountered: