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: 描述
 */

在Edit variables中可以指定變量,默認日期格式為yyyy/mm/dd,通過data("yyyy-MM-dd")可以指定日期格式
方法參數methodParameters()默認格式為[parm1,parm2],可使用自定義方式

groovyScript("def result = '';def params = \"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();if (params.size() > 1) {result +='\\n * @param ' + params[0] + ' \\n';for(i = 1; i < params.size(); i++) {result += ' * @param ' + params[i] + ((i < params.size() - 1) ? ' \\n' : ''); };}else if (params.size()==1) {if (params[0] != '') {result+='\\n * @param ' +params[0] + ' ';} }else {result += params[0] + ' '; };return result", methodParameters())

錯誤方式

轉載請添加本文鏈接 ☄https://www.cnblogs.com/asio/p/12582328.html


本博客與CSDN博客༺ཌ༈君☠纖༈ད༻同步發布


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM