简体   繁体   中英

How configure Intellij Idea javadoc templates?

I have some method:

public int getSomeField()

I want to generate javadoc like this:

/**
* Gets {someField}
*
* @return value of {someField}
*/

Where {someField} is placeholder for field name. It is possible to create this templates for Intellij Idea (may be as live template)?

Bring up the Generate menu (Cmd+N on Mac, Alt+Insert on Windows)

生成菜单

Click the ellipsis in the top right hand corner.

生成 Getters 菜单

Click the plus in the top left corner and create a new template. 创建新模板对话框

Copy the contents of the old template into the new template and add the following lines to the top of it.

/**
* Gets $field.name
*
* @return value of $field.name
*/

在此处输入图片说明

Now when you generate the getter use the 'New Template' and you should get your getter with the JavaDoc.

选择新模板

在此处输入图片说明

You generate Javadoc by placing the caret above the method. Then you type /** and press Enter .

Unfortunately the template can't be changed and there is a request for that: http://youtrack.jetbrains.net/issue/IDEA-28206

This will get you most of the way there.

File | Settings... | Live Templates

Press the green plus to add a new custom template.

Abbrevation: getter

Template text:

/**
 * Gets $FIELD$
 *
 * @return value of $FIELD$
 */
public $RET$ get$FIELD$()
{
    $END$
}

Applicable in Java: declaration .

Type getter where you would a method and tab complete.

The only shortcoming is I don't know how to make the first character of $FIELD$ capitalized in get$FIELD$ but none of the other locations.

Here is an image for reference:

IntelliJ 模板“吸气剂”

If you want to generate the JavaDoc after the method was written (using /** ), there is currently no way to customize this. Vote for issue IDEA-97658: Edit template for javadoc stub if you'd like to see this implemented.

Surely you can add a live template for such javadoc. The easiest way to do it is to select the whole comment and invoke "Save as live template" action (Tools menu). See http://www.jetbrains.com/phpstorm/webhelp/creating-and-editing-live-templates.html for more details.

我尝试了 methodName() 表达式,但这仅在方法内部有效,您希望该方法在方法之外,在 javadoc 中。

For completeness, to change the setter template in a similar fashion you can add this:

/**
 * Sets $field.name
 *
 * @param $paramName value of $field.name
 */

after the #set($paramName = $helper.getParamName($field, $project)) line.

This will take into account the potential parameter prefix/suffix rules from the project config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM