IDEA 使用groovy脚本天生注释

王柳  金牌会员 | 2024-10-31 13:53:11 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

 

  • 介绍
 
本文主要介绍使用IDEA编辑器的活动模板,结合groovy脚本,天生注释模板。现在天生的注释主要适用于java和js文件
推荐此方式,可以根据需要界说groovy脚本,天生不同格式的注释

  • 操作
  • 点击 File >> Setting 
找到 Edtor >> Live Temolates , 发起创建一个模板组 java_often(非必须,创建一个好管理)

 
 

  • 再次点击+号,创建一个 Live Temolate
缩写Abbreviation处填写星号 * 
模板文本填写  **$context$ */  , 留意星号顶头填写,前面没有空格,变量context可自界说

 

  •  点击 Edit Variables
Name处填写 context ,留意对应上一步的变量
Expression处不可选择
Default value填写 
  1. 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脚本文件
  1. def packageName = _1;
  2. def className = _2;
  3. def methodName = _3;
  4. def jsMethodName = _4;
  5. def methodParameters = _5;
  6. def methodReturnType = _6;
  7. def date = _7;
  8. def time = _8;
  9. def confirmedDateTimeString = "2022-03-01 12:20:40";
  10. // 可以修改成自己的相关信息
  11. def author = "mutouyang";
  12. def projectName = "demoProject";
  13. def version = "V1.0.0";
  14. def copyright = "2017 mutouyang Inc. All rights reserved.";
  15. if(packageName){
  16.     String[] str = packageName.split("\\.")
  17.     if(str.length >2){
  18.         projectName=str[1];
  19.     }
  20. }
  21. def outputParams = "";
  22. for(param in methodParameters) {
  23.     outputParams += " * @param " + param + " : \n";
  24. }
  25. def outputReturnType = "";
  26. if (methodReturnType != null & methodReturnType != "void") {
  27.     outputReturnType = " * @return : " + methodReturnType + "\n";
  28. }
  29. def outputMethodName         = " * method : " + methodName + "</br>\n";
  30. def outputDesc               = " * description :     </br>\n";
  31. if(!outputReturnType){
  32.     //outputDesc             = " * description : 构造方法 </br>\n";
  33. }
  34. def outputAuthor             = " * @author : " + author + "</br>\n";
  35. def outputDateTime           = " * @CreateDate : " + date + " " + time + "</br>\n";
  36. def outputProjectName        = " * @Project : " + projectName +"</br>\n";
  37. def outputPackageName        = " * @Package : " + packageName + "</br>\n";
  38. def outputClass              = " * @ClassName : " + className + "</br>\n";
  39. def outputVersion            = " * @version : " + version + "</br>\n";
  40. def outputClassOtherInfo     = " * @Copyright : " + copyright + "</br>\n";
  41. outputClassOtherInfo        += " * @Reviewed : " + "\n";
  42. outputClassOtherInfo        += " * @UpateLog :    Name    Date    Reason/Contents\n";
  43. outputClassOtherInfo        += " *             ---------------------------------------\n";
  44. outputClassOtherInfo        += " *                ****    ****    **** \n";
  45. def outputJsMethodName       = " * method : " + jsMethodName + "</br>\n";
  46. def outputJsParams           = " * @param </br>\n";
  47. def outputJsReturnType       = " * @return : </br>\n";
  48. def result = "";
  49. result += "\n";
  50. if (methodName) {
  51.     result += outputMethodName;
  52.     result += outputDesc;
  53.     result += outputParams;
  54.     result += outputReturnType;
  55.     result += outputAuthor;
  56.     result += outputDateTime;
  57. } else if (jsMethodName) {
  58.     result += outputJsMethodName;
  59.     result += outputDesc;
  60.     result += outputJsParams;
  61.     result += outputJsReturnType;
  62.     result += outputAuthor;
  63.     result += outputDateTime;
  64. } else {
  65.     result += outputProjectName;
  66. //  result += outputPackageName;
  67.     result += outputClass;
  68.     result += outputDesc;
  69.     result += outputAuthor;
  70.     result += outputDateTime;
  71.     result += outputVersion;
  72. //  result += outputClassOtherInfo;
  73. }
  74. //result += " *";
  75. return result;
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王柳

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表