idea live template進階知識, 進階(給方法,類,js方法添加註釋)

來源:互聯網
上載者:User

標籤:注釋   log   variable   iat   over   art   dshow   eal   com   

為瞭解決用一個命令(宏)給方法,類,js方法添加註釋,經過幾天的研究.終於得到結果了.

實現的效果如下:

給Java中的method添加方法:

    /**     *     * @Method : addMenu     * @Description :     * @param menu :     * @return : cn.yiyizuche.common.base.ResultMsg     * @author : Rush.D.Xzj     * @CreateDate : 2017-06-12 星期一 18:17:42     *     */public ResultMsg addMenu(Menu menu){        ResultMsg result = null;        return result;    }

給 Java class 添加註釋:

/** * * @Project : 壹壹OA * @Package : cn.yiyizuche.common.ou.menu.controller * @Class : MenuController * @Description : * @author : Rush.D.Xzj * @CreateDate : 2017-06-12 星期一 18:15:32 * @version : V1.0.0 * @Copyright : 2017 yizukeji Inc. All rights reserved. * @Reviewed : * @UpateLog :    Name    Date    Reason/Contents *             --------------------------------------- *                 ***        ****    **** * */public class MenuController {}

給js 的方法添加註釋

/** * * @Method : standardShowBatchCheckBox * @Description : * @return : * @author : Rush.D.Xzj * @CreateDate : 2017-06-12 星期一 18:15:21 * */function standardShowBatchCheckBox(jspElement, dataList, max, valueIdFunction, textFunction, selectedFunction) {}

 

 

現在把答案公布如下

Abbreviation:

cmj

Template text(注1):

** *$context$  */

Edit variables:

$context$的代碼(主要代碼)如下:

groovyScript("def methodName = \"${_1}\"; def jsMethodName = \"${_2}\"; def outputMethodName = \"${_3}\"; def outputDesc = \"${_4}\"; def outputParams = \"${_5}\"; def outputAuthor = \"${_6}\"; def outputReturnType = \"${_7}\"; def outputDateTime = \"${_8}\"; def outputPackage = \"${_9}\"; def outputClass = \"${_10}\"; def outputClassOtherInfo = \"${_11}\"; def outputProject = \"${_12}\"; def outputVersion = \"${_13}\"; def outputJsMethodName = \"${_14}\"; def outputJsReturnType = \"${_15}\"; def result = ‘‘; if (methodName != ‘null‘) { result += ‘\\n‘; result += outputMethodName; result += outputDesc; result += outputParams; result += outputReturnType; result += outputAuthor; result += outputDateTime; result += ‘ *‘; return result;} else if (jsMethodName != ‘null‘) { result += ‘\\n‘; result += outputJsMethodName; result += outputDesc; result += outputJsReturnType; result += outputAuthor; result += outputDateTime; result += ‘ *‘; return result;} else { result += ‘\\n‘; result += outputProject; result += outputPackage; result += outputClass; result += outputDesc; result += outputAuthor; result += outputDateTime; result += outputVersion; result += outputClassOtherInfo; result += ‘ *‘; return result;} ", methodName(), jsMethodName(), groovyScript("def methodName = \"${_1}\"; def result = ‘ * @Method : ‘ + methodName + ‘\\n‘; return result;", methodName()), groovyScript("def result = ‘ * @Description : ‘ + ‘\\n‘; return result;"), groovyScript("if(\"${_1}\".length() == 2) {return ‘‘;} else {def result=‘‘; def params=\"${_1}\".replaceAll(‘[\\\\[|\\\\]|\\\\s]‘, ‘‘).split(‘,‘).toList(); for(i = 0; i < params.size(); i++) {result+=‘ * @param ‘ + params[i] + ‘ : ‘ + ‘\\n‘}; return result;}", methodParameters()), groovyScript("def author = ‘Rush.D.Xzj‘; def result = ‘ * @author : ‘ + author + ‘\\n‘; return result;"), groovyScript("def returnType = \"${_1}\"; def result = ‘ * @return : ‘ + returnType + ‘\\n‘; return result;", methodReturnType()), groovyScript("def date = \"${_1}\"; def time = \"${_2}\"; def result = ‘ * @CreateDate : ‘ + date + ‘ ‘ + time + ‘\\n‘; return result;", date("yyyy-MM-dd E"), time("HH:mm:ss")), groovyScript("def currentPackage = \"${_1}\"; def result = ‘ * @Package : ‘ + currentPackage + ‘\\n‘; return result;", currentPackage()), groovyScript("def className = \"${_1}\"; def result = ‘ * @Class : ‘ + className + ‘\\n‘; return result;", className()), groovyScript("def result = ‘ * @Copyright : 2017 yizukeji Inc. All rights reserved.‘ + ‘\\n‘; result += ‘ * @Reviewed : ‘ + ‘\\n‘; result += ‘ * @UpateLog :    Name    Date    Reason/Contents‘ + ‘\\n‘; result += ‘ *             ---------------------------------------‘ + ‘\\n‘; result += ‘ *                 ***        ****    **** ‘ + ‘\\n‘; return result;"), groovyScript("def result = ‘ * @Project : 壹壹OA‘ + ‘\\n‘; return result;"), groovyScript("def result = ‘ * @version : V1.0.0‘ + ‘\\n‘; return result;"), groovyScript("def jsMethodName = \"${_1}\"; def result = ‘ * @Method : ‘ + jsMethodName + ‘\\n‘; return result;", jsMethodName()), groovyScript("def result = ‘ * @return : ‘ + ‘\\n‘; return result;"))

