Spotless: Add support for removing unused imports

Created on 7 Jun 2016  路  6Comments  路  Source: diffplug/spotless

Please add support to detect and remove unused imports

enhancement

Most helpful comment

Does Spotless have access to all Java sources by default in a Gradle project it's applied to?

Yes and no. Spotless mostly constrains formatters to be Function<String, String>, but if you implement FormatStep directly, then you can see the actual File object being formatted. Inside of JavaExtension (which is where you create the FormatSteps), you have access to the full gradle project, so you can grab the JavaPluginConvention and get the full classpath and sources.

It could certainly be done :)

All 6 comments

This may be difficult to do, according to https://github.com/google/google-java-format/issues/72.

Does Spotless have access to all Java sources by default in a Gradle project it's applied to? If so, this problem may actually be approachable (but almost certainly not easy to figure out). :)

Does Spotless have access to all Java sources by default in a Gradle project it's applied to?

Yes and no. Spotless mostly constrains formatters to be Function<String, String>, but if you implement FormatStep directly, then you can see the actual File object being formatted. Inside of JavaExtension (which is where you create the FormatSteps), you have access to the full gradle project, so you can grab the JavaPluginConvention and get the full classpath and sources.

It could certainly be done :)

Implemented in 3.0.0.BETA2 #63

Speak of the devil.... I could have just used this: https://github.com/junit-team/junit5/commit/0672e2904cd51786961a3851f5504e93eab9de59

Looking forward to this feature in 3.0.0!

Has this feature only been implemented for the Gradle Plugin? No Maven support?

Nope, it's in maven too. Search removeUnused in https://github.com/diffplug/spotless/blob/master/plugin-maven/README.md

Was this page helpful?
0 / 5 - 0 ratings