IDEA下利用Live Template编写注释模版

注释模版

注释模版是指在编写代码时针对不同的类,方法或者其他文件时自动生成的注释信息。在eclipse我们很熟悉可以利用 /**,Enter在方法,类名等前一行来生成注释模板,但是IntelliJ IDEA并没有去按照原来的方法去实现。他引进了Live Template来达到类似效果。

从eclipse到IDEA

  1. eclipse中的注释模板在code Template中,它存储的形式xml文件。
    修改相关配置如下图:
    eclipse code template

    模板样式如下:
/**
* @Title: ${file_name}
* @Package ${package_name}
* @Description: TODO
* @author syncwt
* @date ${date} ${time}
* @version V1.0
*/

配置好之后只需在方法,类名等前一行输入/**,Enter即可生成注释。

  1. IDEA中也有在文件头注释的模板。但仅限于类文件头和所有文件头。配置如下图:


    IDEA File and Code Template

    这显然难以满足我们开发的需求,这时候就需要用Live Template

Live Template

介绍

用中文应该叫做热加载模板。它的原理就是配置一些常用代码字母缩写,在输入简写时可以出现你预制的模板内容,使得开发效率大大提高。最简单的例子就是在Java中输入sout会出现System.out.println();如图所示:

代码提示

同样,注解的模板配置也是采用这样的原理和形式来进行配置。

配置

我当前的版本是2017.1EAP,其他版本应该都差不多。
在配置当中找到Live Template,右边加号先添加一个TemplateGroup,选中该分组再点击加号添加一个Live Template.
Abbreviation中填写命令,Description填写描述,Template text填写你的配置模板。

配置

模板配置的规则是这样子的:
变量名以$<variable_name>$的形式出现,调用的方法也是先以变量形式出现,写完模板信息后去编辑变量(Edit variables)
编辑变量

找到相应的的方法对应变量名即可。方法名即功能描述表见附件。
我这个版本有点坑,变量名的default值无法显示,所以我直接在Template text中写死了一些信息

样例

Abbreviation: cc
Description: class comment template

Template text:
/**
 * class_name: $CLASS_NAME$
 * package: $PACKAGE_NAME$
 * describe: TODO
 * creat_user: wanwt@senthinkcom
 * creat_date: $CREAT_DATE$
 * creat_time: $CREAT_TIME$
 **/

 Template variables:
 CLASS_NAME -> className()
 PACKAGE_NAME -> currentPackage()
 CREAT_DATE -> date()
 CREAT_TIME -> time()
Abbreviation: mc
Description: method comment template

Template text:
/**
 * class_name: $METHOD_NAME$
 * param: $METHOD_PARAM$
 * describe: TODO
 * creat_user: wanwt@senthinkcom
 * creat_date: $CREAT_DATE$
 * creat_time: $CREAT_TIME$
 **/

 Template variables:
 METHOD_NAME -> methodName()
 METHOD_PARAM -> methodParameters()
 CREAT_DATE -> date()
 CREAT_TIME -> time()

效果

效果

不足

  1. live template中的函数方法是读取当前函数体的属性,所以只有在该方法内使用该命令才能获取,类没有这个问题。
  2. Template variables的Expression不能叠加方法。定制化程度不够好。

附件

Predefined functions to use in live template variables

Item
Description
annotated("annotation qname")
Creates a symbol of type with an annotation that resides at the specified location. For an example, see Live Templates in the iterations group.
arrayVariable()
Suggests all array variables applicable in the current scope. For an example, see Live Templates in the iterations group.
anonymousSuper()
Suggests a supertype for a Kotlin object expression.
camelCase(String)
Returns the string passed as a parameter, converted to camel case. For example, my-text-file/my text file/my_text_file will be converted to myTextFile.
capitalize(String)
Capitalizes the first letter of the name passed as a parameter.
capitalizeAndUnderscore(sCamelCaseName)
Capitalizes the all letters of a CamelCase name passed as a parameter, and inserts an underscore between the parts. For example, if the string passed as a parameter is FooBar, then the function returns FOO_BAR.
castToLeftSideType()
Casts the right-side expression to the left-side expression type. It is used in the iterations group to have a single template for generating both raw-type and Generics Collections.
className(sClassName)
Returns the name of the current class (the class where the template is expanded).
classNameComplete()
This expression substitutes for the class name completion at the variable position.
clipboard()
Returns the contents of the system clipboard.
snakeCase(String)
Returns CamelCase string out of snake_case string. For example, if the string passed as a parameter is foo_bar, then the function returns fooBar.
complete()
This expression substitutes for the code completion invocation at the variable position.
completeSmart()
This expression substitutes for the smart type completion invocation at the variable position.
componentTypeOf (<array variable or array type>)
Returns component type of an array. For example, see the Live Templates in the iterations group in the other group.
currentPackage()
Returns the current package name.
date(sDate)
Returns the current system date in the specified format.
By default, the current date is returned in the default system format. However, if you specify date format in double quotes, the date will be presented in this format:

