美文网首页
intellij定制配置

intellij定制配置

作者: 金色的清晨 | 来源:发表于2019-08-27 19:31 被阅读0次

编辑器设置

general

提供常用的编辑设置,目前关注如下:

  • Auto Import 包自动导入
  • Appearence 显示空格和行号

code style

默认java规则,提供了基本的代码风格。目前关注如下:

  • 一行的长度120字符,超出换行
  • 缩进统一采用4个字符
  • import包去*号,并且去掉static import的排序规则

规则配置好后,export配置给项目统一使用。
快捷键Ctr+Alt+L格式化当前java文件。
如果关联了git,在version control->commit dialog下可以配置代码提交时自动reformat代码

优化插件

通用插件

  • Save Action 保存操作,支持自动格式化代码,导入包等等操作
  • CheckStyle-IDEA 上面虽然设置了代码风格,但有可能漏掉格式化,这款插件可以检测代码风格是否正确,需要配置checkstyle.xml,见最后
  • FindBugs-IDE 扫描代码漏洞,给出建议
  • Key Promoter X 提示快捷键,并统计
  • .gitignore 生成各种忽悠提交文件

java后端

  • Lombok plugin java必备
  • free-idea-mybatis 关联xml和dao
  • better-mybatis-generator 替代mybatis generator,根据数据库表生成xml,entity,dao
  • MyBatis Log Plugin 本地调试时,会拼接sql和参数

模板

checkstyle.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
          
