Skip to content

Add semantics-aware sorting for Java imports#1709

Merged
nedtwigg merged 5 commits intodiffplug:mainfrom
Frettman:PR-522
May 22, 2023
Merged

Add semantics-aware sorting for Java imports#1709
nedtwigg merged 5 commits intodiffplug:mainfrom
Frettman:PR-522

Conversation

@Frettman
Copy link
Copy Markdown
Contributor

This PR is for issue #522.

It introduces semantics-aware sorting of Java imports, i.e. imports are sorted by package, classes and static members, instead of just lexicographically. This is the new default behavior, but the old one can be restored (in Maven e.g. via <semanticSort>false</semanticSort>). The imports are split based on conventions: package names start with lower case, class names with upper case. For exceptions, the <treatAsPackage> and <treatAsClass> configurations can be used.

Copy link
Copy Markdown
Member

@nedtwigg nedtwigg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a great PR!

The most important compatibility guarantee we make is to our existing users. I agree that for new users, semanticSort is probably the correct behavior. But the premise of Spotless is "formatting doesn't matter, turn this on and then stop thinking about it". Anytime we change the defaults they have to think about it, which is bad.

Comment thread plugin-maven/src/main/java/com/diffplug/spotless/maven/java/ImportOrder.java Outdated
Comment thread plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Outdated
Comment thread lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java Outdated
Comment thread lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java Outdated
@Frettman
Copy link
Copy Markdown
Contributor Author

I added the requested changes in a fixup commit.

@nedtwigg
Copy link
Copy Markdown
Member

CI is failing because you need to run spotlessApply.

@nedtwigg nedtwigg enabled auto-merge May 22, 2023 21:58
Copy link
Copy Markdown
Member

@nedtwigg nedtwigg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nedtwigg nedtwigg merged commit 8d4f402 into diffplug:main May 22, 2023
Frederick888 added a commit to Frederick888/spotless that referenced this pull request May 26, 2023
The default value was updated to false [1] per suggestion [2].

[1] diffplug@10610f8
[2] diffplug#1709 (review)
Frederick888 added a commit to Frederick888/spotless that referenced this pull request May 26, 2023
The default value was updated to false [1] per suggestion [2].

[1] diffplug@10610f8
[2] diffplug#1709 (review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants