spotless icon indicating copy to clipboard operation
spotless copied to clipboard

RFE: Sort imports with Google Java Format

Open basil opened this issue 3 years ago • 3 comments

Currently in Spotless, you can sort imports with importOrder() (implementation based on ImportSorterImpl from https://github.com/krasa/EclipseCodeFormatter), you can remove unused imports (with Google Java Format) with removeUnusedImports(), and you can format code, sort imports, and remove unused imports (with Google Java Format) with googleJavaFormat(). What I would like to be able to do is use Google Java Format to sort imports and remove unused imports without also formatting the code. In other words, I like Google Java Format's import sorting more than the implementation in importOrder() 😊. Would the maintainers be receptive to a PR adding a feature like googleJavaFormat().fixImportsOnly() that sorts imports and removes unused imports with Google Java Format without also formatting the source code? If so, would that be the preferred API or is there another preference for how this would be exposed to users?

basil avatar May 01 '21 22:05 basil

A PR like this would be welcome, I would rather name it importOrderGoogleJavaFormat(String version), because if I understand correctly it's a drop-in replacement for any of the importOrder algo's currently available. It's fine to be implemented by the regular GoogleJavaFormatStep (or can be something different, whatever is simplest).

nedtwigg avatar May 03 '21 21:05 nedtwigg

Great! I am happy to name it importOrderGoogleJavaFormat(String version), although in that case would you want it to not remove unused imports? If so, I suppose the user could specify both removeUnusedImports() as well as importOrderGoogleJavaFormat(String version). But in that case I'm not sure if it would then be possible to execute a single pass of Google Java Format or if two passes would be needed (one for removeUnusedImports() and one for importOrderGoogleJavaFormat(String version), which might be less efficient. Not sure if there's a perfect solution here.

basil avatar May 04 '21 01:05 basil

Valid concern, but caching plus incremental build makes it less important. If you want everything that googleJavaFormat does, then googleJavaFormat() is already there for you. If you want just one piece a-la-carte, we have each piece. If you want multiple a-la-carte pieces, you have them and can swap them out for alternatives, but you have to pay a performance penalty. Seems like a good starting point, and it doesn't block future efforts to improve speed further if that makes it to the top of anyone's todo list.

nedtwigg avatar May 04 '21 02:05 nedtwigg