實現效果:Ctrl+S會自動格式化並保存代碼。

應用上圖所示效果之后,在每次對Eclipse保存的時候都會實現自動格式化代碼。
1. Fomated All lines,格式化該文件的所有代碼;還是 Format edited lines 的好,因為如果是修改別人的代碼,破壞別人的代碼風格就不好了。
2. 當然了Ctrl + Shift + F運來格式化代碼,也是非常不錯的。
3. 配置eclipse-formatter.xml文件;下載eclipse-formatter.xml文件,放到eclipse安裝目錄下。Eclipse中,選擇Windows->Preferences->code style。選擇 Formatter 選擇Import,找到eclipse-formatter.xml的文件目錄,選擇該文件,選擇OK導入。
/** * A sample source file for the code formatter preview */
package mypackage; import java.util.LinkedList; public class MyIntStack { private final LinkedList fStack; public MyIntStack() { fStack = new LinkedList(); } public int pop() { return ((Integer) fStack.removeFirst()).intValue(); } public void push(int elem) { fStack.addFirst(new Integer(elem)); } public boolean isEmpty() { return fStack.isEmpty(); } }
上面代碼是Formatter的格式。但是有一個問題,自動保存的時候,代碼的自動換行會把代碼搞的特別難看。

解決辦法就是:如上所示操作,指定800行再換行就好了。
最后建議,最好團隊統一來使用格式,不然的話,每次保存的時候,引起行數的變化,SVN下次又會提交,頻繁提交這些不同格式問題的文件是沒有意義的。
