IDEA自定义liveTemplates(方法模板、类模板)

IDEA自定义liveTemplates(方法模板、类模板)

前言,搞这个模板有何意义? 降低大家写方法注释的成本,统一风格。有时候不是开发同学不爱写注释,而是没有合适的载体和空间。

IDEA模板设置入口

模板设置入口: Preferences | Editor | Live Templates

一、方法注释快速生成设置

第一步

点击➕号,增加模板,并设置快捷键为'ff',作用域为Java文件

模板内容

/**
 * $description$
 $params$
 * @return {@link $retrun$}
 * @author $author$
 * @date $date$
 *
 */

第二步 配置变量

点击EDIT VARIABLES有弹框
对几个$$的变量进行配置,如\(description\)

其中params特别注意,要配置groovy脚本,才能自动识别方法参数,如下

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

第三步 使用

随意点击某个方法

例如

    public String main(String[] args, String aaa) {
        在此处键入ff后,按tab键,即可生成模板注释,将模板注释复制到方法上,格式化即可
        return "";
    }

结果

    /**
     * main
     *
     * @param args description
     * @param aaa  description
     * @return {@link String}
     * @author starkhu
     * @date 2021/6/23
     */
    public String main(String[] args, String aaa) {

        return "";
    }

其他常用模板

todo的模板,带有生成todo的责任人和时间

// TODO: (所属人:$author$ $date$ $time$) description

常用函数

currentPackage()

Returns the name of the current package (with the class where you expanded the template).

date([format])

Returns the current system date.

By default, without a parameter, it returns the date in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the date("Y-MM-d, E, H:m") returns the date formatted as 2020-02-27, Thu, 16:11.

fileName()

Returns the name of the current file with its extension.

fileNameWithoutExtension()

Returns the name of the current file without its extension.

filePath()

Returns the absolute path to the current file.

fileRelativePath()

Returns the current file path relative to the current project. To check what the relative path is for a given file, right-click it and select Copy Reference, or press ⌥ ⇧ ⌘ C.

groovyScript(, [arg, ...])

Executes the Groovy script passed as a string.

The first argument is a string with either the text of the script or the path to the file that contains the script. The function passes other optional arguments to the script as values for _1, _2, _3, ..., _n variables. Also, you can access the current editor from inside the script using the _editor variable.

methodName()

Returns the name of the method in which the template expands.

methodParameterTypes()

Returns the list of parameter types of the method in which the template expands.

methodParameters()

Returns the list of parameter names of the method in which the template expands.

methodReturnType()

Returns the type of the value returned by the current method in which the template expands.

qualifiedClassName()

Returns the fully qualified name of the current class in which the template expands.

user()

Returns the name of the current user.

团队中,导出/导入自定义liveTemplate

导出Export live template configuration

点击 File | Manage IDE Settings | Export Settings from the menu.

导入Import live template configuration

点击 File | Manage IDE Settings | Import Settings from the menu.

posted @ 2021-06-23 15:03  starmoon1900  阅读(902)  评论(0编辑  收藏  举报