简体   繁体   中英

Customize an Eclipse formatter to remove unnecessary curly braces

I would like to configure a formatter in Eclipse that removes curly braces when they surround a return/break/continue control statement:

if (x == y) {
    return true;
} //that's bad

if (x == y) return true; //that's better

I've found under Preferences --> Java --> Code Style --> Formatter --> Control Statements how to keep if and else statements on one line, but not how to get rid of the unnecessary braces. Does Eclipse let you define a formatter which eliminates redundant braces?

Preferences > Java > Code Style > Clean Up , press Edit..., Code Style tab, set Use blocks in if/while/for/do statements to Only if necessary . After that you can simply do the hell on particular file or even whole project with Source > Clean Up... option :)

PS Nevertheless, my choice is "Always".

转到Java - >编辑器 - >打字,看看你是否正在寻找

Formatters aren't supposed to change the non-whitespace characters. What you're referring to would be a part of the Clean Up functionality, but I don't see exactly what you've mentioned as one of the things it can do.

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