• IntelliJ IDEA实时代码模板&快捷注释(Live Templates)


    Live Templates( 实时代码模板)的原理就是配置一些常用代码字母缩写,在输入简写时可以出现你预定义的固定模式的代码,使得开发效率大大提高,同时也可以增加个性化。本篇教程介绍如何使用IntelliJ IDEA快捷代码和自定义快捷代码模板,包括快捷注释的使用。


    目录:

    ☍ 快捷代码使用

    ☍ 自定义Live Template快捷代码

    ☍ IntelliJ IDEA代码注释


    ☍ 快捷代码使用

    ▴ 系统快捷代码模板的使用

    与Eclipse类似,IDEA也提供了许多快捷代码的模板,使用时只需要输入几个定义好的关键字符就可以自动生成对应的代码块。

    官方介绍 Live Templates:☛ [传送门]

    Postfix Completion默认模板

    Live Templates默认模板

    二者的区别在于Live Templates可以自定义,而 Postfix Completion 不可以。同时,有些操作二者都提供了模板,Postfix Templates较Live Templates快一些(速度差别很小,基本体会不到)

    Java中常用快捷代码

    psvm 或者 main :main()方法

    sout :输出语句

    类似的有
    变量.sout
    	System.out.println(变量);
    
    soutv
    	System.out.println("变量名 = " + 第一个成员变量变量(没有的找方法参数,再没有找方法局部变量,再没有输出"true" + true));  
    指定变量.soutv
    	System.out.println("变量名 = " + 指定变量);  
    
    soutm
    	System.out.println("当前类名.当前方法");
    
    soutp
    	soutp=System.out.println("方法形参名 = " + 形参名); //如果方法参数列表为空,则效果相当于sout
    

    fori:for循环语句

    类似的有
    length.fori
    	int length = 10;
        for (int i = 0; i < length; i++) {
        
        }
    length.forr
        for (int i = length; i > 0; i--) {
                
        }
    array.fori
        int array[] = new int[10];
        for (int i = 0; i < arr.length; i++) {
                
        }
    array.for
        int array[] = new int[10];
        for (int i : arr) {
                
        }
    /*同理还有array.forr*/
    
    iter:可生成增强 for 循环
        for (Object o : ) {
                
        }
    array.iter
        int array[] = new int[10];
     	for (int i : arr) {
                
        }
    
    itar:可生成普通 for 循环
         for (int i = 0; i < array.length; i++) {
                 = array[i];
                
         }
    

    list.for:集合list的for循环语句

    类似的有
    list.fori
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
                
        }
    list.forr   //从尾到头遍历 
    

    ifn:可生成if(xxx = null),xxx为定义过的变量

    image-20200326203453603
    类似的有
    inn
       if(xxx != null){
           
       }
    xx.null
       if (xx == null) {
                
       }
    xx.nn
       if (xx != null) {
                
       }
    

    prsf:可生成private static final

    类似的有
    psf: 可生成  public static final
    psfi:可生成  public static final int
    psfs:可生成  public static final String
    

    更多请自行摸索


    ☍ 自定义Live Template快捷代码

    在讲自定义快捷注释前必须先讲以下自定义快捷代码的规则,首先来看一下窗口的功能

    <abbreviation>是自定义模板的名,系统通过这个名来确定对象的模板;

    Dscription是对模板的描述,会在调用时提示;

    Template text是将要显示的完整代码;

    Edit variables是在模板代码中有变量时才可选,用来给变量赋值,变量命名规则为$name$;

    Expand with是当系统匹配到模板名时,选择如何调用模板代码,默认为Tab,个人比较喜欢enter

    Define是模板作用的位置,如果设置全局有效就选everywhere;

    创建好后点击OK就可以生效使用了,需要注意的是在使用时输入了模板名会被系统自动捕捉到,但是选择使用后不会保留这个模板名,只会显示Template text中的代码


    ☍ IntelliJ IDEA代码注释

    在eclipse我们很熟悉可以利用 /**,Enter在方法,类名等前一行来生成注释模板,但是IntelliJ IDEA并没有去按照原来的方法去实现。他引进了Live Template来达到类似效果。

    ▴ 类头的文档注释信息

    IDEA中也有在文件头注释的模板。但仅限于类文件头和所有文件头,在创建新文件时自动生成。配置过程如下

    img

    我的配置
    /**
     * @project: ${PROJECT_NAME}
     * @ClassName: ${NAME}
     * @author: Asio君
     * @creat: ${DATE} ${TIME}
     *  描述:${descript}
     */
    

    效果:

    常用的预设的变量

    ${PACKAGE_NAME} - the name of the target package where the new class or interface will be created.
    ${PROJECT_NAME} - the name of the current project.
    ${FILE_NAME} - the name of the PHP file that will be created.
    ${NAME} - the name of the new file which you specify in the New File dialog box during the file creation.
    ${USER} - the login name of the current user.
    ${DATE} - the current system date.
    ${TIME} - the current system time.
    ${YEAR} - the current year.
    ${MONTH} - the current month.
    ${DAY} - the current day of the month.
    ${HOUR} - the current hour.
    ${MINUTE} - the current minute.
    ${PRODUCT_NAME} - the name of the IDE in which the file will be created.
    ${MONTH_NAME_SHORT} - the first 3 letters of the month name. Example: Jan, Feb, etc.
    ${MONTH_NAME_FULL} - full name of a month. Example: January, February, etc.
    

    ▾ IntelliJ IDEA方法注释

    首先IDEA并没有提供类似Eclipse的方法/构造器/重写方法等的快捷注释,但是IDEA提供了自定义模板的功能

    这里以Eclipse的Code Template中方法的/**+enter生成快捷注释为例,在IDEA中自定义实现相同的功能

    首先在Editor下的Live Templates中点击右上角‘+’号,选择创建一个新的组

    自定义组名

    选中自定义组创建一个模板

    IDEA自定义注释

    上面知道了自定义代码模板,就可以以此为基础创建注释的模板

    与普通自定义模板区别的是,IDEA生成注释的方式是:/+模板名+快捷键

    自定义注释模板命名时可以不写/,在调用时/不会消失所以Template text中第一行的/就不需要加了,并且第一行代码必须顶格写

    使用时必须上以/+模板名的方式调用

    实际模板名可以任意,字符、中英文都可以,不过这里模仿Eclipse就是***

    Template text中的代码为(以我的为例):

    第一种方式

    *
     * @data: $date$-$time$
     * @User: asio
     * @method: $name$
     * @params: $params$
     * @return: $return$
     * @Description: 描述
     */
    

    第二种方式

    **
     * @data: $date$-$time$
     * @User: asio
     * @method: $name$
     * @params: $params$
     * @return: $return$
     * @Description: 描述
     */
    

    错误方式

    转载请添加本文链接 ☄https://www.cnblogs.com/asio/p/12582328.html


    本博客与CSDN博客༺ཌ༈君☠纤༈ད༻同步发布

  • 相关阅读:
    Spring Boot入门篇(基于Spring Boot 2.0系列)
    webpack性能优化配置
    Vuex的通俗详解
    Vuefilter() 过滤器
    transition页面加载触发问题解决方案
    VUE render VS template
    provide & inject 跨组件传值
    Vue 组件通讯(组件传值)
    js封装 DOM获取
    GIT SVN 版本管理 git + gitHub
  • 原文地址:https://www.cnblogs.com/asio/p/12582328.html
Copyright © 2020-2023  润新知