Karsten's Blog

January 6, 2011

Register custom template variable resolver

Filed under: Xtext — kthoms @ 12:47 PM

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";
		}
		@Override
		protected boolean isUnambiguous(TemplateContext context) {
			return false;
		}
	}

	@Override
	protected void addDefaultTemplateVariables() {
		super.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) {
		super(plugin);
	}

	@Override
	public void configure(Binder binder) {
		super.configure(binder);
		binder.bind(XtextTemplateContextType.class).to(MyTemplateContextType.class);
	}
}

Result

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.

About these ads

1 Comment »

  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

    Comment by http://tinyurl.com/karleyre31671 — January 27, 2013 @ 4:39 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

The Silver is the New Black Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 375 other followers

%d bloggers like this: