Skip to content
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

Ability to configure static import sorting to match behaviour of IntelliJ IDEA #879

Closed
hakanai opened this issue Jun 16, 2021 · 5 comments · Fixed by #954
Closed

Ability to configure static import sorting to match behaviour of IntelliJ IDEA #879

hakanai opened this issue Jun 16, 2021 · 5 comments · Fixed by #954

Comments

@hakanai
Copy link
Contributor

hakanai commented Jun 16, 2021

If you have the following configuration:

importOrder('java', 'javax', '', 'acme', 'com.acme', '\\#')

Spotless will sort the static imports like this:

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static org.hamcrest.Matchers.*;

But IntelliJ IDEA will sort the static imports like this:

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.Matchers.*;

This causes constant fighting between IDEA and Spotless so it would be nice if we could configure spotless to say that the wildcard always comes last.

@nedtwigg
Copy link
Member

PR welcome :) https://github.com/diffplug/spotless/blob/main/lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java

Maybe add a boolean wildcardsLast = false, which can be set to true in order to get the IntellIJ behavior? Related #200

@sbilello
Copy link

sbilello commented Sep 30, 2021

Is there a way to use the same java style used by intellij in spotless? https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml

@hakanai
Copy link
Contributor Author

hakanai commented Oct 1, 2021

@sbilello that's a separate issue but yeah that would probably be ideal for us if it were possible. I believe we were waiting on JetBrains breaking out their reformatting code into a library so that it could be used for that?

@hakanai
Copy link
Contributor Author

hakanai commented Oct 1, 2021

I'm having a look at this at the moment.

I think I have sorted out the way it would work in the DSL:

importOrder('java', 'javax', 'com.acme', '').wildcardsLast()

So I wrote the tests. The test for default ordering passes, the test for wildcardsLast fails. Good, so then I wrote all the code to get that wildcardsLast value into ImportSorterImpl and am trying to figure out where in the code it's actually doing the ordering of wildcard vs. non-wildcard.

I see filterMatchingImports receives the list in unsorted order and ultimately sets
allImportOrderItems to a set with one element, "static ", and notMatching to a list with the same.

Then I've been stepping through and I get into mergeMatchingItems(), by which point template has already been sorted, but I didn't see when that happened.

I guess I expected to see a comparator somewhere in the code, but I don't see one at all, so there is no obvious place to change the comparison logic. I can only guess it's implicitly using the default natural string ordering somewhere.

Never mind, I just found the two calls to Collections.sort, here's where the change will go.

hakanai added a commit to hakanai/spotless that referenced this issue Oct 1, 2021
Adds support for sorting wildcards after non-wildcards in the import list. Gradle config like so:

```
importOrder('java', 'javax', 'com.acme', '').wildcardsLast()
```

Not implemented here: Maven plugin support

Fixes diffplug#879.
@nedtwigg
Copy link
Member

nedtwigg commented Oct 2, 2021

Fixed in plugin-gradle 5.16.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants