vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

Feature Request: Support Eclipse "Source Clean Up" actions

Open ghokun opened this issue 5 years ago • 13 comments

Eclipse provides a feature called Clean up (Right click a source folder: Source -> Clean Up). Clean up profile can be defined from Preferences -> Java -> Code Style -> Clean Up. Clean up profiles can be exported to an xml file.

Screenshot_2020-09-18_11-00-04

Like format settings, clean up settings could be set up with an xml file.

{
    "java.format.settings.url": "...",
    "java.cleanup.settings.url" : "..."
}

Also, I would like to contribute to this issue, if someone points me to the right direction.

mentioned in #1379

ghokun avatar Sep 18 '20 08:09 ghokun

@jdneo and @testforstephen is there any update on this a year later?

xtianus79 avatar May 01 '21 00:05 xtianus79

I am interested in this feature as well - any update? Or pointers on how to contribute?

chriscasola avatar Aug 11 '21 16:08 chriscasola

Hi guys, do you know if there's any update on this? Thanks

jcolladosp avatar Sep 06 '21 13:09 jcolladosp

Anything stopping this enhancement? I see most of cleanup implementation doesn’t have ui component dependencies. Could we move those in jdt ui like we have done for type hierarchy?

or are we looking at handling this in a different way for vscode ? Like having individual cleanup as code actions?

@testforstephen @jdneo @rgrunber

gayanper avatar Sep 23 '22 09:09 gayanper

Assign to @rgrunber since his team is working on this area recently.

testforstephen avatar Feb 13 '23 06:02 testforstephen

We started doing this through the new setting java.cleanup.actionsOnSave (through https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java in JDT-LS). Currently supported ones are https://github.com/redhat-developer/vscode-java/blob/master/document/_java.learnMoreAboutCleanUps.md#java-clean-ups .

Any from the following list could be supported though : AbstractCleanupCore

Might be best to prioritize the more interesting ones.

They could probably be added to the "Source Action" menu with just a few lines of code. In order to get them as code actions (quick assists), the cleanup would need to have some helper method to have it apply on an individual ASTNode as opposed to on the entire CompilationUnit, but that should be possible.

rgrunber avatar Feb 13 '23 21:02 rgrunber

Hello. I've tried integrating Eclipse CleanUp into Spotless (https://github.com/diffplug/spotless/pull/1587) but there is still quite a long way to go. I feel some effort would be shared with the one in vscode-java.

Concurrently, you may wish to have a look into Cleanthat which offers a various list of similar CleanUp rules (Mutators).

blacelle avatar Apr 06 '23 05:04 blacelle

@rgrunber is there any progress on this.

I tried in settings.json

"java.cleanup.actions": [ { "qualifyMembers": true, "qualifyStaticMembers": true } ] or

"java.cleanup.actions": [ { "qualifyMembers": "true", "qualifyStaticMembers": "true" } ]

Both in both cases vscode complains with

Incorrect type. Expected "string". Value is not accepted. Valid values: "qualifyMembers", "qualifyStaticMembers", "addOverride", "addDeprecated", "stringConcatToTextBlock", "invertEquals", "addFinalModifier", "instanceofPatternMatch", "lambdaExpressionFromAnonymousClass", "lambdaExpression", "switchExpression", "tryWithResource", "renameFileToType", "organizeImports", "renameUnusedLocalVariables".(1)

Any ideas?

//mike

mpet avatar Feb 12 '25 14:02 mpet

Try "java.cleanup.actions": [ "qualifyMembers", "qualifyStaticMembers" ]. You don't need to set them as true. Just them being listed enables them.

rgrunber avatar Feb 13 '25 04:02 rgrunber

@rgrunber vscode accepts it. In which context can I use this?

  • whole java project
  • maven module
  • java package
  • java file

And how to perform the check? Is it possible to run rule and let IDE make changes on the code?

br,

//mike

mpet avatar Feb 13 '25 06:02 mpet

The should apply to any project type (Maven, Gradle, Eclipse, Unmanaged, etc.) . You have a few options to perform this :

The setting java.cleanup.actions controls which options will be checked/applied. java.saveActions.cleanup (true or false) control whether it happens automatically any time the active editor is saved. It should act as true by default. It is also bound to the keybinding ctrl+shift+alt+s so it may be applied manually (or even mapped to another key binding from the keyboard shortcut menu).

rgrunber avatar Feb 13 '25 14:02 rgrunber

@rgrunber so it is not possible to run these on the complete project?

mpet avatar Feb 13 '25 15:02 mpet

We don't currently do this, but has been suggested in https://github.com/redhat-developer/vscode-java/issues/1820, https://github.com/redhat-developer/vscode-java/issues/1107, https://github.com/redhat-developer/vscode-java/issues/2598. I don't see it as too difficult. We've just prioritized the editing experience rather than bulk project operations.

rgrunber avatar Feb 13 '25 16:02 rgrunber