Jordi
Jordi

Reputation: 5908

Can you define your own template variables in Eclipse

In Eclipse there are templates that help you by automatically inserting some code or comments. You can edit these templates yourself via Preferences > Java > Editor > Templates. There are so-called "template variables" that you can use to make these templates a little smarter.

For instance, there is the ${see_to_overridden} variable that inserts "@see my.package.name.SpuerclassName#methodName(int, my.other.package.SomeType, ...)" into a javadoc comment. It would be really great if I could define my own variables, so that I could obtain Superclassname, SomeType etc. without having the "@see" thing prepended to it so that I could for instance link to the appropriate method. There appear to exist no template variables for this, so I was wondering if there was any way to create your own template variables.

Upvotes: 22

Views: 10794

Answers (1)

Urs Reupke
Urs Reupke

Reputation: 6921

According to this Blog post (alternative link) by the eclipse team, you can.

Extend org.eclipse.ui.editors.templates, add a org.eclipse.jface.text.templates.TemplateVariableResolver and fill in the values required by the system. They should be self explanatory if you ever defined your own template.

Then implement the resolver, replacing your variable with a appropriate string.

The Blog post holds more details and screenshots.

Upvotes: 10

Related Questions