使用如下:

在xx.java或者 xx.js中輸出(注2):

/cmj

後 按 tab鍵(此鍵是預設的,可以更改成其他的)

注1 和注2

也可以換成第二種方法(網上大部分的方法):

Template text

* *$context$  */

跟注1比較第一行少了一個*,

因此注2就是變成了:

/*cmj

我感覺用我的方法比第二種方法好.

 

 

Edit Variable中代碼詳解


首先我分解了如下的 10幾個函數(是小函數):

    // 輸出方法名    groovyScript("def methodName = \"${_1}\"; def result = ‘ * @Method : ‘ + methodName + ‘\\n‘; return result;", methodName())        // 輸出描述    groovyScript("def result = ‘ * @Description : ‘ + ‘\\n‘; return result;")        // 輸出參數列表的子函數    groovyScript("if(\"${_1}\".length() == 2) {return ‘‘;} else {def result=‘‘; def params=\"${_1}\".replaceAll(‘[\\\\[|\\\\]|\\\\s]‘, ‘‘).split(‘,‘).toList(); for(i = 0; i < params.size(); i++) {result+=‘ * @param ‘ + params[i] + ‘ : ‘ + ‘\\n‘}; return result;}", methodParameters())        // 輸出    作者的        子函數  這裡可以改作者    groovyScript("def author = ‘Rush.D.Xzj‘; def result = ‘ * @author : ‘ + author + ‘\\n‘; return result;")        // 輸出    傳回值的   子函數    groovyScript("def returnType = \"${_1}\"; def result = ‘ * @return : ‘ + returnType + ‘\\n‘; return result;", methodReturnType())        // 輸出    日期的     字函數, 這個可以更改日期格式    groovyScript("def date = \"${_1}\"; def time = \"${_2}\"; def result = ‘ * @CreateDate : ‘ + date + ‘ ‘ + time + ‘\\n‘; return result;", date("yyyy-MM-dd E"), time("HH:mm:ss"))    // 輸出Project    groovyScript("def result = ‘ * @Project : 壹壹OA‘ + ‘\\n‘; return result;")        // 輸出package    groovyScript("def currentPackage = \"${_1}\"; def result = ‘ * @Package : ‘ + currentPackage + ‘\\n‘; return result;", currentPackage())        // 輸出class    groovyScript("def className = \"${_1}\"; def result = ‘ * @Class : ‘ + className + ‘\\n‘; return result;", className())        // 輸出copyright/reviewd/updatelog    groovyScript("def result = ‘ * @Copyright : 2017 yizukeji Inc. All rights reserved.‘ + ‘\\n‘; result += ‘ * @Reviewed : ‘ + ‘\\n‘; result += ‘ * @UpateLog :    Name    Date    Reason/Contents‘ + ‘\\n‘; result += ‘ *             ---------------------------------------‘ + ‘\\n‘; result += ‘ *                 ***        ****    **** ‘ + ‘\\n‘; return result;")        // 輸出version    groovyScript("def result = ‘ * @version : V1.0.0‘ + ‘\\n‘; return result;")    // 輸出jsMethodName    groovyScript("def jsMethodName = \"${_1}\"; def result = ‘ * @Method : ‘ + jsMethodName + ‘\\n‘; return result;", jsMethodName())        // 輸出 js 的return    groovyScript("def result = ‘ * @return : ‘ + ‘\\n‘; return result;")

 

然後在主要代碼中,需要把上述的10幾個函數當做參數供給主要代碼使用.

判斷是函數,類,js函數主要是通過如下的2個內建變數來實現的:

methodName()jsMethodName()

當methodName()不為空白的時候, 產生 方法的 注釋

當jsMethodName()不為空白的時候, 產生 js方法的注釋

否則產生類的注釋

所以上述的代碼可以簡單的做如下的歸類了:

擷取相關參數:

    def methodName = \"${_1}\";     def jsMethodName = \"${_2}\";     def outputMethodName = \"${_3}\";     def outputDesc = \"${_4}\";     def outputParams = \"${_5}\";     def outputAuthor = \"${_6}\";     def outputReturnType = \"${_7}\";     def outputDateTime = \"${_8}\";     def outputPackage = \"${_9}\";     def outputClass = \"${_10}\";     def outputClassOtherInfo = \"${_11}\";     def outputProject = \"${_12}\";     def outputVersion = \"${_13}\";     def outputJsMethodName = \"${_14}\";     def outputJsReturnType = \"${_15}\";     def result = ‘‘;

判斷是哪一種類型的注釋(程式碼片段2):

   if (methodName != ‘null‘) {        result += ‘\\n‘;        result += outputMethodName;        result += outputDesc;        result += outputParams;        result += outputReturnType;        result += outputAuthor;        result += outputDateTime;        result += ‘ *‘;        return result;    } else if (jsMethodName != ‘null‘) {        result += ‘\\n‘;        result += outputJsMethodName;        result += outputDesc;        result += outputJsReturnType;        result += outputAuthor;        result += outputDateTime;        result += ‘ *‘;        return result;    } else {        result += ‘\\n‘;        result += outputProject;        result += outputPackage;        result += outputClass;        result += outputDesc;        result += outputAuthor;        result += outputDateTime;        result += outputVersion;        result += outputClassOtherInfo;        result += ‘ *‘;        return result;    }

這下就可以了.就把整個這麼難看(groopscript搞的?)代碼整理的比較清晰了.

 

我還有如下的幾個疑惑/問題需要解決, 如果有朋友能給我答案那就好了.

問題1. 不知道可以通過什麼方法擷取js方法中的參數列表

問題2. 程式碼片段2中如果我改成了:

    result += ‘\\n‘;    if (methodName != ‘null‘) {        result += outputMethodName;        result += outputDesc;        result += outputParams;        result += outputReturnType;        result += outputAuthor;        result += outputDateTime;    } else if (jsMethodName != ‘null‘) {        result += ‘\\n‘;        result += outputJsMethodName;        result += outputDesc;        result += outputJsReturnType;        result += outputAuthor;        result += outputDateTime;    } else {        result += ‘\\n‘;        result += outputProject;        result += outputPackage;        result += outputClass;        result += outputDesc;        result += outputAuthor;        result += outputDateTime;        result += outputVersion;        result += outputClassOtherInfo;    }    result += ‘ *‘;    return result;

會出現如下的錯誤:

startup failed:Script1.groovy: 1: expecting EOF, found ‘result‘ @ line 1, column 1036.   lt += outputClassOtherInfo; } result +=                                  ^1 error

問題3: 不知道怎麼擷取Override方法對應的介面方法(父類方法)

關於問題3: 實際上給這種方法加註釋是沒有意義的,具體可以參考: https://stackoverflow.com/questions/4310858/comments-on-overridden-method-in-java

但是如果非要加該怎麼去判斷.

 

參考資料:

http://blog.sina.com.cn/s/blog_4c4195e70102wh7e.html

https://stackoverflow.com/questions/4310858/comments-on-overridden-method-in-java

 

 

idea live template進階知識, 進階(給方法,類,js方法添加註釋)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.