【问题标题】:Eclipse Custom Variable for Java Code TemplatesJava 代码模板的 Eclipse 自定义变量
【发布时间】:2008-12-08 19:38:08
【问题描述】:

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

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

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

我希望它们被创建为:

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

begin_filecomment 和 end_filecomment 出现在插入变量列表中的位置。

【问题讨论】:

    标签: java eclipse templates


    【解决方案1】:

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

    的扩展点

    org.eclipse.ui.editors.templates

    你会知道怎么做的。

    【讨论】:

    • 这似乎只适用于Window->Preferences->Java->Editor->Templates,不适用于Window->Preferences->Java->Code Style->Code Templates 模板。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      请参阅here 以获取有关扩展点的帮助以及提供 ant 变量的示例。

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

      【讨论】:

      • 这似乎只适用于Window->Preferences->Java->Editor->Templates,不适用于Window->Preferences->Java->Code Style->Code Templates 模板。
      【解决方案4】:
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:
        【解决方案7】:

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

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

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

        如此简单的解决方案我在模板中的模式中添加了一个额外的$。两个$$ 符号将其解析为一个$ 符号,因此给了我想要的东西。希望这对发帖者或有类似情况的人有所帮助。

        【讨论】:

          猜你喜欢
          • 2011-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-29
          相关资源
          最近更新 更多