代码之家  ›  专栏  ›  技术社区  ›  IAdapter

如何在eclipse插件中将参数传递到代码模板中

  •  7
  • IAdapter  · 技术社区  · 16 年前

    like this blog post ). 如何将参数传递到模板中?喜欢 ${name:param} ?

    2 回复  |  直到 14 年前
        1
  •  6
  •   Community Mohan Dere    9 年前

    ${word_selection} 包含当前所选内容。

    但许多人缺少的是,你可以定义自己的变量:

    private static final ${type} ${name} = new ${type} (${cursor});
    

    也不 ${type} 也没有 ${name} 在单击“插入变量…”按钮时得到的列表中。Eclipse注意到并允许您使用 标签 并且它将保持这些自定义“模板字段”的内容同步(因此您可以在更新部件之后 new

    See this answer for other useful Eclipse templates .

        2
  •  2
  •   SomeInternetGuy    6 年前

    你所要做的就是传递你的参数,然后你就可以使用它们了。

    假设我们想要创建一个TemplateVariableResolver,它将传递参数的第一个字母大写。

    您将首先按如下方式填充plugin.xml:

    <extension point="org.eclipse.ui.editors.templates">
        <resolver class="org.eclipse.ui.templates.UppercaseResolver"
            contextTypeId="java"
            description="${Uppercase(word[, word...])}  uppercase's the provided words"
            name="Uppercase words" type="Uppercase"/>
    </extension>
    

    public void resolve(TemplateVariable variable, TemplateContext context) {
        if (variable.getVariableType().getParams().size() > 0) {
            StringBuffer result = new StringBuffer();
            for(String value : (List<String>) variable.getVariableType().getParams()) {
                value = value.substring(0,1).toUpperCase() + value.substring(1);
                result.append(value);
            }
            variable.setValue(result.toString());
        }
    }
    

    最后,在代码模板中:

    String name = ${Uppercase(jim,laughlin)};