<module name = "Checker">
    <property name="charset" value="UTF-8"/>

    <property name="severity" value="warning"/>

    <property name="fileExtensions" value="java, properties, xml"/>
    
    <!-- Checks that there are no tab characters ('\t') in the source code.
         Rationale:
               Developers should not need to configure the tab width of their text editors in order to be able to read source code.
               From the Apache jakarta coding standards: In a distributed development environment, when the commit messages get sent to a mailing list, they are almost impossible to read if you use tabs.                              -->
    <!-- See http://checkstyle.sf.net/config_whitespace.html -->
    <!--文件中不允许出现tab字符-->
    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>

    <module name="TreeWalker">
        <!--Checks that the outer type name and the file name match. For example, the class Foo must be in a file named Foo.java.-->
        <!--顶级类名称必须与源文件名相同-->
        <module name="OuterTypeFilename"/>
        
        <!--Checks for long lines.
            Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, 
            e.g. if the IDE displays additional information like project tree, class hierarchy, etc-->
        <!--每行最多120字符-->
        <module name="LineLength">
            <property name="max" value="120"/>
            <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
        </module>
        
        
        <!--Checks that there are no import statements that use the * notation.
            Rationale: Importing all classes from a package or static members from a class leads to tight coupling between packages or 
                       classes and might lead to problems when a new version of a library introduces name clashes-->
        <!--import语句不允许使用星号-->
        <!--<module name="AvoidStarImport"/>-->
        
        <!--Checks that each top-level class, interface or enum resides in a source file of its own. 
            Official description of a 'top-level' term:7.6. Top Level Type Declarations. 
            If file doesn't contains public class, enum or interface, top-level type is the first type in file-->
        <!--<module name="OneTopLevelClass"/>-->
        
        <!--Checks for braces around code blocks.-->
        <!--检查是否应该使用括号的地方没有加括号-->
        <module name="NeedBraces"/>

        <!--Checks for the placement of left curly braces ('{') for code blocks. The policy to verify is specified using the property option.
            Policies eol and nlow take into account the property maxLineLength.-->
        <!--option: 定义左大括号'{'显示位置,eol在同一行显示,nl在下一行显示
          maxLineLength: 大括号'{'所在行行最多容纳的字符数
          tokens: 该属性适用的类型,例:CLASS_DEF,INTERFACE_DEF,METHOD_DEF,CTOR_DEF -->
        <!-- 1) 左大括号前不换行。2) 左大括号后换行-->
        <module name="LeftCurly">
            <property name="option" value="eol"/>
        </module>

        <!-- 代码缩进采用 4 个空格-->
        <module name="Indentation"/>

        <!--Checks the placement of right curly braces ('}') for if-else, try-catch-finally blocks, while-loops, for-loops, method definitions, class definitions, constructor definitions,
            instance and static initialization blocks.
            The policy to verify is specified using the property option.
            -->
        <!--右大括号前换行, 右大括号后还有 else 等代码则不换行;表示终止右大括号后必须换行-->
        <module name="RightCurly">
            <property name="id" value="RightCurlySame"/>
            <property name="tokens" value="LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE"/>
        </module>
        <module name="RightCurly">
            <property name="id" value="RightCurlyAlone"/>
            <property name="option" value="alone"/>
            <property name="tokens"
                      value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT, INSTANCE_INIT"/>
        </module>
        
        <!--Checks that a token is surrounded by whitespace. Empty constructor, method, class, enum, interface, loop bodies (blocks), lambdas of the form-->
        <!--任何运算符左右必须加一个空格, 左括号和后一个字符之间不出现空格, 右括号和前一个字符之间也不出现空格-->
        <module name="WhitespaceAround">
            <property name="allowEmptyConstructors" value="true"/>
            <property name="allowEmptyMethods" value="true"/>
            <property name="allowEmptyTypes" value="true"/>
            <property name="allowEmptyLoops" value="true"/>
        </module>
        
        <!--Checks that there is only one statement per line.
            Rationale: It's very difficult to read multiple statements on one line.
            In the Java programming language, statements are the fundamental unit of execution. All statements except blocks are terminated by a semicolon. Blocks are denoted by open and close curly braces.
            OneStatementPerLineCheck checks the following types of statements: variable declaration statements, empty statements, import statements, 
            assignment statements, expression statements, increment statements, object creation statements, 'for loop' statements, 'break' statements, 'continue' statements, 'return' statements.-->
        <!--每行只能有一条语句-->
        <module name="OneStatementPerLine"/>
        
        <!--Checks that each variable declaration is in its own statement and on its own line.-->
        <!-- 每一行只能定义一个变量 -->
        <module name="MultipleVariableDeclarations"/>
        
        <!-- Checks the style of array type definitions. Some like Java-style: public static void main(String[] args) and some like C-style: public static void main(String args[])
          检查再定义数组时,采用java风格还是c风格,例如:int[] num是java风格,int num[]是c风格。默认是java风格-->
        <module name="ArrayTypeStyle"/>
        
        <!--  Checks that switch statement has "default" clause. 检查switch语句是否有‘default’从句
           Rationale: It's usually a good idea to introduce a default case in every switch statement.
           Even if the developer is sure that all currently possible cases are covered, this should be expressed in the default branch,
            e.g. by using an assertion. This way the code is protected aginst later changes, e.g. introduction of new types in an enumeration type. -->
        <!--在一个 switch 块内,都必须包含一个 default 语句并且放在最后,即使它什么代码也没有-->
        <module name="MissingSwitchDefault"/>

        <!--Checks for fall-through in switch statements. Finds locations where a case contains Java code but lacks a break, return, throw or continue statement.
            The check honors special comments to suppress the warning. By default the text "fallthru", "fall through", "fallthrough", "falls through" and "fallsthrough" are recognized (case sensitive). 
            The comment containing these words must be all on one line, and must be on the last non-empty line before the case triggering the warning or on the same line before the case (ugly, but possible).-->
        <!--switch中case后是否加入了跳出语句,例如:return、break、throw、continue -->
        <module name="FallThrough"/>
        
        <!--  Checks that long constants are defined with an upper ell. That is ' L' and not 'l'. This is in accordance to the Java Language Specification,  Section 3.10.1.
          looks a lot like 1.         
          检查是否在long类型是否定义了大写的L.字母小写l和数字1(一)很相似。-->
        <module name="UpperEll"/>
        
        <!--检查修饰符的顺序,默认是 public,protected,private,abstract,static,final,transient,volatile,synchronized,native -->
        <module name="ModifierOrder"/>

        <!--Checks that a token is followed by whitespace.-->
        <!--逗号后面必须有空格-->
        <module name="WhitespaceAfter">
            <property name="tokens" value="COMMA"/>
        </module>
        
        <!--Checks line wrapping with separators.-->
        <!--方法调用的点符号与下文一起换行-->
        <module name="SeparatorWrap">
            <property name="id" value="SeparatorWrapDot"/>
            <property name="tokens" value="DOT"/>
            <property name="option" value="nl"/>
        </module>
        <!--在多个参数超长,逗号后进行换行-->
        <module name="SeparatorWrap">
            <property name="id" value="SeparatorWrapComma"/>
            <property name="tokens" value="COMMA"/>
            <property name="option" value="EOL"/>
        </module>
        
        <!--命名规范, 参见阿里的命名规范-->
        <module name="PackageName">
            <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
        </module>
        <module name="TypeName">
            <property name="format" value="^[A-Z][a-zA-Z0-9]*$"/>
        </module>       
        <module name="MemberName">
            <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
        </module>
        <module name="ParameterName">
            <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
        </module>
<!--        <module name="CatchParameterName">
            <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
        </module>-->
        <module name="LocalVariableName">
            <property name="tokens" value="VARIABLE_DEF"/>
            <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
        </module>
        <module name="ConstantName">
            <property name="applyToProtected" value="false"/>
            <property name="applyToPrivate" value="false"/>
            <property name="applyToPackage" value="false"/>
            <property name="format" value="^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
        </module>
        <module name="MethodName">
            <property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
        </module>

        
        <!--Checks that overload methods are grouped together.
         当一个类有多个构造方法,或者多个同名方法,这些方法应该按顺序放置在一起,便于阅读。-->
        <module name="OverloadMethodsDeclarationOrder"/>
        
    </module>
</module>

相关文章

网友评论

      本文标题:intellij定制配置

      本文链接:https://www.haomeiwen.com/subject/yijvwqtx.html