IDEA快捷鍵拆解系列(十八):Live Templates篇

??這是IDEA快捷鍵拆解系列的第十八篇。

??本文將介紹一下IDEA強大的Live Template功能。

首先,我們要知道Live Template是在哪里定義的,先按Ctrl + Shift + S進入設置,接著在輸入框輸入“Live Template”就可以定位到了,如下圖所示。

Live Template

從上圖可以看到,IDEA官方已經(jīng)幫我們定義好了一些常用的Live Template,而且針對不同的文件類型也劃分了不同的Live Template Group。查看Live Template的快捷鍵是Ctrl + J,這里列幾個比較常用的。

iterate (迭代)

itar:Iterate elements of array,操作順序迭代數(shù)組

? ? //1.定義一個數(shù)組? ? String[] strings = new String[];? ? //2.輸入itar后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵快速進行光標切換? ? for (int i = 0; i < strings.length; i++) {? ? ? ? String string = strings[i];? ? ? }

ritar:Iterate elements of array in reverse order,反轉(zhuǎn)迭代數(shù)組

? ? ? String[] strings = new String[];? ? //輸入ritar后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵進行光標快速切換? ? for (int i = strings.length - 1; i >= 0; i--) {? ? ? ? String string = strings[i];? ? ? }

iter:Iterate (for each..in),F(xiàn)orEach迭代

? ? List<String> stringList = new ArrayList<>();? ? //輸入iter后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵進行光標快速切換? ? for (String s : stringList) {? ? ? }

fori:Create iteration loop,含下標的普通迭代

? ? //輸入fori后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵進行光標快速切換? ? for (int i = 0; i < ; i++) {? ? ? }

itli:Iterate elements of java.util.List,List迭代

? ? List<String> stringList = new ArrayList<>();? ? //輸入itli后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵進行光標快速切換? ? for (int i = 0; i < stringList.size(); i++) {? ? ? ? String s =? stringList.get(i);? ? ? }

itco:Iterate elements of java.util.Collection,iterator迭代

? ? List<String> stringList = new ArrayList<>();? ? //輸入itco后會有提示,按`Enter`確認后會自動輸出以下內(nèi)容,默認是按`Tab`鍵進行光標快速切換? ? for (Iterator<String> iterator = stringList.iterator(); iterator.hasNext(); ) {? ? ? ? String next =? iterator.next();? ? ? }

iten:Iterate java.util.Enumeration

itit:Iterate java.util.Iterator

ittok:Iterate tokens from String

itve:Iterate elements of java.util.Vector

define (定義)

St String

thr throw new

psf public static final

prsf private static final

psfi public static final int

psfs public static final String

psfs public static final String

geti:Inserts singleton method getInstance public static $CLASS_NAME$ getInstance() {? ? return ; }

ifn:Inserts if null statement if ($VAR$ == null) {? }

inn:Inserts if not null statement if ($VAR$ != null) {? }

inst:Checks object type with instanceof and down-casts it if ($EXPR$ instanceof $TYPE$) {? $TYPE$ $VAR1$ = ($TYPE$)$EXPR$;? $END$ }

lazy:Performs lazy initialization? ? if ($VAR$ == null) {? ? ? $VAR$ = new $TYPE$();? ? }

lst:Fetches last element of an array

mn:Sets lesser value to a variable

mx:Sets greater value to a variable

toar:Stores elements of java.util.Collection into array

main

psvm public static void main(String[] args) {}

print (打印)

sout:Prints a string to System.out System.out.println();

souf:Prints a formatted string to System.out System.out.printf("");

serr:Prints a string to System.err System.err.println("");

soutm:Prints current class and method names to System.out System.out.println("className.methodName");

soutv:Prints a value to System.out System.out.println("variable name = " + variable value);

soutp:Prints method parameter names and values to System.out System.out.println("parameter name = [" + parameter value + "]");

Maven

dep:dependency

<dependency>? ? <groupId></groupId>? ? <artifactId></artifactId>? ? <version></version> </dependency>

pl:plugin

<plugin>? ? <groupId></groupId>? ? <artifactId></artifactId>? ? <version></version> </plugin>

repo:repository

<repository>? ? <id></id>? ? <name></name>? ? <url></url> </repository>

SQL

col:new column definition $col$ $type$ $null$$END$

ins:insert rows into a table insert into $table$ ($columns$) values ($END$);

sel:select all rows from a table select * from $table$$END$;

selc:select the number of specific rows in a table select count(*) from $table$ $alias$ where $alias$.$END$;

selw:select specific rows from a table select * from $table$ $alias$ where $alias$.$END$;

tab:new table definition create table $table$ (? $col$ $type$ $null$$END$ );

upd:update values in a table update $table_name$ set $col$ = $value$ where $END$;

??當然了,Live Templat強大的地方不僅在于官方定義好的這部分,更重要是還支持自定義的Live Template。在學習工作中,我們可以嘗試把一些重復的代碼抽出來變成模板,接下來我們就來看看如何自定義Live Template。

配置自定義Live Template

配置自定義Live Template

修改作用范圍

修改作用范圍

修改作用范圍

新建POJO,輸入自定義的Live Template快捷鍵,如這里是cps,按Enter鍵選擇后光標將停留在VAR1的位置,默認按Tab鍵即可快速切換到VAR2的位置。

測試Live Template

測試Live Template

由于個人疏忽,所以截圖部分Live Template存在部分錯誤,下面是更正后的Live Template。


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容