Skip to content

Add support for "Number Of Imports Needed Imports for .*" #17

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

Closed
im-bob-loucans opened this issue Apr 9, 2016 · 12 comments
Closed

Add support for "Number Of Imports Needed Imports for .*" #17

im-bob-loucans opened this issue Apr 9, 2016 · 12 comments

Comments

@im-bob-loucans
Copy link

More of a feature request if it does not already exist in the tool.

Is it possible to apply the "Number Of Imports Needed Imports for .*" preference when organizing imports?

@nedtwigg
Copy link
Member

It's definitely possible! It's not a priority for me, but I'd be happy to merge a PR, and to help along the way.

For code formatting, we use the actual jars from Eclipse. For import sorting, we have a knockoff that can read Eclipse's import files, but it doesn't support all of their functionality.

ImportSorterStep is what reads the user's input, and ImportSorterImpl is what does the actual import sorting. The impl part is copied from https://github.com/krasa/EclipseCodeFormatter. It looks like they've made a lot of progress since we last incorporated their work.

If you (or anyone else) is interested in digging into this, I'd be happy to offer help here or in the gitter channel.

@nedtwigg
Copy link
Member

I just dug into this a little, and krasa/EclipseCodeFormatter hasn't implemented this. If we wanted to use the Eclipse implementation, the key code is this: org.eclipse.jdt.core.dom.rewrite.ImportRewrite.java, and here's an example of it in use: ImportRewriteTest.

@im-bob-loucans
Copy link
Author

Well I finally got to fork the project. See what I can do over the next couple of weeks

@nedtwigg
Copy link
Member

Great, thanks! Just as a heads-up, I'll be updating Spotless to Eclipse Neon and Gradle 2.14 around late June, as well as adding support for .gitattributes. This won't affect any Spotless API's, and I highly doubt that the Eclipse Neon API will be much different than the Mars API, so we shouldn't have any conflicts.

@sbrannen
Copy link

It's not super high on our priority list, but.... the JUnit 5 team would certainly appreciate seeing this support implemented in Spotless. 😉

@nedtwigg nedtwigg changed the title Include "Number Of Imports Needed Imports for .*" Preference When Running Spotless Check and Apply Add support for "Number Of Imports Needed Imports for .*" Mar 9, 2017
@nedtwigg
Copy link
Member

One year without progress. Also, googleJavaFormat() is gaining popularity, and it bans wildcard imports. Future contributors are free to reopen if any progress is made.

@sbrannen
Copy link

😞

@DJViking
Copy link

Has there been any improvements to enforce the googleJavaFormat of banning wildcard imports?
The Eclipse formater I am using with Spotless (which is derived from the Google standard) does remove the wildcard imports in the IDE, but spotless does nothing about it.

java {
    importOrderFile 'config/spotless/my-eclipse.importorder'
    eclipse().configFile 'config/spotless/my-eclipse-format.xml'

    trimTrailingWhitespace()
    removeUnusedImports()
}

@nedtwigg
Copy link
Member

There isn't and there probably won't be. For example:

import pkgA.*;
import pkgB.*;

public class Test {
  AmIFromAorB field;
}

When you organize imports in eclipse, the IDE knows whether AmIFromAorB came from A or B, so it can dereference it properly. But spotless's model is just Function<String, String>, so it doesn't know the whole build path, and it would be very difficult to change this.

@DJViking
Copy link

I don't mean spotless should change it, but allow to fail if wildcard imports are used.

failIfWildcardImports()

@nedtwigg
Copy link
Member

Ahh. I am not aware of anyone currently working on the issue, but the issue you want to watch is probably #167

@rnett
Copy link

rnett commented Jul 31, 2021

Google Java Format doesn't handle this apparently (google/google-java-format#113), so it would be nice to have here. Doesn't seem easy though.

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

No branches or pull requests

5 participants