Skip to content

Automatically organize imports with spotless - #606

Merged
marko-radosavljevic merged 4 commits into
developfrom
feature/spotless-organize-imports
Oct 8, 2022
Merged

marko-radosavljevic merged 4 commits into
developfrom
feature/spotless-organize-imports

Conversation

@marko-radosavljevic

@marko-radosavljevic marko-radosavljevic commented Oct 4, 2022 •

Copy link
Copy Markdown
Contributor

This PR introduces automatic import organizing with spotless, since it's a common pain point in the workflow.
You push → sonarcloud doesn't like the unused import → you have to fix the issue and push again.

So with this PR, we have automatic fixes for:

  • newline missing at the end of files, or too many newlines at the end of files (can be extended to other files if we want, like json config etc)
  • unused imports
  • import order

Automatically ordering imports is not necessary, but it's a nice thing to have, since everything would be always consistent.
Import are ordered like this: external dependencies, internal org.togetherjava classes, java classes and static imports. Since that's the ordering we have been using so far. There are still small changes in a lot of files, mostly new lines between categories.

Feel free to suggest new ordering, but keep in mind it would introduce heavy merge conflicts since every PR branch has import changes. ^^


Diff of the build.gradle since there are a lot of changes by spotless:

diff --git a/build.gradle b/build.gradle
index 9f4a7cb..e04ada7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -74,6 +74,13 @@ subprojects {
         java {
             // Excludes build folder since it contains generated java classes.
             targetExclude("build/**")
+            endWithNewline()
+            removeUnusedImports()
+            // empty string '' covers all imports that aren't explicitly specified,
+            // we use it as catch-all for external dependencies like JDA
+            // '\\#` is prefix for static imports
+            importOrder('','org.togetherjava', 'javax', 'java', '\\#')
+            // TODO Use latest eclipse jdt formatter, issue tracking this: https://github.com/Together-Java/TJ-Bot/issues/605
             eclipse('4.19.0').configFile("${rootProject.rootDir}/meta/formatting/google-style-eclipse.xml")
         }
     }

so our crimes cannot be forgotten
Order imports automatically in this order: external dependecies, internal org.togetherjava classes, java and static imports. Since that's the ordering we have been using so far.
@sonarqubecloud

sonarqubecloud Bot commented Oct 4, 2022

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Taz03

Taz03 commented Oct 4, 2022

Copy link
Copy Markdown
Contributor

but keep in mind it would introduce heavy merge conflicts since every PR branch has import changes

they are easy to fix so no problem to me

@Zabuzard Zabuzard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two things to ease migration here:

  • can you adjust the meta/formatting/google-style-eclipse.xml file?
  • can you export an IntelliJ formatter settings file and/or tell us which checkboxes to check so that it conforms?

I want my IDE to conform with the setup as much as possible, to reduce the amount of stuff that Spotless changes.

We also promote such a setup, with a small guide, in our Wiki for beginners.

@marko-radosavljevic

marko-radosavljevic commented Oct 7, 2022 •

Copy link
Copy Markdown
Contributor Author

There shouldn't be much or any migration with this. It's not like we are changing code formatting, it's just imports, so workflow shouldn't be affected.

You write code → IDE adds imports for you, you don't even see them because they are collapsed → you commit and push and everything is organized for the repo. So ideally, this should be completely invisible to the user.

The whole idea with our fully automatic workflow was to make it beginner-friendly. So nobody has to worry about formatting and complex configuration importing in all your tooling, only to realize half of the IDEs don't even support it.

Now, I understand wanting to conform to formatting of the code, since you don't want to flip-flop completely different formatting after each commit. But who cares about imports? Since currently it's wild west anyway, everyone does imports however they want or however their tooling does it.

But yeah, sure, if people want to be able to import a config, and we can support it, why not! ☺️


Now to answer your questions,

can you adjust the meta/formatting/google-style-eclipse.xml file

No, eclipse has a separate file format for ordering imports.
Spotless can use eclipse exported import order, looks something like this

#Organize Import Order
#Wed Oct 05 11:50:58 CEST 2022
0=java
1=javax
2=org
3=com

It's a text file, and IntelliJ doesn't understand it, doubt other IDEs either.

can you export an IntelliJ formatter settings file and/or tell us which checkboxes to check so that it conforms?

Yeah, this should give the exact same result as our spotless import order. Since I copied our current ordering and IntelliJ default as much as possible, the difference is mostly in spacing:

image

(Unable to resize the window, but the rest is same)

It would look like this in IntelliJ XML config:

<option name="IMPORT_LAYOUT_TABLE">
      <value>
        <package name="" withSubpackages="true" static="false" />
        <emptyLine />
        <package name="org.togetherjava" withSubpackages="true" static="false" />
        <emptyLine />
        <package name="javax" withSubpackages="true" static="false" />
        <emptyLine />
        <package name="java" withSubpackages="true" static="false" />
        <emptyLine />
        <package name="" withSubpackages="true" static="true" />
      </value>
    </option>

Note that IntelliJ organize imports does more than just ordering, it does things like these too:

image

Who knows what other tooling does by default, I think it's not worth it trying to align all tooling over imports. It's fine if someone uses awt.* and someone doesn't, we can live with that kind of inconsistency.


Ordering is not that important anyway, we can drop that commit as far as I'm concerned. Just some consistency is nice, if it comes at no cost.

  • newline missing at the end of files, or too many newlines at the end of files (can be extended to other files if we want, like json config etc)
  • unused imports

These two are actually important since they waste so much dev hours monthly, sonar flags them and fails the build, while they generate no changes at all.

@Zabuzard Zabuzard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

feel free to hit the button @marko-radosavljevic

@marko-radosavljevic
marko-radosavljevic merged commit 6a36e3b into develop Oct 8, 2022
@marko-radosavljevic
marko-radosavljevic deleted the feature/spotless-organize-imports branch October 8, 2022 18:47
@Zabuzard Zabuzard mentioned this pull request Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request priority: normal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants