原文地址:https://developer.connectopensource.org/display/CONNECTWIKI/Coding+templates+and+formatters
We make available code formatting templates for NetBeans and Eclipse, two IDEs the CONNECT development team uses. Community members are welcome to contribute templates for other IDEs.
NetBeans:
Download the NBFormatter.zip file.
Open Netbeans, and go to options -->Editor.
At the bottom left, click "Import", and then select the NBFormatter.zip file. Restart NetBeans for changes to take effect.
For Eclipse:
- Download formatter.xml
- Open Eclipse
- Go to Window -> Preferences
- Under General -> Editors -> Text Editors, check "Show print margin" and set "Print margin column" to 120
- Under Java -> Code Style -> Code Templates select "Automatically add comments for new methods and types
- Under Java -> Code Style -> Formatter import the file formatter.xml.
- Under Java -> Compiler -> Errors/Warnings expand the Annotations settings and set "Unhandled token in '@SuppressWarnings'" to Ignore
- Under Web -> HTML Files -> Editor set the Line width to 120 and select Indent using spaces, and set the indentation size to 4.
- Under XML -> XML Files -> Editor set the Line width to 120 and select Indent using spaces, and set the indentation size to 4.
- Under Ant -> Editor -> Formatter set the Max Line Width to 120 and unselect the use tab chars for spaces option.
- To prevent Eclipse from suggesting obscure suggestions for common classes (such as List), you can add the unused classes/packages to the list at Java -> Appearance -> Type Filters.
- Under General --> Workspace and under "New text file line delimiter" select UNIX.
这个模板主要指定了以下几条规范:
- 120个字符换行
- 4个空格(而非TAB)缩进
- 花括弧位置采用K&R风格
- 算数操作符前后加空格
Formatter 模版文件导入方法:
打开Reference, 通过 formatter 过滤找到 配置项,然后导入 yumihua_java_eclipse_formatter.xml 文件。
导入成功后请测试一下是否设置成功,可能每个独立项目都需要导入,如果你能发现一次导入适用于所有项目的办法,请更新此 wiki 页面
在自动保存的时候格式化文件的办法:
Window -> Preferences, 搜索 action, 找到 save action, 如图:
3,勾选 perform the selected action on save,选择 format source code, format all code,Organize imprt,并保存 如图: