全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

浅谈idea live template高级知识_进阶(给方法,类,js方法添加注释)

为了解决用一个命令(宏)给方法,类,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 += 
                 ^
error

问题3:不知道怎么获取Override方法对应的接口方法(父类方法)

关于问题3: 实际上给这种方法加注释是没有意义的,具体可以参考: https://stackoverflow.com/questions/4310858/comments-on-overridden-method-in-java

但是如果非要加该怎么去判断.

以上这篇浅谈idea live template高级知识_进阶(给方法,类,js方法添加注释)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


# idea  # live  # template  # IDEA添加Java类注释模版的方法  # IDEA创建方法时如何快速添加注释  # idea如何添加文档注释  # idea中创建新类时自动添加注释的实现  # 几个  # 给大家  # 种方法  # 子函数  # 为空  # 进阶  # 好了  # 我还  # 那就  # 几天  # 其他的  # 可以通过  # 希望能  # 就把  # 少了  # 这下  # 非要  # 这篇  # 能给  # 该怎么 


相关文章: 企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  php条件判断怎么写_ifelse和switchcase的使用区别【对比】  如何通过主机屋免费建站教程十分钟搭建网站?  如何在云指建站中生成FTP站点?  合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站三合一如何选?哪家性价比更高?  建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略  怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?  网站企业制作流程,用什么语言做企业网站比较好?  如何在景安服务器上快速搭建个人网站?  建站之星免费模板:自助建站系统与智能响应式一键生成  如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本  如何在万网开始建站?分步指南解析  高防服务器如何保障网站安全无虞?  建站之星24小时客服电话如何获取?  建站VPS能否同时实现高效与安全翻墙?  西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?  专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?  免费网站制作模板下载,除了易企秀之外还有什么H5平台可以制作H5长页面,最好是免费的?  如何通过wdcp面板快速创建网站?  怎么制作网站设计模板图片,有电商商品详情页面的免费模板素材网站推荐吗?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  网站制作费用多少钱,一个网站的运营,需要哪些费用?  建站之星3.0如何解决常见操作问题?  网站制作话术技巧,网站推广做的好怎么话术?  太原网站制作公司有哪些,网约车营运证查询官网?  ,怎么在广州志愿者网站注册?  中山网站推广排名,中山信息港登录入口?  如何在景安云服务器上绑定域名并配置虚拟主机?  建站之星上传入口如何快速找到?  建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南  ,购物网站怎么盈利呢?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  上海网站制作开发公司,上海买房比较好的网站有哪些?  深圳网站制作培训,深圳哪些招聘网站比较好?  制作无缝贴图网站有哪些,3dmax无缝贴图怎么调?  大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?  如何通过远程VPS快速搭建个人网站?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  b2c电商网站制作流程,b2c水平综合的电商平台?  青岛网站建设如何选择本地服务器?  长沙企业网站制作哪家好,长沙水业集团官方网站?  电商网站制作价格怎么算,网上拍卖流程以及规则?  高端网站建设与定制开发一站式解决方案 中企动力  c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】  制作营销网站公司,淘特是干什么用的?  c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。