-
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
ktfmt rearranges comments on top of imports statements #300
Comments
We had a discussion about this change #291 Previously, the example input would have crashed ktfmt. There are more complex approaches that could be used to address the crash, but none of them seemed worth the trade offs. As a workaround, you should be able to suppress using |
As a workaround its fine, but it's a little heavy handed (suprresses for the entire file), but anyway. just wanted to get it on your radar. thanks |
For future readers, I'd also point out that these comments are a bit of a hack in Android Lint. AL primarily expresses suppressions using annotations (i.e. From personal experience, I can say that encoding metadata in comments leads to sad times. |
Fair point. Filed this if you'd like to star. https://issuetracker.google.com/issues/225529525 |
The word from the lint team is "Kotlin syntax doesn't allow annotations on imports" |
That comment was from me. Both the AL team and I work at Google.
|
I think we should revert this change as it causes data corruption for some users. We can still implement my suggestion of detecting whether a comment looks like an import, which should avoid cases like this issue. Thoughts? |
For this example, I'd describe reverting as caving to a hack in another project. We can't have all these simultaneously:
|
If there's some structure for this comments that we can handle, I'm down with doing that. Reverting the change won't solve the problem - I think Ktfmt will just not format the file, so it's as bad as the heavy handed solution. @nreid260 , @ColtonIdle - what does Google Java Format do in the same case for you guys? My go to will be to have the tools do the same. |
From what I can tell, GJF "glues" comments to the preceding import:
This has weird effects though:
I think what's going on is GJF optimizes for trailing comments, and sees comments on the next line as a degenerate form of trailing.
Example of a bad thing that happens:
|
Yeah, that's not that useful. Can you find the POC for GJF and question them about how they got to this? Maybe they can shed some context instead of us going through all the fixes & comments they went through. Happy to chat with them myself if you know who that is. |
From talking to the current maintainers:
At this point, if someone wants to make ktfmt behave like GJF, with gluing, that seems fine to me. As long as ktfmt doesn't crash, or delete the comments, it works for us. Just be aware that there's also weirdness in that approach. Fundamentally, associating comments with other syntax is unspecified, so there will always be tools that chose to do it in a different way. |
Let's say I have
ktfmt reformats it as
This is problematic because some linting tools use a comment to opt out of errors on an import statement. (for example android lint allows you to opt out of a import error with a comment, but then you run ktfmt and it rearranges the comments therefore causing lint to break again.
The text was updated successfully, but these errors were encountered: