-
Notifications
You must be signed in to change notification settings - Fork 460
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
Import Sorter to cope with multi-line comments and misplaced imports #112
Conversation
I looked at this briefly. The modifications look good, but the original code is kinda spaghetti so I'm not sure if it introduced a subtle bug or not (maybe even fixed some, who knows!). For code like this, it earns my trust by simple mileage - run it on a ton of code and make sure nothing breaks. Once this gets merged to master so that it shows up in @jbduncan tends to have a more careful eye than me, so I'll defer to him :) |
I was wondering whether it is time to put the I ran the new version on junit, for which I need it in the first place. |
Fantastic! Good enough for me 👍
The problem with naive pseudo-parsers such as I think you made the right call re: refactoring. |
That is also the point why I did not open a new child-class. You can of course implement more and more features to come closer to a real syntax aware implementation. But this cannot be the goal. |
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.
Although I do have a comment about a code block within ImportSorter.java
, it's not clear to me that this change is wise.
google-java-format
, which Spotless supports, purposely throws an exception upon encountering comments within the imports section, since it's not clear whether the comment was purposely inserted or not, since it may document something important, so it should be up to a human to decide whether it should be removed or placed elsewhere in the file.
isMultiLineComment = false; | ||
if (!next.contains("/*")) { | ||
continue; | ||
} |
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.
I'm not entirely sure that this if..continue
block is needed.
AFAICT, !next.contains("/*")
can never be true at this point. I can explain my reasoning if you wish, but it's a bit long-winded, I've not tested it with JUnit or a debugger, and there's no harm to this block staying anyway AFAICT.
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.
You have this situation in the following case:
/* //isMultiLineComment = true
import what.so.ever */ //isMultiLineComment = false
In the old code what.so.ever
would have been part of the refactored imports, so it was originally commented out.
If I refactor it to an inner class, it becomes easier to read. But at first I wanted to assure you, that I actually did not alter much in the initial logic of the method.
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.
In the old code
what.so.ever
would have been part of the refactored imports, so it was originally commented out.
Sorry, I don't understand what this sentence is saying. Can you rephrase it for me?
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.
Original behavior:
import i.need.this
/*
import i.don.t.need.that */
Results in
import i.need.this
import i.don.t.need.that
New behavior detects multi-line comments. So result is:
import i.need.this
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.
Sorry, just saw my mistake. I wanted to say:
In the old code what.so.ever would
have been part of the refactored imports, though it was originally commented out.
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.
Aha I see now. Thanks for the clarifications @fvgh. :)
Hi @fvgh, as I mentioned in my review, it's not clear to me that this code change is wise.
But then again, most users (if not all users) of Spotless will also be using a VCS like Git, so they can revert or change commits if an important comment was accidentally removed. So maybe this isn't such a big problem... 🤔 |
This is an option. I thought that the current behavior to eat comments was intended, so I did not change it. But let's do that in a separated PR, since it is unrelated to my changes.
I think if you run a code formatter over your complete code without source control, well, ... Refactoring the code and use an inner class is definitely an option here. But in the current form I think it is easier to see what I changed in the behavior. So for me it is important that we have both a common understanding of the behavioral change before I propose a refactored code. |
Aha, I didn't realise that. Let's keep the comment-eating behaviour then. I'm fine with the tests, and it looks like they pass, so no problems there. |
Basically, this LGTM now. 👍 |
The change shall enhance the robustness of the
java.ImportOrderStep
step.I admit that I basically need it to use it on Groovy code (see #13), but it is also an issue in the java world:
If you comment out imports, the
java.ImportOrderStep
step will ignore the multi-line comment and use these imports in its sorted listIf you have misplaced imports (still working on code, or using groovy), the
java.ImportOrderStep
will (delete all code in between)[java.ImportOrderStep].Two improvements:
ImportSorter
stops looking forimport
statements, as soon as it finds the beginning of a scope.{
,import
) within multi-line comments.