如何使 Eclipse 格式化程序将 while 语句保持在同一行?

How to make eclipse formatter keep while statement on same line?

我希望 eclipse 格式化程序保留

while(condition) doSomething();

按原样格式化,而不是将其更改为

while(condition)
    doSomething();

我似乎找不到执行此操作的选项。这可以用 eclipse 格式化程序实现吗?

没有这样的配置值。您可以使用

//@formatter:off
while(true) doStuff();
//@formatter:on

让eclipse不格式化代码。但是你需要为它配置eclipse的格式化程序。

或者您获取 eclipse 的源代码并实现您自己的格式化程序。

根据 this recently-updated bug report 判断,这似乎是不可能的。

如果你问了,我会在个人层面上补充,'which is great because this kind of formatting is horrific for many reasons',但你没有问,所以我会在这个问题上保持沉默:P

没错 - 该设置(尚)不可用(参见 this bug report/feature request

我还是建议格式化为

while(condition) {
    doSomething();
}

即使这会增加您的代码长度,我仍然认为它最容易阅读和修改。

this commit in July 2018 开始,现在可以做到这一点。

eclipse 格式化程序现在包含选项 "Keep simple 'for' loop body on same line," "Keep simple 'while' loop body on same line, and "在同一行保持简单 'do while' 循环体”,这完全符合我最初的要求。

在设置 -> Java -> 格式化程序中,如果你点击 "Edit",你可以在 New Lines -> In control statements -> Simple loops

下找到它们