Skip to content
Advertisement

VSCode Redhat language support for Java’s formatter forces max line length

I’m using VS Code IDE for Java project. I install Language Support for Java(TM) by Red Hat for formating code.

settings.json:

{
    "java.configuration.updateBuildConfiguration": "automatic",
    "java.debug.settings.hotCodeReplace": "auto",
    "java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
    "java.format.settings.profile": "GoogleStyle",
}

I’m writing a class like that:

public class Foo {
    private int theFirstVariable, theSecondVariable, theThirdVariable, theFourthVariable, theFifthVariable, theSixthVariable;
    Foo(
        int theFirstVariable,
        int theSecondVariable,
        int theThirdVariable,
        int theFourthVariable,
        int theFifthVariable,
        int theSixthVariable
    ) {
        this.theFirstVariable = theFirstVariable;
        this.theSecondVariable = theSecondVariable;
        this.theThirdVariable = theThirdVariable;
        this.theFourthVariable = theFourthVariable;
        this.theFifthVariable = theFifthVariable;
        this.theSixthVariable = theSixthVariable;
    }
}

When i format code (Press Ctrl + s), it becomes:

public class Foo {
    private int theFirstVariable, theSecondVariable, theThirdVariable, theFourthVariable,
            theFifthVariable, theSixthVariable;

    Foo(int theFirstVariable, int theSecondVariable, int theThirdVariable, int theFourthVariable,
            int theFifthVariable, int theSixthVariable) {
        this.theFirstVariable = theFirstVariable;
        this.theSecondVariable = theSecondVariable;
        this.theThirdVariable = theThirdVariable;
        this.theFourthVariable = theFourthVariable;
        this.theFifthVariable = theFifthVariable;
        this.theSixthVariable = theSixthVariable;
    }
}

At the constructor, it seems like the formator was trying to fill every parameters into one line until maximum line length exceeded. Can we keep keep the fommer’s format and how to if we can?

Advertisement

Answer

There’s no way to save all your wanted formatting style. but there’s optional setting.

Download the GoogleStyle.xml and change the setting:

<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>

Then in Settings.json:

"java.format.settings.url": "<local path to java-google-style.xml>",

This won’t keep you code as the same as the one when you write it down, but at least the parameters are split into lines. Although not that good:

enter image description here

Advertisement