简体   繁体   中英

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

I want the eclipse formatter to keep

while(condition) doSomething();

formatted as-is, rather than changing it to

while(condition)
    doSomething();

I can't seem to find an option for it to do this. Is this possible with the eclipse formatter?

That's true - that setting is not (yet) available (see this bug report/feature request )

I'd recommend anyway to format to

while(condition) {
    doSomething();
}

Even if this adds length to your code, I still believe it easiest to read and modify.

There is no such a configuration value. You can use

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

To let eclipse not format the code. But you need to configure eclipse's formatter for it.

Or you get eclipse's source and implement your very own formatter.

It looks like this is not possible, judging by this recently-updated bug report .

If you'd asked, I'd add on a personal level, 'which is great because this kind of formatting is horrific for many reasons', but you didn't ask, so I'll stay quiet on the subject :P

As of this commit in July 2018 , this is now possible.

The eclipse formatter now contains the options "Keep simple 'for' loop body on same line," "Keep simple 'while' loop body on same line, and "Keep simple 'do while' loop body on same line," which do exactly what I originally asked for.

In settings -> Java -> formatter, if you click "Edit", you can find them under New Lines -> In control statements -> Simple loops

Eclipse 格式化程序首选项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM