我正在为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;
}
}
在构造函数中,似乎格式化程序试图将每个参数填充到一行中,直到超过最大行长。我们能保留发信人的格式吗?如果可以的话,我们怎么做?
2条答案
按热度按时间yks3o0rb1#
我试着创造一种习惯
eclipse-java-google-style.xml
并按照@molly wang的建议进行修改。我发现了一些修改可以得到我想要的结果:格式化后,代码变为:
如果希望构造函数的关闭参数位于下一行中,请添加如下空行:
drnojrws2#
无法保存所需的格式样式。但是有一个可选的设置。
下载googlestyle.xml并更改设置:
然后在settings.json中:
这不会让你的代码和你写的一样,但至少参数会被分成几行。虽然不是很好: