我们的工作有一个非常奇怪的编码约定,而且我没有设法在 Eclipse 中正确设置 Java Formatter 来执行我想要的操作。约定是:
[更新]
我们的约定中没有规定“{”后面是否应该换行。这些示例实际上使用了换行符(到目前为止我看到的几乎所有约定都表明或暗示在“{”和“}”之后应该始终是换行符)。所以样本 1 和样本 2 都是“语法正确的”。
[/更新]
由于这破坏了代码,我们的团队决定编写这样的代码(不,这不是我的选择!):
public void methode(final boolean b)
{ if (b)
{ do.something();
}
else
{ do.somethingElse();
}
}
但是在格式化程序中我只能得到这个:
public void methode(final boolean b)
{
if (b)
{
do.something();
}
else
{
do.somethingElse();
}
}
或者这个:
public void methode(final boolean b) {
if (b) {
do.something();
}
else {
do.somethingElse();
}
}
有没有办法告诉格式化程序在“{”之前换行,但在“{”之后不换行?我知道第一种风格很糟糕,我很乐意使用后两种风格中的一种,但这是公司的决定。
we have here at work a very strange coding convention, and I didn't managed to setup the Java Formatter in Eclipse right to do what I want. The convention says:
- Before a curly brace "{" there should always be a new Line
[UPDATE]
There is no rule in our convention saying, if after a "{" should be a line break or not. The examples actually use a line break (and almost ANY convention I saw so far says or implies that after a "{" and a "}" should always be a line break). So sample 1 and 2 are both "syntactically correct".
[/UPDATE]
As this blows the code, our team have decided to write code like this (no, this wasn't my choice!):
public void methode(final boolean b)
{ if (b)
{ do.something();
}
else
{ do.somethingElse();
}
}
But in the formatter I only managed to get this:
public void methode(final boolean b)
{
if (b)
{
do.something();
}
else
{
do.somethingElse();
}
}
or this:
public void methode(final boolean b) {
if (b) {
do.something();
}
else {
do.somethingElse();
}
}
Is there a way to tell the formatter to break lines before a "{" but not after that? I know the first style is awful, and I would be pleased to use one of the last two, but this is a company decision.
发布评论
评论(2)
所以,这里有关于这个主题的信息。我还做了一些研究。这里令人厌恶的 Brace-Style(示例 1)有一个名称: Horstmann 支撑样式 或此处 霍斯特曼。有一小群人捍卫它,因为它结合了 K&R 和 Allman(样本 2)风格的优点。由于支架排列整齐,因此不会“浪费”空间。
但这并不是唯一的事实。这种风格对于VCS来说惨不忍睹。如果需要在左大括号和第一个语句之间添加一行,则需要首先断开该行,然后将新行放在那里。在差异或合并中,您将看到的不是“添加了一行”,而是“一行被两行交换了”。但实际上旧的说法被你改变了。
所以还有一个说法,不要使用这种风格。
So, here an Information about this Topic. I have done some more research. The here so abominated Brace-Style (sample 1) has a name: The Horstmann Brace Style or here Horstmann. There is a small group of people defending it, as it combines the advantages from the K&R and the Allman (sample 2) Style. As the braces are lined up, and there is no "waste" of space.
But this is not the only true. This style miserable for the VCS. If you need to add a Line between the opening brace and the first statement, you need first to break the line, and put your new line there. In the diff or merge you will see then not "one line been added", but "one line been exchanged by two lines". But the actually old statement was changed by you.
So another argument, not to use this style.
您可以关闭代码格式化程序的相关部分并改用模板吗?例如,当键入
并点击 ctrl+space 时将调用 private_method 模板。然后,您可以将私有模板修改为类似的内容 -
您必须对其他块语句执行类似的操作,并且必须修改编码风格才能开始更频繁地使用模板,但我认为它可以工作。
Could you turn off the relevant parts of the code formatter and make use of the Templates instead. For example when typing
and hitting ctrl+space would invoke the private_method template. You could then modify the private template to be something like this -
You'd have to do similar things to the other block statements and you'd have to modify your coding style to start using the templates more often, but I reckon it could work.