IDEA 利用groovy指令碼生成註釋

Java一哥發表於2024-10-31

  • 介紹

本文主要介紹利用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;

相關文章