Skip to content

Support removing wildcard imports via removeWildcardImports step #2517

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

Merged
merged 12 commits into from
Jun 24, 2025

Conversation

iddeepak
Copy link
Contributor

@iddeepak iddeepak commented Jun 18, 2025

I have just opened a PR to help us say “goodbye” to those sneaky wildcard imports (like import java.util.*;) whenever Spotless tidies up our Java files. Wildcard imports can make it hard to see exactly which classes you’re relying on, so this change introduces a new step in the Spotless plugin that:

Spots any import foo.*; lines in your source

Removes them outright (so your IDE or build tools can regenerate only the exact imports you need)

Copy link
Contributor

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

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

awesome, thanks.

iddeepak and others added 2 commits June 20, 2025 01:19
Co-authored-by: Consequences of Lacking Discipline <8830888+Pankraz76@users.noreply.github.com>
…WildcardsUnformatted.test

Co-authored-by: Consequences of Lacking Discipline <8830888+Pankraz76@users.noreply.github.com>
@iddeepak iddeepak marked this pull request as ready for review June 19, 2025 21:33
Copy link
Member

@Goooler Goooler left a comment

Choose a reason for hiding this comment

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

LGTM! Would you mind adding a test for the Gradle plugin?

@Pankraz76
Copy link
Contributor

Pankraz76 commented Jun 23, 2025

could migrate the .editorconfig as well dedicated, assuming spot it applied on itself, like checkstyle does impose all checks on itself, too.

ij_java_class_count_to_use_import_on_demand = 999

@Goooler Goooler requested a review from Copilot June 23, 2025 16:38
Copilot

This comment was marked as outdated.

@iddeepak
Copy link
Contributor Author

iddeepak commented Jun 23, 2025

could migrate the .editorconfig as well dedicated, assuming spot it applied on itself, like checkstyle does impose all checks on itself, too.

ij_java_class_count_to_use_import_on_demand = 999

you mean spot checks on .editorconfig something like this

format("editorconfig") { target "spotless/.editorconfig" editorConfig() }
@Pankraz76

@Pankraz76
Copy link
Contributor

No, sorry—I meant we could remove this config, assuming it's not needed anymore, as Spot will prevent it now. But keeping it wouldn't hurt, as it still helps to avoid potential issues.

If auto-fix were possible, the config would still help prevent problems before fixing them, in the first place, but it wouldn't be strictly necessary anymore.

The next version will go further and deliver actual imports to fully automate this.

A win for the project. Thanks for the good craftsmanship!

@iddeepak
Copy link
Contributor Author

No, sorry—I meant we could remove this config, assuming it's not needed anymore, as Spot will prevent it now. But keeping it wouldn't hurt, as it still helps to avoid potential issues.

If auto-fix were possible, the config would still help prevent problems before fixing them, in the first place, but it wouldn't be strictly necessary anymore.

The next version will go further and deliver actual imports to fully automate this.

A win for the project. Thanks for the good craftsmanship!

Ohh no my bad, I misunderstood it. Now it makes sense. Lets keep that setting in place as an extra safety as of now. Without it, IntelliJ’s default of 5 would collapse to a wildcard on the 6th import, then Spotless would strip that wildcard and force everyone to re-import.

@Goooler
Copy link
Member

Goooler commented Jun 24, 2025

I meant we could remove this config, assuming it's not needed anymore, as Spot will prevent it now.

It could be honored by the IDEA side.

@Goooler Goooler changed the title Remove wildcard imports Add support for removing wildcard imports via removeWildcardImports step Jun 24, 2025
@Goooler Goooler changed the title Add support for removing wildcard imports via removeWildcardImports step Support removing wildcard imports via removeWildcardImports step Jun 24, 2025
@Goooler Goooler merged commit 922f9f1 into diffplug:main Jun 24, 2025
16 checks passed
@Pankraz76
Copy link
Contributor

Thanks.

Copy link
Contributor

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

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

idea on removeObsoletes().

@@ -188,6 +188,7 @@ spotless {
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse

removeUnusedImports()
removeWildcardImports()
Copy link
Contributor

Choose a reason for hiding this comment

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

removeObsoletes()
removeObsoleteInit()
removeObsoleteInitializations()

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