为Eclipse定制你自己的注释模板变量

Eclipse的代码注释模板很丰富,如:user、year、date、time等等,请参考附件。
尽管模板变量如此之多,但是对于复杂多边的实际项目而言,还是不够用,怎么办?自己增加新的呗!
Eclipse配置界面都不支持定制自己的注释模板变量,没办法,改Eclipse源码,
具体怎么改,好像无头苍蝇,百度和谷歌了2天,还是无果,在边搜边摸索的过程中才知道需要修改如下两个架包对应的源码,
org.eclipse.jdt.ui.jar
org.eclipse.text.jar
发现了三个重要的文件:
org.eclipse.jface.text.template.TextTemplateMessageg.properties
org.eclipse.jface.text.templates.GlobalTemplateVariables.java
org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType.java

1、TextTemplateMessageg.properties里面定义了一些全局模板:

# global variables
GlobalVariables.variable.description.cursor=The cursor position after editing template variables
GlobalVariables.variable.description.dollar=The dollar symbol
GlobalVariables.variable.description.date=Current date
GlobalVariables.variable.description.year=Current year
GlobalVariables.variable.description.time=Current time
GlobalVariables.variable.description.user=User name
GlobalVariables.variable.description.selectedWord= The selected word
GlobalVariables.variable.description.selectedLines= The selected lines
我们可以看到user、year、date、time都在里面。
如果要增加自己的模板变量,就加吧?例如:
GlobalVariables.variable.description.copyright=Copyright all resolved

2、全局模板变量在GlobalTemplateVariables里面,均继承自SimpleTemplateVariableResolver
增加一个对应GlobalVariables.variable.description.copyright的模板变量
/**
* The copyright variable evaluates to the current copyright.
* Added by 博主 2011-10-30
*/
public static class Copyright extends SimpleTemplateVariableResolver
{
/**
* 默认从环境变量中取copyright信息,环境变量可以定义在eclipse.ini的vmargs下面
*/
private static String value = System.getProperty("copyright");
/**
* Creates a new copyright variable
*/
public Copyright()
{
super("copyright", TextTemplateMessages.getString("GlobalVariables.variable.description.copyright")); //$NON-NLS-1$ //$NON-NLS-2$
}

/**
* {@inheritDoc}
*/
protected String resolve(TemplateContext context)
{
if (value != null)
{
return TextTemplateMessages.getString("GlobalVariables.variable.description.copyright");
}
return value; //$NON-NLS-1$
}
}

原文地址:https://www.cnblogs.com/bjanzhuo/p/3575969.html