Register custom template variable resolver

Xtext allows easily to add code template proposals for your DSL. By default it ships with some built-in variables besides the ones defined by GlobalTemplateVariables. Now I had the requirement to add a template with a proposal for a version number. Normally all unkown variable bindings in a template are resolved to their name within ${} e.g. ${version} will resolve to “version”. But the proposal should be “1.0.0”. Unfortunately this did not work, since the dot character is not allowed in the variable.

To work around this I wanted to add a custom variable named “version” which will resolve in a first step to the static string “1.0.0”.

1. Extend class XtextTemplateContextType

Xtext’s template variable resolvers are contributed in class XtextTemplateContextType, method addDefaultTemplateVariables().

	protected void addDefaultTemplateVariables() {
		addResolver(new GlobalTemplateVariables.WordSelection());
		addResolver(new GlobalTemplateVariables.LineSelection());
		addResolver(new GlobalTemplateVariables.Date());
		addResolver(new GlobalTemplateVariables.Year());
		addResolver(new GlobalTemplateVariables.Time());
		addResolver(new GlobalTemplateVariables.Dollar());
		addResolver(new GlobalTemplateVariables.User());
		addResolver(new GlobalTemplateVariables.Cursor());

In your subclass you can add your own resolver by overriding addDefaultTemplateVariables(). The following snippet shows the specialized class with the custom VersionResolver:

public class MyTemplateContextType extends XtextTemplateContextType {
	static class VersionResolver extends SimpleTemplateVariableResolver {

		/** Name of the variable, value= {@value} */
		public static final String NAME= "version"; //$NON-NLS-1$

		 * Creates a new word selection variable
		public VersionResolver() {
			super(NAME, "Version"); //$NON-NLS-1$
		protected String resolve(TemplateContext context) {
			XtextTemplateContext ctx = (XtextTemplateContext) context;
			return "1.0.0";
		protected boolean isUnambiguous(TemplateContext context) {
			return false;

	protected void addDefaultTemplateVariables() {
		addResolver(new VersionResolver());

If you want that the variable should be editable you have to return false in method isUnambigious().

2. Guice Configuration

To register your custom XtextTemplateContextType override the configure() method of your UI Module and bind class XtextTemplateContextType to your class:

public class MyDslUiModule extends x.y.z.AbstractMyDslUiModule {
	public MyDslUiModule(AbstractUIPlugin plugin) {

	public void configure(Binder binder) {


After this you will get this variable ‘version’ in your template editor:

Note: Just after finishing this article I found that my colleague Alexander Nittka already blogged this article about the topic.


One thought on “Register custom template variable resolver

  1. I Think posting, “Register custom template variable resolver Karsten’s Blog” ended up being spot on! I actuallycouldn’t see
    eye to eye together with u even more! Finally seems like I reallystumbled upon a internet site worthy of browsing.
    Thank you, Karl

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s