Skip to content

Java Formatter settings are not used #3020

Open

Description

@Artur-

If I open the flow-server project from https://github.com/vaadin/flow/tree/main/flow-server

and have Eclipse formatter settings set to use https://raw.githubusercontent.com/vaadin/flow/main/eclipse/VaadinJavaConventions.xml which defines <setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="4"/>
image

then when I open e.g. the HandlerHelperTest.java file, VS Code shows "Spaces: 8" in the bottom bar and formatting the file changes it to use indentation of 8 spaces instead of 4

The settings file should be fine because when I go back to command line and run mvn formatter:format in the project, the indentation is changed back to 4 spaces.

Environment
  • Operating System: macOs Ventura
  • JDK version: 17
  • Visual Studio Code version: 1.76.2
  • Java extension version: 1.16.0
Steps To Reproduce
  1. See above
Current Result

File is not formatted according to given settings

Expected Result

File is formatted according to given settings

Activity

  1. CsCherrYY commented on Mar 24, 2023

    @CsCherrYY
    Contributor

    are you using editorconfig in your project?

  2. Artur- commented on Mar 24, 2023

    @Artur-
    Author

    No .editorconfig file as far as I can see

  3. Artur- commented on Apr 1, 2023

    @Artur-
    Author

    If I select "Ident Using Spaces", I see this
    image

    Why would the "current" tab size differ from the default?

    Some files in the project show "Spaces: 8" and other files seem to use the default and show "Spaces: 4"

  4. CsCherrYY commented on Apr 3, 2023

    @CsCherrYY
    Contributor

    Why would the "current" tab size differ from the default?

    The default tab size should be configured via VS Code setting editor.tabSize. As for the current tab size, you can check VS Code setting editor.detectIndentation, if it's set, VS Code will auto detect the indentation of your current file and show it.

  5. Artur- commented on Apr 3, 2023

    @Artur-
    Author

    Thanks! Now it works as expected when I turned of editor.detectIndentation.

    But does it not kind of defeat the purpose of having formatter settings defined, if they are not used by default for incorrectly formatted files?

  6. CsCherrYY commented on Apr 3, 2023

    @CsCherrYY
    Contributor

    Agree. Currently as for indentation settings, vscode-java will pick VS Code ones rather than the specific settings in profile. That would be not suitable since most users want to specific indentation settings within a project via a profile, we should fix it.

  7. slowhigh commented on May 30, 2025

    @slowhigh

    any update?

  8. bigunyak commented on Feb 13, 2026

    @bigunyak

    I had the same issue. Every time I was opening a Java file in my project, VSCode would randomly pick 8 spaces indentation instead of 4. My understanding is that the automatic detection of indentation is pretty dumb and doesn't take into account that continuation indentation is usually 2x of normal indentation size.
    Like if I have a method with a long list of parameters which I want to place each on a separate line, then those lines will have indentation of 8 spaces.

    public void myMethodWithManyParams(
            String param1,
            String param2,
            ...) {
        var localVar = ...;
    }

    If the detectIndentation feature "sees" that there are many lines with 8 spaces indentation, more than lines with 4 spaces indentation, then it decides that the default should be 8 spaces, which is obviously wrong. See this related report: microsoft/vscode#193543

    But there is another issue!
    I now have the following configuration in my settings.json.

      "java.format.settings.url": "~/.vscode/eclipse_code_style.xml",
      "[java]": {
          "editor.tabSize": 4,
          "editor.insertSpaces": true,
          "editor.detectIndentation": false,
          "editor.formatOnSave": true,
          "editor.defaultFormatter": "redhat.java"
      },

    Java files get correctly formatted when saved, everything according to my custom formatting rules in the eclipse_code_style.xml. The problem though is that this OnSave formatting doesn't match Live formatting when I'm writing the code.
    Take the same above example with method input parameters placed each on a new line. When I hit Enter after the opening parenthesis, live formatter only inserts 4 spaces. Now, if I save the file, another 4 spaces injected to get 8 spaces continuation indentation according to the config.
    This discrepancy in formatting is quite annoying, and I don't know what causes it. Must be a bug?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions