Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates

Tags:

eclipse

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

like image 286
Michal Avatar asked Jan 12 '11 03:01

Michal


1 Answers

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.

like image 162
Woody Zenfell III Avatar answered Oct 04 '22 07:10

Woody Zenfell III