在 Eclipse 中重新格式化代码样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2411024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reformat code style in Eclipse
提问by Amir Rachum
I'm looking for an automatic way in eclipse to reformat curly braces to K&R style, eliminating braces for single statements if, etc. Is there such a way?
我正在 eclipse 中寻找一种自动方法来将大括号重新格式化为 K&R 样式,消除单个语句的大括号 if 等。有这样的方法吗?
Thanks.
谢谢。
采纳答案by stacker
Before you reformat you need to customize the formatter settings in the Preferences Dialog Java/ Code Style / Formatter
to your K&R settings if the default settings don't match your requirements.
Java/ Code Style / Formatter
如果默认设置不符合您的要求,则在重新格式化之前,您需要将“首选项”对话框中的格式化程序设置自定义为您的 K&R 设置。
The Preferences Dialog is opened from Mainmenu / Window / Preferences
首选项对话框从 Mainmenu / Window / Preferences
You can even configure a save action Java / Editor / Save Action
to format source code on save.
您甚至可以配置保存操作Java / Editor / Save Action
以在保存时格式化源代码。
回答by Fortega
A lot of options for formatting can be chosen in the preferences dialog (Java -> Code Style -> Formatter).
可以在首选项对话框(Java -> Code Style -> Formatter)中选择许多格式化选项。
After that, you can simply format your code by pressing ctrl-shift-f (windows)
之后,您可以通过按 ctrl-shift-f (windows) 来简单地格式化您的代码
Also, you can choose to automatically format your code whenever you save your file. This is also in the preferences dialog (Java -> Editor -> Save Actions)
此外,您可以选择在保存文件时自动设置代码格式。这也在首选项对话框中(Java -> Editor -> Save Actions)
回答by Santi P.
You can customize how Java code is formatted going to Window > Preferences
. There, choose Java > Code Style > Formatter
, click on New...
to create a new profile. Then Edit...
it.
您可以自定义 Java 代码的格式Window > Preferences
。在那里,选择Java > Code Style > Formatter
,单击New...
以创建新配置文件。然后Edit...
它。
However, I'm quite sure Eclipse's code formatter won't add or remove braces, it will simply position them according to your style rules. You will need a separate tool to do this or a fancy search and replace.
但是,我很确定 Eclipse 的代码格式化程序不会添加或删除大括号,它只会根据您的样式规则来定位它们。您将需要一个单独的工具来执行此操作或进行花哨的搜索和替换。
回答by Hans Westerbeek
right click in your editor => source => format
右键单击您的编辑器 => 源 => 格式