VSCode Redhat 语言对 Java 格式化程序的支持强制最大行长度
VSCode Redhat language support for Java's formatter forces max line length
我正在为 Java 项目使用 VS Code IDE。我安装 Language Support for Java(TM) by Red Hat 用于格式化代码。
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",
}
我正在写一个这样的 class:
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;
}
}
在构造函数中,格式化程序似乎试图将每个参数填充到一行中,直到超过最大行长度。我们可以保留前者的格式吗?如果可以的话该怎么做?
无法保存所有您想要的格式样式。但是有可选的设置。
下载 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>",
这不会让你的代码和你写下来的一样,但至少参数被分成了行。虽然不是很好:
我尝试创建自定义 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;
}
}
我正在为 Java 项目使用 VS Code IDE。我安装 Language Support for Java(TM) by Red Hat 用于格式化代码。
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",
}
我正在写一个这样的 class:
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;
}
}
在构造函数中,格式化程序似乎试图将每个参数填充到一行中,直到超过最大行长度。我们可以保留前者的格式吗?如果可以的话该怎么做?
无法保存所有您想要的格式样式。但是有可选的设置。
下载 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>",
这不会让你的代码和你写下来的一样,但至少参数被分成了行。虽然不是很好:
我尝试创建自定义 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;
}
}