Skip to content

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

Open

Description

@houtaru

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

Activity

  1. testforstephen commented on Jan 3, 2023

    @testforstephen
    Collaborator

    @CsCherrYY could you provide some advice on this issue?

  2. snjeza commented on Jan 4, 2023

    @snjeza
    Collaborator
  3. CsCherrYY commented on Jan 6, 2023

    @CsCherrYY
    Contributor

    @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.

  4. datho7561 commented on Jan 6, 2023

    @datho7561
    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.

  5. CsCherrYY commented on Mar 13, 2023

    @CsCherrYY
    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)

  6. rgrunber commented on Mar 13, 2023

    @rgrunber
    Member

    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 ?

  7. blacelle commented on Apr 6, 2023

    @blacelle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions