首页 > 基础资料 博客日记

IDEA 利用groovy脚本生成注释

2024-10-31 15:00:03基础资料围观71

文章IDEA 利用groovy脚本生成注释分享给大家,欢迎收藏Java资料网,专注分享技术知识

 

  • 介绍

 

本文主要介绍利用IDEA编辑器的活动模板,结合groovy脚本,生成注释模板。目前生成的注释主要适用于java和js文件

推荐此方式,可以根据需要定义groovy脚本,生成不同格式的注释

  • 操作

  • 点击 File >> Setting 

找到 Edtor >> Live Temolates , 建议创建一个模板组 java_often(非必须,创建一个好管理)

 

 

  • 再次点击+号,创建一个 Live Temolate

缩写Abbreviation处填写星号 * 

模板文本填写  **$context$ */  , 注意星号顶头填写,前面没有空格,变量context可自定义

 

  •  点击 Edit Variables

Name处填写 context ,注意对应上一步的变量

Expression处不可选择

Default value填写 

groovyScript("D:\\idea\\javadoc.groovy", currentPackage(), className(), methodName(), jsMethodName(), methodParameters(), methodReturnType(), date("yyyy-MM-dd"), time("HH:mm:ss"))

由于需要的groovy脚本过长,所以创建了一个文件,这个文件建议跟idea安装目录放一起,文件内容放到了文章结尾

 

  • 最后

设置触发键 Enter 。

选择适用文件 java,

 

  • 展示

在Java类名输入 /* ,点击回车,即可生产注释,

 

 

 

  • 附录

groovy脚本文件

def packageName = _1;
def className = _2;
def methodName = _3;
def jsMethodName = _4;
def methodParameters = _5;
def methodReturnType = _6;
def date = _7;
def time = _8;

def confirmedDateTimeString = "2022-03-01 12:20:40";
// 可以修改成自己的相关信息
def author = "mutouyang";
def projectName = "demoProject";
def version = "V1.0.0";
def copyright = "2017 mutouyang Inc. All rights reserved.";

if(packageName){
    String[] str = packageName.split("\\.")
    if(str.length >2){
        projectName=str[1];
    }
}

def outputParams = "";
for(param in methodParameters) {
    outputParams += " * @param " + param + " : \n";
}
def outputReturnType = "";
if (methodReturnType != null & methodReturnType != "void") {
    outputReturnType = " * @return : " + methodReturnType + "\n";
}

def outputMethodName         = " * method : " + methodName + "</br>\n";
def outputDesc               = " * description :     </br>\n";
if(!outputReturnType){
    //outputDesc             = " * description : 构造方法 </br>\n";
}
def outputAuthor             = " * @author : " + author + "</br>\n";
def outputDateTime           = " * @CreateDate : " + date + " " + time + "</br>\n";
def outputProjectName        = " * @Project : " + projectName +"</br>\n";
def outputPackageName        = " * @Package : " + packageName + "</br>\n";
def outputClass              = " * @ClassName : " + className + "</br>\n";
def outputVersion            = " * @version : " + version + "</br>\n";
def outputClassOtherInfo     = " * @Copyright : " + copyright + "</br>\n";
outputClassOtherInfo        += " * @Reviewed : " + "\n";
outputClassOtherInfo        += " * @UpateLog :    Name    Date    Reason/Contents\n";
outputClassOtherInfo        += " *             ---------------------------------------\n";
outputClassOtherInfo        += " *                ****    ****    **** \n";
def outputJsMethodName       = " * method : " + jsMethodName + "</br>\n";
def outputJsParams           = " * @param </br>\n";
def outputJsReturnType       = " * @return : </br>\n";

def result = "";

result += "\n";
if (methodName) {
    result += outputMethodName;
    result += outputDesc;
    result += outputParams;
    result += outputReturnType;
    result += outputAuthor;
    result += outputDateTime;
} else if (jsMethodName) {
    result += outputJsMethodName;
    result += outputDesc;
    result += outputJsParams;
    result += outputJsReturnType;
    result += outputAuthor;
    result += outputDateTime;
} else {
    result += outputProjectName;
//  result += outputPackageName;
    result += outputClass;
    result += outputDesc;
    result += outputAuthor;
    result += outputDateTime;
    result += outputVersion;
//  result += outputClassOtherInfo;
}
//result += " *";
return result;

 


文章来源:https://www.cnblogs.com/mutouyang/p/18517639
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云