栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了。

实现的效果如下:

给Java中的method添加方法:

public ResultMsg addMenu(Menu menu){
    ResultMsg result = null;
    return result;
  }

给 Java class 添加注释:


public class MenuController {
}

给js 的方法添加注释


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方法添加注释)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145693.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号