Skip to content

Sort imports #37

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
jamo opened this issue Apr 5, 2016 · 9 comments
Closed

Sort imports #37

jamo opened this issue Apr 5, 2016 · 9 comments

Comments

@jamo
Copy link

jamo commented Apr 5, 2016

Are there any plans to add feature to sort and group imports as described on the style guide?

@sherter
Copy link
Contributor

sherter commented Apr 5, 2016

Look what I found 😉 : eb00300

@jamo
Copy link
Author

jamo commented Apr 5, 2016

Whow, nice.
Thou with quick tests it puts the com.google on top of the imports, when, I guess the "special imports" should really be based on the package of the file... But I'll dig in and see if I can get it working for me...

@cushon
Copy link
Collaborator

cushon commented Apr 5, 2016

We're planning to stop special-casing com.google and simplify the style guide rule about imports. The implementation in the formatter should already be doing that.

@cushon
Copy link
Collaborator

cushon commented Jul 13, 2016

The CLI now sorts imports according to the new rules (google/styleguide#160), and also removes unused imports. There's also a --fix-imports-only flag. I'll plan on making a new release soon.

The API doesn't change imports because it's unclear how that should work with partial formatting. I'm open to ideas there, and using ImportOrderer and RemoveUnusedImports directly would also work.

@sherter
Copy link
Contributor

sherter commented Jul 13, 2016

I'm not against having an ImportOrderer and an UnusedImportRemover in addition to the (Whitespace)Formatter (which actually is two different formatters: a code formatter and a javadoc formatter). In fact, it seems to be a good way to separate concerns.

When going down this multi-pass road, the only thing that bothers me with the current implementation is that we repeatedly do the same work and emit a new string in each pass. Maybe we can parse the source only once into some representation that every CodeFormatter/JavadocFormatter/Orderer/Remover/whatever can work with efficiently. After parsing the source into the intermediate representation, the user chooses which formatters to apply and afterwards generates a string from or writes the representation to a Writer/CharSink.

Then again, I don't know exactly how formatting is implemented right now and if implementing this idea would be worth the effort.

@cushon
Copy link
Collaborator

cushon commented Jul 13, 2016

I agree it would be better to parse everything once. It would complicate the implementation, though, and the current approach seems to perform well enough in practice.

SortImports doesn't parse anything, it just tokenizes imports at the beginning of the file. And the javadoc formatter processes individual comments. So the duplicated effort is mainly between the unused import pass and the actual formatting logic.

PhilippWendler referenced this issue in sosy-lab/java-smt Aug 18, 2016
Checkstyle being this picky is quite annoying,
I am thinking about disabling this check altogether.
@cushon
Copy link
Collaborator

cushon commented Oct 4, 2016

The latest release enables import sorting by default in the CLI.

Using ImportOrderer and RemoveUnusedImports directly is still the only way to do it programatically, but we may revisit that.

@cushon cushon closed this as completed Oct 4, 2016
@nmarkandeya
Copy link

Apologies for commenting on a older thread.

When I used the latest 1.7 version. I found out individual unused import classes are removed. however the wildcard import statements are not. Is that an expected behaviour?

@cushon
Copy link
Collaborator

cushon commented Oct 18, 2019

@nmarkandeya that is expected behaviour, there's some related discussion here: #113 (comment).

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

No branches or pull requests

4 participants