Skip to content

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

Closed

Description

@hakanai

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.

Activity

  1. nedtwigg commented on Jun 16, 2021

    @nedtwigg
    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

  2. sbilello commented on Sep 30, 2021

    @sbilello

    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

  3. hakanai commented on Oct 1, 2021

    @hakanai
    ContributorAuthor

    @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?

  4. hakanai commented on Oct 1, 2021

    @hakanai
    ContributorAuthor

    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.

  5. added a commit that references this issue on Oct 1, 2021
    3b74071
  6. added a commit that references this issue on Oct 1, 2021
    72e8c21
  7. nedtwigg commented on Oct 2, 2021

    @nedtwigg
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions