Skip to content
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

Auto add brackets to if/else/while/do when format code #2846

Open
houtaru opened this issue Dec 26, 2022 · 7 comments
Open

Auto add brackets to if/else/while/do when format code #2846

houtaru opened this issue Dec 26, 2022 · 7 comments

Comments

@houtaru
Copy link

houtaru commented Dec 26, 2022

Is there a way to add brackets to the if/else/while/do statement when we use format code?

Environment
  • Operating System: Ubuntu 20.04
  • JDK version: 1.8.0, 17.05
  • Visual Studio Code version: 17.05
  • Java extension version:v1.13.0
Steps To Reproduce

Right-click -> Choose Format Doccment

Current Result
public int test(int x) {
    if (x == 1) return x + 1;
    return x * 10;
}
Expected Result
public int test(int x) {
    if (x == 1) {
        return x + 1;
    }
    return x * 10;
}
Additional Informations
@testforstephen
Copy link
Collaborator

@CsCherrYY could you provide some advice on this issue?

@snjeza
Copy link
Contributor

snjeza commented Jan 4, 2023

@CsCherrYY @houtaru @testforstephen We could add the control statements cleanup (Use blocks in if/while/for/do statements).
See

@CsCherrYY
Copy link
Contributor

CsCherrYY commented Jan 6, 2023

@snjeza I just tried and it works in eclipse using clean up👍

Clean up is a great feature, and I think we still have two things to do:

  • Support using blocks in control statements cleanup in jdt.ls, so that we can fix this issue
  • Promote the discoverability of the clean up feature in the extension. Currently it's available on save, and not easy to find. Maybe a button in the editor menu and webview based wizard would be great, in this way, the clean up can be done as a single "action"

cc @rgrunber @datho7561 who contributed the clean up feature for further suggestion.

@datho7561
Copy link
Contributor

  • Support using blocks in control statements cleanup in jdt.ls, so that we can fix this issue

Looks like this will require a eclipse.jdt.ui PR to access, since the ICleanUpFix class is in the ui package.

When I talked with Roland, he was interested mainly in migrating the clean ups that don't address formatting. I think his reasoning is that any clean ups that fix formatting should really be a part of the formatter instead.

  • Promote the discoverability of the clean up feature in the extension. Currently it's available on save, and not easy to find. Maybe a button in the editor menu and webview based wizard would be great, in this way, the clean up can be done as a single "action"

From my understanding, the main benefit of the clean ups is that they are run automatically when you save. If you have to perform additional steps to use them, in my opinion they are no longer clean ups, but instead they are closer to something like quick fixes or source actions. I think it's worth discussing if any of the clean ups that we have should be available through other means, like as source actions or through a command.

@CsCherrYY
Copy link
Contributor

@datho7561 I think the clean ups can be a part of the current source actions (like what we see in eclipse), and there is also an existing request for this: #1626.

We can also implement a webview to help users to configure available clean ups (like eclipse ui), showing all available clean ups in jdtls. So that there would be only one extra source action: "Clean Up...", and vscode-java behaves like eclipse in the clean up field (available in both source action and save action)

@rgrunber
Copy link
Member

rgrunber commented Mar 13, 2023

I'm completely open to migrating the control statement fix, provided there is demand for it. We've migrated plenty of cleanups so it shouldn't be too difficult.

I agree with improving the discoverability. When we did java.cleanup.actionsOnSave, the main priority was to start migrating many of the interesting ones (eg. relating to new Java language features). A context menu item for Cleanup ... under Source Action subgroup should be simple to do without requiring too much code.

Lastly, https://github.com/redhat-developer/vscode-java/blob/master/document/_java.learnMoreAboutCleanUps.md was just a workaround for lack of proper UI for it on the vscode-java side. We need a UI that displays an editor showing the before/after state of code that is affected by each cleanup. i think we've discussed this before, and there might be some POC for it ?

@blacelle

This comment was marked as off-topic.

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

No branches or pull requests

7 participants