How to pass parameter into code-template in eclipse-plugin

后端 未结 2 1180
死守一世寂寞
死守一世寂寞 2021-02-15 13:06

I want to create a plugin which defines a new code template (like this blog post). How can I pass a parameter into the template? like ${name:param}?

2条回答
  •  别跟我提以往
    2021-02-15 13:16

    This solution is aimed at Eclipse 4.2 Juno, I have not tested this in any other environment.

    All you have to do is pass your parameters, and then you'll have them available.

    Say we wanted to create a TemplateVariableResolver that would uppercase the first letter of a passed parameter.

    You'll first populate your plugin.xml as follows:

    
        
    
    

    You'd also create your custom resolver:

    public void resolve(TemplateVariable variable, TemplateContext context) {
        if (variable.getVariableType().getParams().size() > 0) {
            StringBuffer result = new StringBuffer();
            for(String value : (List) variable.getVariableType().getParams()) {
                value = value.substring(0,1).toUpperCase() + value.substring(1);
                result.append(value);
            }
            variable.setValue(result.toString());
        }
    }
    

    Finally in your code Template:

    String name = ${Uppercase(jim,laughlin)};
    

提交回复
热议问题