Java 代码模板的 Eclipse 自定义变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/350600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:26:07  来源:igfitidea点击:

Eclipse Custom Variable for Java Code Templates

javaeclipsetemplates

提问by Paul Croarkin

How do you add a new variable to be inserted into a Java code template. How do I add a variable to the list in Window->Preferences->Java->Code Style->Code Templates->Code->New Java Files->Edit->Insert Variable... ?

如何添加要插入到 Java 代码模板中的新变量。如何在 Window->Preferences->Java->Code Style->Code Templates->Code->New Java Files->Edit->Insert Variable... 中的列表中添加变量?

Currently my new files get created with:

目前我的新文件是通过以下方式创建的:

${filecomment}
${package_declaration}
${typecomment}
${type_declaration}

I'd like them to get created with something like:

我希望它们能够通过以下方式创建:

${begin_filecomment}
${package_declaration}
${typecomment}
${type_declaration}
${end_filecomment}

where begin_filecomment and end_filecomment appear in the Insert Variable list.

其中 begin_filecomment 和 end_filecomment 出现在插入变量列表中。

采纳答案by matt b

I'm pretty sure that the list of "variables" is generated by Eclipse and there is no way to add a new template variable.

我很确定“变量”列表是由 Eclipse 生成的,并且无法添加新的模板变量。

What do you want ${begin_filecomment}and ${end_filecomment}to be? Just type the content into the Edit box - there is nothing that says you cannot put static content in there.

你想要什么${begin_filecomment},并${end_filecomment}要?只需在编辑框中输入内容 - 没有任何内容表明您不能将静态内容放入其中。

回答by Paul Croarkin

Yes, you can indeed add a variable to this list. See the extension point called

是的,您确实可以在此列表中添加一个变量。查看名为的扩展点

org.eclipse.ui.editors.templates

org.eclipse.ui.editors.templates

and you'll find out how.

你会知道怎么做的。

回答by Rich Seller

To contribute a new variable, you need to create a plugin and implement the org.eclipse.ui.editors.templates extension-point.

要贡献一个新变量,您需要创建一个插件并实现 org.eclipse.ui.editors.templates 扩展点。

You have to contribute a sub-class of org.eclipse.jface.text.templates.TemplateVariableResolver that will implement the various resolve methods to resolve the placeholder to your desired value(s).

您必须贡献一个 org.eclipse.jface.text.templates.TemplateVariableResolver 的子类,它将实现各种解析方法以将占位符解析为您想要的值。

See herefor help on the extension point and an example that contributes the ant variables.

有关扩展点的帮助和提供 ant 变量的示例,请参见此处

As matt b says, you often just need to define your text as boilerplate, so implementing a variable for that is overkill.

正如 matt b 所说,您通常只需要将文本定义为样板,因此为此实现一个变量是多余的。

回答by Hosam Aly

I didn't try it, but maybe you could edit the template files in the JDT jar:

我没有尝试过,但也许您可以编辑 JDT jar 中的模板文件:

eclipse\plugins\org.eclipse.jdt.ui_*.jar\templates\

回答by javaBean007

To add on to the conversation, I also wanted to insert a custom variable into a template. My issue was that I wanted to insert a variable from jsp/jstl definition inside the template.

为了添加到对话中,我还想在模板中插入一个自定义变量。我的问题是我想在模板中插入来自 jsp/jstl 定义的变量。

(I.E. <c:set var="myVariable" value="${requestScope.variableName}" />)

(IE <c:set var="myVariable" value="${requestScope.variableName}" />)

This apparently caused some issues because templates already use the ${}syntax to help resolve its own variables.

这显然引起了一些问题,因为模板已经使用${}语法来帮助解决它自己的变量。

So simple solution I included an additional $to the pattern in the template. Two $$signs resolves it to a single $sign, hence giving me exactly what I wanted. Hopefully this helps the poster or someone with similar situation.

如此简单的解决方案我$在模板中包含了一个额外的模式。两个$$符号将其解析为一个$符号,因此正是我想要的。希望这对海报或有类似情况的人有所帮助。