我也想要这个,并尝试构建一个自定义 TemplateVariableResolver 来做到这一点。 (我已经有了一个自定义解析器,可以生成新的 UUID,例如 http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/。)
我做了一个绑定到capitalize的自定义解析器:
public class CapitalizingVariableResolver extends TemplateVariableResolver {
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
@SuppressWarnings("unchecked")
final List<String> params = variable.getVariableType().getParams();
if (params.isEmpty())
return;
final String currentValue = context.getVariable(params.get(0));
if (currentValue == null || currentValue.length() == 0)
return;
variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
}
}
(plugin.xml:)
<extension point="org.eclipse.ui.editors.templates">
<resolver
class="com.foo.CapitalizingVariableResolver"
contextTypeId="java"
description="Resolves to the value of the variable named by the first argument, but with its first letter capitalized."
name="capitalized"
type="capitalize">
</resolver>
</extension>
我会这样使用:(我在 Java 中工作;我发现您似乎不是)
public PropertyAccessor<${propertyType}> ${property:field}() {
return ${property};
}
public ${propertyType} get${capitalizedProperty:capitalize(property)}() {
return ${property}.get();
}
public void set${capitalizedProperty}(${propertyType} ${property}) {
this.${property}.set(${property});
}
从 Eclipse 3.5 开始,我遇到的问题是,一旦我为 property 变量指定了值,我的自定义解析器就没有机会重新解析。 Java 开发工具 (Eclipse JDT) 似乎通过 JavaContext 中称为 MultiVariableGuess 的机制重新解析了这个依赖模板变量(请参阅 addDependency())。对我们来说不幸的是,该机制似乎没有暴露出来,所以我/我们不能用它来做同样的事情(没有大量的复制和粘贴或其他多余的工作)。
在这一点上,我再次放弃一段时间,并将继续将前导小写和前导大写名称分别输入到两个独立的模板变量中。