原文地址: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,並保存 如圖: