有没有办法在Eclipse(Helios)代码模板中大写一个变量值的第一个字母 [英] Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates

查看:186
本文介绍了有没有办法在Eclipse(Helios)代码模板中大写一个变量值的第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有变量的代码模板,我想在某些情况下大写(仅第一个字母)该变量的值。有没有办法这样做?



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

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

public function set $ {PropertyName}($$ value)
{
$$ this-> $ {PropertyName} = $$ value;
}

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

解决方案

也想要这个,并试图构建一个自定义的 TemplateVariableResolver 来做到这一点。 (我已经有一个自定义解析器生成新的UUID a http: //dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/ 。)



我做了一个自定义解析器绑定到大写

  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 =解析由第一个参数命名的变量的值,但其第一个字母大写。
name =大写
type =大小写>
< / resolver>
< / extension>

我将使用这样:(我在Java工作;我看到你没有出现to be)

  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,我遇到的问题是我的自定义解析器一旦指定了属性变量的值,就无法重新解析。看来,Java开发工具(Eclipse JDT)通过一个名为 MultiVariableGuess 的机制,在 JavaContext (见 addDependency())。不幸的是,我们似乎没有暴露这种机制,所以我/我们不能使用它来做同样的事情(没有大量的复制和粘贴或其他冗余工作)。



在这一点上,我放弃了一段时间,并将继续输入领先的小写和大写字母名称,分成两个独立的模板变量。


I have a code template with a variable and I would like to capitalize(just the first letter) the value of this variable only in some occurrences. Is there a way to do this?

The template code is as follows - I would like to capitalize Property Name in my function names...

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

public function set${PropertyName}($$value) 
{
  $$this->${PropertyName} = $$value;
}

Please Note: This is a template for use with code templates in the IDE (not in PHP). For details see: http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-code-templates/index.html

解决方案

I also want this and tried to build a custom TemplateVariableResolver to do it. (I already have one custom resolver in place that generates new UUIDs a la http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/.)

I made a custom resolver bound to 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>

that I would use like this: (I am working in Java; I see that you do not appear to be)

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});
}

As of Eclipse 3.5, the problem I am having is that my custom resolver does not get a chance to re-resolve once I've specified a value for the property variable. It appears that the Java Development Tools (Eclipse JDT) do this dependent template variable re-resolution via a mechanism called MultiVariableGuess within the JavaContext (see addDependency()). Unfortunately for us, that mechanism does not seem to be exposed, so I/we can't use it to do the same (without lots of copy-and-paste or other redundant work).

At this point, I am giving up again for a while and will keep typing the leading-lowercase and leading-uppercase names separately into two independent template variables.

这篇关于有没有办法在Eclipse(Helios)代码模板中大写一个变量值的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