IDEA下利用Live Template編寫注釋模版

注釋模版

注釋模版是指在編寫代碼時(shí)針對(duì)不同的類,方法或者其他文件時(shí)自動(dòng)生成的注釋信息。在eclipse我們很熟悉可以利用 /**,Enter在方法,類名等前一行來(lái)生成注釋模板,但是IntelliJ IDEA并沒(méi)有去按照原來(lái)的方法去實(shí)現(xiàn)。他引進(jìn)了Live Template來(lái)達(dá)到類似效果。

從eclipse到IDEA

  1. eclipse中的注釋模板在code Template中,它存儲(chǔ)的形式xml文件。
    修改相關(guān)配置如下圖:
    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

    這顯然難以滿足我們開(kāi)發(fā)的需求,這時(shí)候就需要用Live Template

Live Template

介紹

用中文應(yīng)該叫做熱加載模板。它的原理就是配置一些常用代碼字母縮寫,在輸入簡(jiǎn)寫時(shí)可以出現(xiàn)你預(yù)制的模板內(nèi)容,使得開(kāi)發(fā)效率大大提高。最簡(jiǎn)單的例子就是在Java中輸入sout會(huì)出現(xiàn)System.out.println();如圖所示:

代碼提示

同樣,注解的模板配置也是采用這樣的原理和形式來(lái)進(jìn)行配置。

配置

我當(dāng)前的版本是2017.1EAP,其他版本應(yīng)該都差不多。
在配置當(dāng)中找到Live Template,右邊加號(hào)先添加一個(gè)TemplateGroup,選中該分組再點(diǎn)擊加號(hào)添加一個(gè)Live Template.
Abbreviation中填寫命令,Description填寫描述,Template text填寫你的配置模板。

配置

模板配置的規(guī)則是這樣子的:
變量名以$<variable_name>$的形式出現(xiàn),調(diào)用的方法也是先以變量形式出現(xiàn),寫完模板信息后去編輯變量(Edit variables)
編輯變量

找到相應(yīng)的的方法對(duì)應(yīng)變量名即可。方法名即功能描述表見(jiàn)附件。
我這個(gè)版本有點(diǎn)坑,變量名的default值無(wú)法顯示,所以我直接在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中的函數(shù)方法是讀取當(dāng)前函數(shù)體的屬性,所以只有在該方法內(nèi)使用該命令才能獲取,類沒(méi)有這個(gè)問(wèn)題。
  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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,578評(píng)論 19 139
  • 設(shè)置注釋模板的入口: Window->Preference->Java->Code Style->Code Tem...
    柒黍閱讀 776評(píng)論 0 0
  • =========================================================...
    lavor閱讀 3,649評(píng)論 0 5
  • 記得電影《天下無(wú)賊》里面葛優(yōu)飾演的大佬黎叔有句經(jīng)典的臺(tái)詞: 有組織,無(wú)紀(jì)律! 在傳統(tǒng)社會(huì)里面,無(wú)組織無(wú)紀(jì)律就是一團(tuán)...
    藥山閱讀 1,624評(píng)論 0 51
  • [self.window.layer addAnimation:animation forKey:@"animat...
    iOS小菜鳥(niǎo)閱讀 232評(píng)論 0 3

友情鏈接更多精彩內(nèi)容