spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Add remove wildcards imports when formating and ordering imports.

Open survivant opened this issue 3 years ago • 8 comments

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

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.0.1</version>
                <executions>
                    <execution>
                        <!-- Runs in compile phase to fail fast in case of formatting issues.-->
                        <id>format java</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>apply</goal>
                        </goals>
                        <configuration>
                            <formats>
                                <!-- you can define as many formats as you want, each is independent -->
                                <format>
                                    <!-- define the files to apply to -->
                                    <includes>
                                        <include>src/main/java/**/*.java</include>
                                        <include>src/test/java/**/*.java</include>
                                    </includes>
                                    <indent>
                                        <spaces>true</spaces>
                                        <spacesPerTab>4</spacesPerTab>
                                    </indent>
                                </format>
                            </formats>
                            <java>
                                <eclipse>
                                    <file>${basedir}/formatter/eclipse-format.xml</file>
                                </eclipse>
                                <removeUnusedImports/>
                                <importOrder>
                                    <file>${basedir}/formatter/eclipse.importorder</file>
                                </importOrder>
                            </java>
                        </configuration>
                    </execution>
                    
                </executions>
            </plugin>

I have this import order

#Organize Import Order
#Wed Jul 22 13:29:56 EDT 2020
0=java
1=javax
2=org
3=com

survivant avatar Jul 22 '20 19:07 survivant

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.

nedtwigg avatar Jul 22 '20 23:07 nedtwigg

I found this plugin. Could it be integrated to the project ? https://github.com/nxnet/organize-imports-maven-plugin

survivant avatar Aug 03 '20 11:08 survivant

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 organizeImportsNxNet() or organizeImportsModern(), so that people who want the old behavior can keep it.

nedtwigg avatar Aug 04 '20 17:08 nedtwigg

fwiw, I added the following replaceRegexp to remove wildcards while formatting:

              <replaceRegex>
                <name>Remove wildcard imports</name>
                <searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
                <replacement>$1</replacement>
              </replaceRegex>

brendandburns avatar Dec 03 '20 18:12 brendandburns

fwiw, I added the following replaceRegexp to remove wildcards while formatting:

              <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 mvn spotless:apply instead of directly removing them? One possible scenario is that developers thought their wildcard imports got fixed and pushed their code to the remote but actually they lost some imports.

EricGao888 avatar Aug 13 '22 05:08 EricGao888

whether there is a way to only block wildcards or throw some error message

With the Gradle plugin, you can add a custom step that throws an Exception. We don't have a way to do that using the maven syntax.

nedtwigg avatar Aug 21 '22 20:08 nedtwigg