Loading

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

posted @ 2022-04-21 10:17  jevan  阅读(186)  评论(0编辑  收藏  举报