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

前端 未结 1 2066
既然无缘
既然无缘 2021-01-01 17:46

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?<

1条回答
  •  隐瞒了意图╮
    2021-01-01 18:16

    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 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:)

    
      
      
    
    

    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.

    0 讨论(0)
提交回复
热议问题