JDT/UI and Text Team Blog

The JDT/UI and Text team presents new API and features

Text Templates 2

In the previous blog post, I have shown how to use and add templates through the UI. In this post, I’m going to show how to contribute templates and template variables through an extension point.

Add a Set of Templates

Adding a set of templates is easy. Use the org.eclipse.ui.editors.templates extension point. It allows to include an XML file which contains the templates you want to contribute:

<extension point="org.eclipse.ui.editors.templates">

Where swttemplates.xml may contain the description of the grid data template from my previous blog post:

<?xml version="1.0" encoding="UTF-8"?>
	description="Set the layout data of a control to grid data"
	context="swt" enabled="true" autoinsert="false"

>GridData ${dataName:newName}= new GridData(SWT.FILL, SWT.FILL, true, true);

Add a Template Variable

The org.eclipse.ui.editors.templates extension point can also be used to add a new template variable. To do so you need to implement an org.eclipse.jface.text.templates.TemplateVariableResolver. The template variable resolver is asked to resolve its template variable to a string whenever a template is evaluated. Lets say we want to add a variable called UUID which resolves to a random universally unique identifier. First we contribute the variable through the extension point:

<extension point="org.eclipse.ui.editors.templates">
      		description="Resolves to an universally unique identifier"
      		name="universally unique identifier" type="UUID"/>

Then we implement our UUIDResolver:

public class UUIDResolver extends TemplateVariableResolver {
	protected String resolve(TemplateContext context) {
		return UUID.randomUUID().toString();

Once you have added the plug-in defining the new variable to your runtime you can start using the new variable in any Java template. You can for example add a uuid field template:

uuid_field template definition

And this is how the result looks like when you insert the uuid_field template:

the uuid template inserted

Cool isn’t it?

What’s next?

There is more to come in 3.4 concerning templates. There is bug 69581 [templates] Template view tagged for 3.4 M5. One of the problems with templates is that you need to know that they are there. The Templates view will change that. It will show all templates in a view, and will enable users to insert templates into an editor by drag and drop.

I would also like to add more default templates, for example there are no templates for AWT/Swing. I could also imagine to have templates for JFace or for Implementation Patterns. But we need YOU to do that. You can do so by creating a new enhancement request against jdt/text and attach your patch. It would be great if I could provide high quality template sets contributed by you.

Posted December 4th, 2007 by Benno Baumgartner in category: Uncategorized
Responses are currently closed, but you can trackback from your own site.

3 Responses to “Text Templates 2”

  1. Eugene Kuleshov Says:

    What is sad about Eclipse templates is that there is no centralized UI to manage them and each editor have to declare its own prefence page for editing templates. More over, plain text editors still don’t have template support.

    Special view for templates is sounds fancy but somehow seem not practical. Practically the same as the Snippets view. Actually maybe it should be merged into the Snippets view instead, so won’t have to create an extra view

    Another annoying thing about template support is that there is no quickfix (Ctrl-1) to create a new template from selected text. That would be a real time saver.

  2. Jacek Pospychala Says:

    agree with Eugene. Create template from selection by Ctrl-1 (or Ctrl-Space) would be awesome, people often don’t realize how much code they repeat and that templates would help them :)

  3. Use Maven Artifact Version in Eclipse Templates « Andreas Höhmann’s Weblog Says:

    [...] easy to add a new template-variable to eclipse, you can read this. Based on  org.eclipse.jface.text.templates.TemplateVariableResolver we can wrote a [...]

Recent Posts