IntelliJ IDEA sets the method annotation template and solves the problem of empty input parameters and return values

## Introduction
There are many articles on the Internet explaining setting method annotation templates. After trying many methods, I found that the template input parameter value and return value generated outside the method are both null, and the input parameter value and return value placed in the method body can be automatically generate. After trying N times, I finally found the trick. This setting of Idea is really not very user-friendly, and it is recommended to improve it. This setting wastes a lot of my time, I share it here, I hope to help you solve the problem. This method is applicable to IntelliJ IDEA 2019.3 x64and later versions (latest version as of the date of publication: 2020.03version) should be available.

1. Open the real-time template and create a new template group

  1. Click in sequence: 【File】-->【settings】-->【Editor】-->【Live Templates】, click the +sign in the upper right corner, and then select Template Group.
  2. In the pop-up box, enter an English name at will: Method Comment(select the name at will), click ok.

insert image description here

2. Create a live template

  1. You must click to select the template group created in the previous step Method Comment, then click the +sign in the upper right corner again to select live Template.

  2. Abbreviationtext box input x.

insert image description here

Defined here is the shortcut key for creating a log. In the line above the method, enter /x+ 回车it, and a comment template will be automatically generated. You can customize the shortcut key according to your own preferences.
The reason why it is set to x, but not used *, is because it is compatible with the block comments that come with IDEA. When we want to use block comments or method comments that come with the system, we still enter the same as before. /*+ 回车Or /**+ 回车, you will find that the original comment template can still be automatically generated and has not been replaced by our custom one. This allows seamless compatibility with system templates. Of course, if you no longer need the system template, it can also be used *as a shortcut key to directly replace the method comments or block comments that come with the system.

** What will happen if it is set to other letters? **
If during coding, there is an attribute in an object that starts with the letter you set, when we enter the object, and then enter .it (for example, if it is set to 's', when you are using the set method, enter: user.s), idea In the first item in the prompt drop-down box, our custom method template prompt will appear. In order to reduce this phenomenon, we use it, because there are fewer words starting with xEnglish letters, the probability of such a situation is relatively low. xWe don't set it *, and part of the reason is that typing *, you have to press shift+8, which itself is not fast. So after what I tried, the most recommended is the xkey. You might be thinking, would 1n't it be better to use the number keys? English words and object properties do not start with numbers, don't they avoid this problem? I also thought of this and tried it. There is indeed no attribute that starts with a number, but when your attribute ends with a number, press Enter, and a method annotation will be automatically generated. So numbers are not very comfortable as shortcut keys.

  1. DescriptionYou can enter a description at will, and it has no effect on the configuration.
  2. Template TextEnter the following template ( 关键步骤,十分重要) in the text box.
**
 * $discription$
 $params$
 $return$
 * @Author: 大脑补丁on $date$ $time$
 */

The first line of the template starts with no /, why define the template like this?
The template is defined in this way because we want to use it in combination with the shortcut keys set/ above as a shortcut key to trigger the template. AbbreviationOnly after inputting the/ shortcut keys in our custom can trigger the annotation of our custom annotation. In this way, when writing code, when inputting , there will be no comment template in the pop-up candidate, which will not interfere with our normal code writing. As shown in the figure below, we enter , the prompt box will not contain the annotation template. The advantage of this is that the custom comment template will not invade normal coding (when you do not need to write comments, you will not be prompted in the pop-up box).Abbreviationxxx
insert image description here

Secondly, defining the template in this way can solve the problem of null input parameters and return values. If it is added at the beginning of the first line /of the template, the generated template may not get the input parameters and return values, and may be displayed as null.

  1. DefineClick the or button at the bottom Change, in the pop-up check box list, check Javaor only check Javatheother

insert image description here

Only check otherthe effect, when writing java code, when declaring the method, enter /x+ 回车(the shortcut key set above), you can pop up the comment template, and it will not pop up in other places, such as in the method body, in the class name /x+ 回车. No template will be generated. If you select all , you can javaenter a template anywhere in the Java code /x+ 回车to generate a template. The usage scenario for generating method annotations is of course only used when the mouse is flickering on the line above the method body; when the mouse is flickering in other positions, we do not need to add method annotations, so it is recommended to only check otherto control this an effect.

  1. Expand withFrom the drop-down box, select Enter.
    After entering the shortcut key /x, press the Enter key again Enterto automatically generate the annotation template. The system default is the Tabkey. After testing, it is found that if this option is not set, pressing Enter Enterwill also take effect.

3. Edit variables

  1. Click on the right Edit variables, refer to the figure below, and enter the following parameters.
    Note: The parameters in the pop-up box are controlled by the parameters in the above Template Text, you can adjust the parameters in the template code.
    insert image description here

Default valueScripts required in the column:

①Script params:

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

②Script return:

groovyScript("def returnType = \"${_1}\"; def result = '* @return ' + returnType; return result;", methodReturnType());
  1. Click okto close the Edit Variable window. Click Applyand okclose the settings window again, and the configuration is complete.

4. How to use

After clicking on the upper line of the method body in the java code, a /xprompt box will pop up for input. The first option xis our custom method annotation template, and then press the Enter key Enterto automatically generate the code template.
insert image description here

The effect of the generated template is as follows, and the return value and parameters can be successfully obtained:
insert image description here

V. Summary

IDEA's template customization function is very powerful, but there are many hidden details in it. I believe this article has explained it thoroughly, especially when setting the template, why set the shortcut key to the letter 'x' instead of using *it, and why enter the generated template into the template. The parameter and return value is null and how to avoid it is clearly written here. And IDEA has been upgrading and iterating, the old version and the new version may be different, this article is based on IntelliJ IDEA 2019.3 x64the version, so when watching various online tutorials, you must pay attention to the comparison version, I hope it will be helpful to you, don’t forget to bookmark it if it is useful like.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324074304&siteId=291194637