2012年8月31日 星期五

[Eclipse] 暫時關閉排版功能 - formatter:off

相信各位都知道,Eclipse 可以利用快捷鍵 (Ctrl+shift+f 或是 command+shift+f) 幫助排版。但有時候它就是這麼的"假熬" (台),反而破壞程式碼的可讀性!例如:
private static final String DATABASE_CREATE = "create table " 
      + TABLE_TOGO
      + "(" 
      + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_TITLE + " text not null, "
      + COLUMN_DESCRIPTION + " text not null" 
      + ");";
變成
private static final String DATABASE_CREATE = "create table " + TABLE_TOGO
      + "(" + COLUMN_ID + " integer primary key autoincrement, "
      + COLUMN_TITLE + " text not null, " + COLUMN_DESCRIPTION
      + " text not null" + ");";
真是糟糕透了!!!相信這個狀況大家都曾遇過,在下小弟我就提供一下解決方法囉!

開啟 Formatter

Window --> Preferences --> Java --> Code Style --> Formatter



預設的 Active profile 會是 "Eclipse [built-in]",這時候點選右邊的 Edit ,並在跳出的視窗中,選擇 Off/On Tags


然後將 Enable Off/On tags 的選項勾起來,並且修改 Profile name 後,按下 OK 就可以了!

使用方式

使用方式很簡單,將程式碼的前後,分別用 @formatter:off@formatter:on 的註解包起來就可以了,如下:

// @formatter:off
private static final String DATABASE_CREATE = "create table " 
      + TABLE_TOGO
      + "(" 
      + COLUMN_ID + " integer primary key autoincrement, " 
      + COLUMN_TITLE + " text not null, "
      + COLUMN_DESCRIPTION + " text not null" 
      + ");";
// @formatter:on

如此一來,就算使用自動整理,也不會被自作聰明的 Formatter 弄亂囉!

沒有留言: