简体   繁体   English

自定义Eclipse格式化程序以删除不必要的花括号

[英]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: 我想在Eclipse中配置一个格式化程序,它在它们包围return / break / continue控制语句时删除大括号:

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. 我发现在Preferences --> Java --> Code Style --> Formatter --> Control Statements如何将ifelse语句保留在一行上,而不是如何摆脱不必要的括号。 Does Eclipse let you define a formatter which eliminates redundant braces? Eclipse是否允许您定义一个消除多余括号的格式化程序?

Preferences > Java > Code Style > Clean Up , press Edit..., Code Style tab, set Use blocks in if/while/for/do statements to Only if necessary . 首选项> Java>代码样式>清理 ,按编辑...,代码样式选项卡,将Use blocks in if/while/for/do statements设置为Only if necessary After that you can simply do the hell on particular file or even whole project with Source > Clean Up... option :) 在那之后你可以简单地在特定文件或甚至整个项目上使用Source> Clean Up ...选项:)

PS Nevertheless, my choice is "Always". PS然而,我的选择是“永远”。

转到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. 你所指的是清理功能的一部分,但我没有看到你所提到的确切内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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