1. 程式人生 > 其它 >Save Actions 配置與選項詳解,看完再也不迷惑了

Save Actions 配置與選項詳解,看完再也不迷惑了

Save Actions 是idea一款格式自動化的外掛,非常好用,但是在設定Sava Actions時往往是一頭霧水,所以研究了一下各個選項的意思。

我的設定


設定詳解

General 基本設定

  • Activate save actions on save(before saving each file,performs the configured actions below)
    儲存時自動格式化

  • Activate save actions on shortcut
    使用快捷鍵儲存時自動格式化
    使用場景:收到一份格式很亂的檔案,可以使用這個快捷鍵自動格式化

  • Activate save actions on batch(Code>Save Actions>Execute on multiple files)
    儲存時批量格式化
    這個沒用過,因為有的時候你的格式化設定和同事的不一樣,一起格式化了會導致合併時候有衝突

Formatting Actions 格式化觸發設定

  • Optimize imports
    優化匯入(沒有用到的類自動刪去import,這個一般要勾選)

  • Reformat file
    重新格式化檔案(只要儲存檔案就會自動格式化)

  • Reformat only change code
    僅僅當代碼變化時重新格式化
    以上兩個設定是互斥的,意思是,要麼程式碼變化時候觸發,要麼檔案變化時觸發
    我一般勾選上面那個,覺得好用一點

  • Rerrange fields and methods(configured in “File>Setting>Editor>Code Style>(…)>Arragement”)
    重新調整欄位和方法的範圍
    不知道什麼意思,沒用過

Build Actions build設定

是在build專案時候用到的設定,這裡就不討論了

Java Inspection and Quick Fix 具體格式化設定

  • Add final modifier to field
    給欄位新增final修飾符
    沒用過不知道啥意思

  • Add final modifier to local variable or parameter
    向區域性變數或引數新增final修飾符
    使用效果如下,所有的區域性引數都添加了final修飾符

   public void printStr(final String str1, final String str2, final String str3) {
        System.out.println(str1 + str2 + str3);
    }

這個選項我不會勾選,因為有的時候形參會發生變化

  • Add final modifier to local variable or parameter except if it is implicit
    向非隱式的區域性變數或引數新增final修飾符
    不太理解這個非隱式是什麼意思

  • Add static modifier to methods
    給方法新增static修飾符
    效果如下,不要勾選

    public static void testStatic(String str) {
        System.out.println(str);
    }
  • Add this to field access
    欄位的使用加上“this”指標
    可勾可不勾,看個人的使用習慣。

  • Add this to method access
    方法使用加上this
    跟上面那個選項意思差不多,可勾可不勾,看程式碼習慣

  • Add class qualifier to static member access
    靜態成員訪問新增類限定符
    就是說靜態方法訪問了靜態變數的話,會自動加上類.變數名,可以點

  • Add class qualifier to static member access outside declaring class
    宣告類外的靜態成員訪問新增類限定符
    不太懂這個宣告之外的類是什麼意思,反正跟上面那個是互斥的

  • Add missing @Override annotions
    新增漏寫的@Override註解
    可以勾

  • Add blocks to if/while/for statements
    給if/while/for語句新增大括號

    public String judgeIt(int a) throws Exception {
        if (a == 3) {
            return "yes";
        }
        throw new Exception();
    }
  • Remove blocks from if/while/for statements
    給if/while/for語句移除大括號
    public String judgeIt(int a) throws Exception {
        if (a == 3) return "yes";
        throw new Exception();
    }

我會勾選下面那個,好像java6以後如果沒有別的程式碼,可以直接return,比較酷

  • Remove unnessary this to field and method
    給欄位或者方法去掉不必要的“this”
    看個人習慣,我不怎麼喜歡用this,所以會勾選這個

-Remove final from private method
私有方法去掉final關鍵字

    private final String getStr(final String str) {// 設定後final關鍵字儲存時會被去掉
        return str;
    }

其實我個人也不知道為什麼私有方法前面要加final關鍵字,如果有java基礎好一點的大神看到我的文章,請評論或私信告訴我~

    • Remove explicit generic type for diamond
      刪除顯式泛型型別的尖括號
      這個我不懂什麼意思,就沒有勾

    • Remove unused suppress warning annotation
      移除沒用的Suppress警告

    • Remove unnecessary semicolon
      刪除不必要的分號

    • Change visibility of field or method to lower access
      更改欄位或方法的可見性以降低訪問許可權
      這個我也不知道。。。



轉自:https://blog.csdn.net/weixin_44712778/article/details/117332374