2011-01-12 2 views
15

私は変数を持つコードテンプレートを持っていますが、この変数の値を(最初の文字だけ)大文字にしますいくつかの出現でのみ。これを行う方法はありますか?Eclipse(Helios)コードテンプレートの変数の値の最初の文字を大文字にする方法はありますか

次のようにテンプレートコードがある - 私は私の関数名にプロパティ名を大文字にしたいと思います...

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

答えて

14

私もこれをして、それを行うにはカスタムTemplateVariableResolverを構築しようとしました。

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> 
:(。私はすでに新しいのUUIDラ http://dev.eclipse.org/blogs/jdtui/2007/12/04/text-templates-2/を生成し、代わりに1つのカスタムリゾルバを持っている)

を私はcapitalizeにバインドされたカスタムリゾルバを作りました私はこのように使用する

は:(私は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)は、JavaContextaddDependency()参照)内のMultiVariableGuessというメカニズムを介して、この依存テンプレートの再解析を行うように見えます。私たちのために残念なことに、その仕組みは公開されていないようですので、コピー&ペーストやその他の冗長な作業を何もしなくても同じことをすることはできません。

この時点で、もう一度あきらめて、大文字小文字と先頭大文字の名前を2つの独立したテンプレート変数に分けて入力し続けます。

+0

これはすばらしい答えです。ありがとう。 – Michal

関連する問題