Detailed Explanation of IntelliJ IDEA Annotation Templates

A code farmer who converts Idea from Eclipse needs to set some comments during the process of using IntelliJ IDEA. In fact, there are many articles on the Internet that are similar to idea template comments, but the author will encounter some more or less in the process of using it. The situation is inexplicable, but many existing articles have not given similar solutions, so the author will summarize some of my own experience during the use process, hoping to give readers and friends some help, if there is something wrong, I hope you criticize Correct, learn together!

First, I will show you the effect icon of the template used by the author, and then roughly explain the configuration process according to the icon.

1. First, let's set the template of the class in IDEA: (IDEA will automatically add comments when creating the class)

File-->settings-->Editor-->File and Code Templates-->Files (Idea default shortcut key is ctrl+alt+s)

Class file (of course you can also choose Interface file if you want to set the interface) comment setting

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} !="")package ${PACKAGE_NAME};#end
 
/**
* All rights Reserved, Designed By www.super-master.cn
* @ProjectName: ${PROJECT_NAME}
* @Package: ${PACKAGE_NAME}
* @ClassName: ${NAME}
* @Description: []
* <p> </p>
* @Author: [Lance Ting]
* @Date: ${DATE} ${TIME}
* @Version: V1.0
* @Copyright: ${YEAR} www.super-master.cn Inc. All rights reserved. 
* TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
**/
public class/interface/enum ${NAME} {
 
}

As for @attributes, this is based on your own needs, you don't need to follow the author's instructions!

The effect icon is

Note: If you add #if (${PACKAGE_NAME} && ${PACKAGE_NAME} !="")package ${PACKAGE_NAME};#end, then add #parse("File Header.java") must be put behind,

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} !="")package ${PACKAGE_NAME};#end
#parse("File Header.java")

Otherwise, when creating the class, two package names will be automatically imported (the author has never known what the problem is, so you need to pay special attention to this point), of course, you don’t need to add #parse("File Header.java"), generally The author will add annotation templates to Class, Interface, and Enum (this is based on your needs).

 

Two, setting method annotation template

1、设置Live Templates,File-->Settings-->Editor-->Live Templates

1.1 Add Template Group

1.2 Select the added Template Group (common templates), click +, and add [1.Live Template]

Template text

*
 * @Description: [$description$]
 * <p></P>
 * @Title: $title$
 * @Author: Lance Ting
 * @Date: $date$
 * @Param: $params$
 * @Return: $return$
 * @Throws: $throws$
 */

1.3. Modify the template attribute display mode Edit variables, so that you can beautify the method comment display, and params are displayed as an array by default

What needs special attention is

params

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

result

groovyScript("def returnType = \"${_1}\"; def result =  returnType; return result;", methodReturnType());

Save the settings, the display effect icon is

Special Note

1. Idea's default way of generating comments is /*+template name+shortcut key when the comment template is /*** + enter key

2. The first line of the Template text option box value * needs to be placed on the first line, otherwise the comment is displayed as /* * and grayed out

display effect

Therefore, be sure to put the first row and the second row by one column! !

OK! At this point, the basic annotation configuration has been completed!

 

-------------------------------------------------- ----------------------
author: World coding
source: CSDN
original: https: //blog.csdn.net/dgxin_605/article/details/107616635
copyright notice : This article is the original article of the blogger, please attach a link to the blog post if you reprint it!

------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/dgxin_605/article/details/107616635