有没有办法在Eclipse(Helios)代码模板中大写变量值的第一个字母

Mic*_*hal 16 eclipse

我有一个带变量的代码模板,我想只在某些情况下将这个变量的值大写(只是第一个字母).有没有办法做到这一点?

模板代码如下 - 我想在我的函数名中大写Property Name ...

private $$${PropertyName};
${cursor}    
public function get${PropertyName}() 
{
  return $$this->${PropertyName};
}

public function set${PropertyName}($$value) 
{
  $$this->${PropertyName} = $$value;
}
Run Code Online (Sandbox Code Playgroud)

请注意:这是一个模板,用于IDE中的代码模板(不是PHP).有关详细信息,请参阅:http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/index.html

Woo*_*III 15

我也想要这个,并试图建立一个自定义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));
    }
}
Run Code Online (Sandbox Code Playgroud)

(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>
Run Code Online (Sandbox Code Playgroud)

我会这样使用:(我在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});
}
Run Code Online (Sandbox Code Playgroud)

从Eclipse 3.5开始,我遇到的问题是,一旦我为property变量指定了值,我的自定义解析器就没有机会重新解析.似乎Java开发工具(Eclipse JDT)通过MultiVariableGuessJavaContext(参见addDependency())中调用的机制来执行此依赖模板变量的重新解析.对我们来说不幸的是,这种机制似乎没有暴露出来,所以我/我们不能用它来做同样的事情(没有大量的复制粘贴或其他冗余工作).

此时,我暂时放弃了一段时间,并将继续将前导小写和前导大写名称分别输入到两个独立的模板变量中.