decapitalize(sName)
Replaces the first letter of the name passed as a parameter with the corresponding lowercase letter.
descendantClassEnum(<String>)
Shows the children of the class entered as a string parameter.
enum(sCompletionString1,sCompletionString2,...)
List of comma-delimited strings suggested for completion at the template invocation.
escapeString(sEscapeString)
Escapes the specified string.
expectedType()
Returns the type which is expected as a result of the whole template. Makes sense if the template is expanded in the right part of an assignment, after return, etc.
fileName(sFileName)
Returns file name with extension.
fileNameWithoutExtension()
Returns file name without extension.
firstWord(sFirstWord)
Returns the first word of the string passed as a parameter.
groovyScript("groovy code")
Returns Groovy script with the specified code.
You can use groovyScript macro with multiple arguments. The first argument is a script text that is executed or a path to the file that contains a script. The next arguments are bound to _1, _2, _3, ..._nvariables that are available inside your script. 
Also, _editor variable is available inside the script. This variable is bound to the current editor.
guessElementType (<container>)
Makes a guess on the type of elements stored in a java.util.Collection. To make a guess, IntelliJ IDEA tries to find the places where the elements were added to or extracted from the container.
iterableComponentType(<ArrayOrIterable>)
Returns the type of an iterable component, such as an array or a collection.
iterableVariable()
Returns the name of a variable that can be iterated.
lineNumber()
Returns the current line number.
lowercaseAndDash(String)
Returns lower case separated by dashes, of the string passed as a parameter. For example, the string MyExampleName is converted to my-example-name.
methodName()
Returns the name of the embracing method (where the template is expanded).
methodParameters()
Returns the list of parameters of the embracing method (where the template is expanded).
methodReturnType()
Returns the type of the value returned by the current method (the method within which the template is expanded).
qualifiedClassName()
Returns the fully qualified name of the current class (the class where the template is expanded).
Clear the Shorten FQ names check box.
rightSideType()
Declares the left-side variable with a type of the right-side expression. It is used in theiterations group to have a single template for generating both raw-type and Generics Collections.
snakeCase(sCamelCaseText)
Returns snake_case string out of CamelCase string passed as a parameter.
spaceSeparated(String)
Returns string separated with spaces out of CamelCase string passed as a parameter. For example, if the string passed as a parameter is fooBar, then the function returns foo bar.
subtypes(sType)
Returns the subtypes of the type passed as a parameter.
suggestIndexName()
Suggests the name of an index variable. Returns i if there is no such variable in scope, otherwise returns j if there is no such variable in scope, etc.
suggestVariableName()
Suggests the name for a variable based on the variable type and its initializer expression, according to your code style settings that refer to the variable naming rules.
For example, if it is a variable that holds an element within iteration, IntelliJ IDEA makes a guess on the most reasonable names, also taking into account the name of the container being iterated.
suggestFirstVariableName(sFirstVariableName)
Doesn't suggest true, false, this, super.
time(sSystemTime)
Returns the current system time.
typeOfVariable(VAR)
Returns the type of the variable passed as a parameter.
underscoresToCamelCase(sCamelCaseText)
Returns the string passed as a parameter with CamelHump letters substituting for underscores. For example, if the string passed as a parameter is foo_bar, then the function returns fooBar.
underscoresToSpaces(sParameterWithSpaces)
Returns the string passed as a parameter with spaces substituting for underscores.
user()
Returns the name of the current user.
variableOfType(<type>)
Suggests all variables that may be assigned to the type passed as a parameter, for example variableOfType("java.util.Vector"). If you pass an empty string ("") as a parameter, suggests all variables regardless of their types.
JsArrayVariable
Returns JavaScript array name.
jsClassName()
Returns the name of the current JavaScript class.
jsComponentType
Returns the JavaScript component type.
jsMethodName()
Returns the name of the current JavaScript method.
jsQualifiedClassName
Returns the complete name of the current JavaScript class.
jsSuggestIndexName
Returns a suggested name for an index.
jsSuggestVariableName
Returns a suggested name for a variable.

参考

Live Template Variables

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容