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

Java code formatter improperly formats one-liner conditions and loops, and after method signatures #3502

Open
rohanbari opened this issue Feb 18, 2024 · 2 comments
Labels

Comments

@rohanbari
Copy link

The Java code formatter is working improperly after single-line conditions, loops, and specifically after multiline method signatures. For example, consider the following code segment:

public static List<List<Integer>> combinationSum(
        int[] candidates, int target) {

    return new ArrayList<>();
}

Now, if you hit Enter after the opening curly brace, the cursor will be jumped right to the next line with unexpected indentation:

Unexpected indentation

Environment
  • Operating System: Windows 10 22H2
  • JDK version: javac 17.0.10
  • Visual Studio Code version: 1.86.2
  • Java extension version: 1.28.1
Steps To Reproduce

The brief information above represents just one example. Here is more with single-line conditions and loops:

Consider this code segment:

public static List<List<Integer>> combinationSum(
        int[] candidates, int target) {
    int x = 5;

    if (x > 5)
        System.out.println("ok");
    else
        System.out.println("Not ok");

        

    return new ArrayList<>();
}

Have a look after the end of the else statement. It should not be indented in such cases.
Here is the screenshot:

Indentation problem in conditional statements

The same goes with one-liner loops:

Indentation problem in loop statements

A sample file saved unformatted:
Main.zip

Log file generated by RedHat formatting server:
redhat_server_log.txt

@rgrunber
Copy link
Member

Maybe similar to #3396 . I think we have some issues with indentation after carriage return. CC'ing @hopehadfield for awareness.

@rgrunber
Copy link
Member

rgrunber commented May 8, 2024

Just as a note, I played around with the examples mentioned here.

It's not the language server that's causing this. textDocument/formattingOnType returns an empty result. It's the indentation rules inherited by our extension : https://code.visualstudio.com/api/language-extensions/language-configuration-guide#indentation-rules . We probably need to update them to increase/decrease indentation in the cases mentioned. If that can't be done, maybe we need to have the language server actually handle these.

@hopehadfield hopehadfield moved this to 🏗 In progress in IDE Cloudaptors May 15, 2024
@hopehadfield hopehadfield removed their assignment May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants