vscode redhat语言支持java的格式化程序强制使用最大行长度

8oomwypt  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(627)

我正在为java项目使用vs代码ide。我安装了red hat对java(tm)的语言支持,用于格式化代码。
设置.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",
}

我在写这样一节课:

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;
    }
}

当我格式化代码时(按 Ctrl + s ),它变成:

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;
    }
}

在构造函数中,似乎格式化程序试图将每个参数填充到一行中,直到超过最大行长。我们能保留发信人的格式吗?如果可以的话,我们怎么做?

yks3o0rb

yks3o0rb1#

我试着创造一种习惯 eclipse-java-google-style.xml 并按照@molly wang的建议进行修改。我发现了一些修改可以得到我想要的结果:

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

格式化后,代码变为:

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;
    }
}

如果希望构造函数的关闭参数位于下一行中,请添加如下空行:

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;
    }
}
drnojrws

drnojrws2#

无法保存所需的格式样式。但是有一个可选的设置。
下载googlestyle.xml并更改设置:

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

然后在settings.json中:

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

这不会让你的代码和你写的一样,但至少参数会被分成几行。虽然不是很好:

相关问题