You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From my understanding Spotless uses org.eclipse.jdt:org.eclipse.jdt.core to do formatting with Eclipse. Currently this only provides whitespace changes, but Eclipse itself also has Clean Up which makes code changes. I think this functionality is provided by the org.eclipse.jdt:org.eclipse.jdt.ui package. Would it be possible to also add this functionality to Spotless?
The spotless-eclipse-jdt should do a little bit more than white-space changes 😉
But you are right, the JDT Clean-Up would be useful. I added the clean-up tasks already for Eclipse-WTP as far as supported. Should be no problem to add it for JDT as well.
I'm all for it, but I see some potential hangups, e.g. Add @Override and @Deprecated tags The only way to implement this feature is if the formatter has already compiled the code, so that it knows which methods are overrides. Spotless' model is heavily reliant on every formatter being a Function<String, String>. In this case, that function is going to have to be created with the compiled result of the project as its state. It's not impossible, but it will be tricky. Would be a great contribution if you can figure it out though 👍
I always use Eclipse for coding, but since you don't need to care about the Eclipse plugins and the OSGI layer, InelliJ should also work fine. Since MavenCentral also provides the Java source bundles for JDT, debugging your code should not be problematic.
Please have a look at the existing tests. Try to run them in your IDE and step down to the Eclipse code.
All Spotless Eclipse formatter basically just do the following:
Don't worry too much about the underlying Eclipse framework. I think the the current SpotlessEclipseFramwork setup should be sufficient. Eclipse uses OSGI and JDT is just a plugin. Since we don't want the full Eclipse environment, our framework just stubs out most of the Eclipse framework functionality.
So in the end you just have to find the JDT method for the clean-up and how to configure it. Add the configuration to the EclipseJdtFormatterStepImpl constructor and an additional call to the format method. Please add some unit tests.
Most Eclipse formatters look quite similar. This CSS Clean-UP example might give you a hint, what you should look for.
Afterwards we can talk about remaining issues (documentation, default configuration, ...). As Ned pointed out, not all clean-up functionality can work with Spotless, since Spotless only knows the content of a single file and not the project structure. Hence some configuration option must be suppressed.
Activity
fvgh commentedon Aug 31, 2018
The
spotless-eclipse-jdtshould do a little bit more than white-space changes 😉But you are right, the JDT Clean-Up would be useful. I added the clean-up tasks already for Eclipse-WTP as far as supported. Should be no problem to add it for JDT as well.
fvgh commentedon Aug 31, 2018
@JamieMagee Don't know when I'll find time. If you want to do it yourself, I am happy to assist.
nedtwigg commentedon Aug 31, 2018
I'm all for it, but I see some potential hangups, e.g.
Add @Override and @Deprecated tagsThe only way to implement this feature is if the formatter has already compiled the code, so that it knows which methods are overrides. Spotless' model is heavily reliant on every formatter being aFunction<String, String>. In this case, that function is going to have to be created with the compiled result of the project as its state. It's not impossible, but it will be tricky. Would be a great contribution if you can figure it out though 👍HoldYourWaffle commentedon Dec 7, 2018
Is there any progress on this?
I'd love to use spotless to clean up some auto-generated code!
fvgh commentedon Dec 8, 2018
@HoldYourWaffle I am afraid I still have no time. But I am always happy to assist if you volunteer to implement it.
HoldYourWaffle commentedon Dec 8, 2018
@fvgh I'd happily volunteer, but I just don't know enough (basically none) about the eclipse platform or the internals of spotless to implement it.
fvgh commentedon Dec 8, 2018
@HoldYourWaffle
You should read the Spotless contribution guide first.
The Eclipse formatter is part of the
_ext.We obtain the Eclispe JDT parts from MavenCentral. I expect Clean-Up to be part of the JDT core code, so the current setup of the spotless-eclipse-jdt should be sufficient for this task.
I always use Eclipse for coding, but since you don't need to care about the Eclipse plugins and the OSGI layer, InelliJ should also work fine. Since MavenCentral also provides the Java source bundles for JDT, debugging your code should not be problematic.
Please have a look at the existing tests. Try to run them in your IDE and step down to the Eclipse code.
All Spotless Eclipse formatter basically just do the following:
Don't worry too much about the underlying Eclipse framework. I think the the current SpotlessEclipseFramwork setup should be sufficient. Eclipse uses OSGI and JDT is just a plugin. Since we don't want the full Eclipse environment, our framework just stubs out most of the Eclipse framework functionality.
So in the end you just have to find the JDT method for the clean-up and how to configure it. Add the configuration to the
EclipseJdtFormatterStepImplconstructor and an additional call to theformatmethod. Please add some unit tests.Most Eclipse formatters look quite similar. This CSS Clean-UP example might give you a hint, what you should look for.
Afterwards we can talk about remaining issues (documentation, default configuration, ...). As Ned pointed out, not all clean-up functionality can work with Spotless, since Spotless only knows the content of a single file and not the project structure. Hence some configuration option must be suppressed.
fvgh commentedon Jun 13, 2019
Provided first POC showing the import organization. The
Simpletest result looks promising.nedtwigg commentedon Jun 13, 2019
VERY cool!!
fvgh commentedon Jun 14, 2019
As demonstrated by the tests, the implementation of this issue may also solve (partly) #306 and #167.
budjb commentedon Nov 13, 2020
Another +1 for this functionality. This would be super helpful for us.
bwRavencl commentedon Dec 17, 2020
+1 from me as well! This would really take Spotless to the next level!
mjacoby commentedon Nov 8, 2021
+1
Any updates on the status?
blacelle commentedon Feb 25, 2023
The Eclipse CleanUp work is being revived in #1587, following the effort from @fvgh .
In the meantime, one can find some related Java refactoring through Cleanthat (https://github.com/diffplug/spotless/blob/main/lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java)
jzelAdmin2006 commentedon Dec 8, 2024
+1
This would still take Spotless to the next level! Are there any updates?