views:

95

answers:

1

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

+3  A: 

There aren't many things that you can pass into a code template. For example ${word_selection} contains the current selection.

But what many people are missing is that you can define your own variables:

private static final ${type} ${name} = new ${type} (${cursor});

Neither ${type} nor ${name} are in the list which you get when you click the "Insert variable..." button. Eclipse notices and allows you to cycle through them with Tab and it will keep the content of these custom "template fields" in sync (so you the part after new gets filled in if you type in the first field).

See this answer for other useful Eclipse templates.

[EDIT] According to the answers in the blog post which you mention, this is only possible at the moment with editor templates, not code templates. I suggest to file a bug against JDT Text to open the API for this.

Aaron Digulla
i meant from eclipse plugin, when i define my own template.
01
@Aaron - What is the difference code templates and editor templates?
fastcodejava
Code templates are used by the IDE when it generates code for you (like in "Generate Getters and Setters"). Editor templates is what you get when you type a keyword (that you define) and press Ctrl+Space.
Aaron Digulla
Thanks, I thought they are the same thing.
fastcodejava