简体   繁体   English

如何让eclipse自动添加大括号到IF语句?

[英]How to make eclipse automatically add braces to an IF statement?

In Java the following is completely valid: 在Java中,以下内容完全有效:

if (x == null)
    Y();
else
    Z();

I personally don't like it at all. 我个人根本不喜欢它。 I like all my IF statements to have braces: 我喜欢我所有的IF语句都有括号:

if (x == null) {
    Y();
} else {
    Z();
}

The eclipse formatter is wonderful and can beautify my code in many other ways. eclipse格式化程序非常棒,可以通过许多其他方式美化我的代码。

Is there a way to have it add the braces to IF statements? 有没有办法让它为IF语句添加大括号?

Under "Preferences": Java > Editor > Save Actions 在“首选项”下:Java>编辑器>保存操作

1) Check "Additional actions" 1)检查“附加操作”

2) Click "Configure…" 2)单击“配置...”

3) Go to the "Code Style" tab 3)转到“代码样式”选项卡

4) Check "Use blocks in if/while/for/do statements" and configure to your preferences 4)选中“在if / while / for / do语句中使用块”并配置您的首选项

Yes. 是。

Eclipse menu: Source -> Clean Up... Eclipse菜单:来源 - >清理......

Configure... -> Code Style -> Use blocks in if/while/for/do statements. 配置... - >代码样式 - >在if / while / for / do语句中使用块。

In Eclipse Oxygen (2017+), that option is now at: 在Eclipse Oxygen(2017+)中,该选项现在位于:

Window -> Preferences -> Java -> Code Style -> Clean Up -> Edit -> second tab "Code Style"

After changing, select the corresponding parts of your code and run the "Clean Up" option. 更改后,选择代码的相应部分并运行“清理”选项。

这就是我所做的:Eclipse -Preferences-Java-Code Style-Formatter-Edit-Braces在每个框中根据需要选择下一行,然后另存为另一个名称(您的选择)。

I usually do it by creating a code formatter by copying the built in eclipse formatter, here: 我通常通过复制内置的eclipse格式化程序来创建代码格式化程序,这里:

Windows > Preferences > Java > Code Style > Formatter 

Select the profile that you have just created. 选择刚刚创建的配置文件。 and click on Edit and go to "Braces" tab. 然后单击编辑并转到“大括号”选项卡。

Change the braces placement as you like (it's a WYSIWYG kind of editor, makes it easy) . 根据需要更改大括号位置(这是一种所见即所得的编辑器,使其变得简单)。

When I need to format, I just select all the text and Ctrl+Shift+F and it works fine for me. 当我需要格式化时,我只选择所有文本和Ctrl+Shift+F ,它对我来说很好。

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

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