-
Notifications
You must be signed in to change notification settings - Fork 460
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
Add remove wildcards imports when formating and ordering imports. #649
Comments
Currently, all of Spotless' java formatters look at each file in isolation. To implement this feature, Spotless would need to hook into the full classpath and other compiled files. Which is possible, but a lot of work for a small benefit. See #240 for details. Happy to merge a PR, but the level of difficulty is quite high. |
I found this plugin. Could it be integrated to the project ? https://github.com/nxnet/organize-imports-maven-plugin |
Happy to take a PR for it. One important caveat is that there is a ton of code out there which has already been formatted by our old broken importSorter. Some people will want the new behavior, but some projects might prefer to just not have their import ordering change. So the new step should have a new name, perhaps |
fwiw, I added the following <replaceRegex>
<name>Remove wildcard imports</name>
<searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
<replacement>$1</replacement>
</replaceRegex> |
@brendandburns @nedtwigg This is a pretty cool way to remove wildcards automatically. May I ask whether there is a way to only block wildcards or throw some error messages when doing |
With the Gradle plugin, you can add a custom step that throws an |
In case anyone is interested in the removal approach. The regex below also works for static imports. <searchRegex>import\s+(?:static\s+)?[^\*\s]+\*;(\r\n|\r|\n)</searchRegex> Note that the non-capturing group |
I have added an option to the plugin below that executes IntelliJ IDEA's 'optimize imports' task while disallowing the use of asterisk imports. This means that asterisk imports will be replaced with fully qualified names before applying the Spotless task. |
Do you have something that integrates with Gradle itself? In our team we want to automatically fix wildcard imports when we run Gradle |
Unfortunately, I don't. |
Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649 Signed-off-by: Thomas Farr <tsfarr@amazon.com>
* Forbid wildcard imports Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649 Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Make conventions plugin Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix wildcard imports Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Allow overriding eclipse formatter config file Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix message formatting Signed-off-by: Thomas Farr <tsfarr@amazon.com> --------- Signed-off-by: Thomas Farr <tsfarr@amazon.com>
* Forbid wildcard imports Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649 Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Make conventions plugin Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix wildcard imports Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Allow overriding eclipse formatter config file Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix message formatting Signed-off-by: Thomas Farr <tsfarr@amazon.com> --------- Signed-off-by: Thomas Farr <tsfarr@amazon.com> (cherry picked from commit e856ecb)
* Forbid wildcard imports Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649 Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Make conventions plugin Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix wildcard imports Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Allow overriding eclipse formatter config file Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Fix message formatting Signed-off-by: Thomas Farr <tsfarr@amazon.com> --------- Signed-off-by: Thomas Farr <tsfarr@amazon.com> (cherry picked from commit e856ecb)
I have some java classes that have imports with wildcards like : import java.util.*;
I want to replace that with the appropriate import list like
import java.util.List;
I'm using this configuration to format my java code
I have this import order
The text was updated successfully, but these errors were encountered: