Closed
Description
If you are submitting a bug, please include the following:
- summary of problem
Spotless with eclipse formatter orders static imports differently than eclipse or eclipse plugin in Intellijgradle or maven version
mvn 3.3.3spotless version
1.27.0operating system and version
Description: Ubuntu 16.04.6 LTScopy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
<java>
<importOrder>
<file>/style/myTeam.importorder</file>
</importOrder>
</java>
</configuration>
with order
#Organize Import Order
#Fri Jun 28 15:00:43 PDT 2019
4=com.myTeam
3=java
2=javax
1=
0=\#
- copy-paste the full content of any console errors emitted by
gradlew spotless[Apply/Check] --stacktrace
N/A
If you're just submitting a feature request or question, no need for the above.
EclipseFormatter issues
krasa/EclipseCodeFormatter#200
krasa/EclipseCodeFormatter#105
Write
package somePackage;
import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeZClass.m;
import java.util.function.Supplier;
// Use as a type argument to be a static import.
public class Foo implements Supplier<SomeZClass> {
public void someMethod() {
SomeEnum e = Bar;
m(); // call m with a static import
}
@Override
public SomeZClass get() {
return null;
}
public enum SomeEnum {
Bar
}
// Add Z this should be sorted after SomeEnum
public static class SomeZClass {
static void m() {}
}
}
Eclipse Plugin will order imports:
import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass.m;
Eclipse will order imports:
import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass.m;
import java.util.function.Supplier;
import somePackage.Foo.SomeZClass;
and spotless will order imports
import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeZClass.m;
import java.util.function.Supplier;
Spotless using Eclipse format rules should have the same behavior as Eclipse
Activity
nedtwigg commentedon Feb 6, 2020
Thanks for another example. Related: #167, #174
thespags commentedon Feb 6, 2020
Sorry I forgot to look up relevant issues, would you like me to dupe this issue out?
nedtwigg commentedon Feb 7, 2020
I think your issue adds helpful new information. Looks like krasa/EclipseCodeFormatter#105 fixed part of this problem (so they are closer to a fix than we are), and krasa/EclipseCodeFormatter#200 fixes the rest of it. If someone ever gets around to a PR for one of these import sorter issues, I'd like to resolve all of them, to minimize back-compat issues.
What I mean by back-compat is that it's important for Spotless users to have the option to freeze their formatting. Fixing this bug will change the order of imports in existing code. So it's important to have something like
<importOrder><mode>legacy</mode>...
for people who don't want to choose between "reorder all your imports" and "get new bugfixes and features". If we fix these bugs all at once, then there's just two modes, but if we fix them bit by bit it gets more complicated.ekropotin commentedon Apr 24, 2020
Hello. I have another case of inconsistent import order handling which is not related neither upper case packages(EclipseCodeFormatter#105) nor static imports(EclipseCodeFormatter#200). Do I need to file a new issue against it or it's ok to provide details here?
nedtwigg commentedon Apr 24, 2020
Is the problem that it's different than eclipse? If so this issue is a good place for it. Otherwise a new issue is better.
ekropotin commentedon Apr 24, 2020
@nedtwigg yes it is.
So, in a nutshell the problem is that Spotless tries to arrange imports of packages that are not specified in "importorder" config in alphabetical order with respect to other imports. Eclipse Code Fromatter, in turn, places such imports at the end of the imports block.
Below is an example:
build.gradle:
Test.importorder:
The file pre-formatted with ECF with Test.importorder:
Output of
gradle spotlessCheck
:ekropotin commentedon Jun 24, 2020
@nedtwigg any update on this issue?
nedtwigg commentedon Jun 25, 2020
I have no personal plans to work on any of the importsorter issues. I don't care what the order is, I just care that something enforces it. I agree it's a bug, and I'm happy to merge a solution from anyone.
cimi commentedon Jul 21, 2020
@ekropotin we can work around this issue by adding an additional item to the
.importorder
file that forces unspecified imports to go at the end, for example:This makes spotless formatting consistent with IDEA/Eclipse code formatter, so I'm able to use save actions with reformat on save without causing any conflicts.
ekropotin commentedon Jul 22, 2020
@cimi This works like a charm! Thanks. You made my day!
bwRavencl commentedon Jul 22, 2020
Good advice, however unfortunately this still doesn't seem fix the issue entirely in some cases:
Eclipse:
Spotless:
Eclipse seems to order segments consisting entirely of uppercase letters below segments with mixed case, regardless of the characters.
spotless.importorder: order unspecified imports at the end - see: dif…
Reformat with new spotless rule that is closer to IDE behavior
34 remaining items