redhat-developer / redhat-developer/vscode-java

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

Open
#3,502 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

typing
Dominant language
TypeScript
Stars
2.3k
Forks
546
Avg merge
20h 1m
Merged PRs (30d)
11

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the indentation problems using the code examples or the attached Main.zip, and inspect redhat_server_log.txt for formatter context. Done means single-line conditions and loops, and new lines after multiline method signatures, receive the expected indentation without affecting other formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.