Xtext-based editors automatically support code templates. That means that you get the corresponding preference page where users can add and change template proposals. If you want to ship a couple of default templates, you have to put a file named templates.xml inside the templates directory of the generated UI-plug-in. This file contains templates in a format as described in the Eclipse online help .
By default Xtext registers ContextTypes for each rule ( .[RuleName]) and for each keyword ( .kw[keyword]_). If you don’t like these defaults you’ll have to subclass XtextTemplateContextTypeRegistry and configure it via Guice.
In addition to the standard template proposal extension mechanism, Xtext ships with a predefined set of TemplateVariableResolvers to resolve special variable types inside a given template (i.e. TemplateContext). Besides the standard template variables available in org.eclipse.jface.text.templates.GlobalTemplateVariables like ${user}, ${date}, ${time}, ${cursor}, etc., these TemplateVariableResolver support the automatic resolving of CrossReferences (type CrossReferences) and Enumerations (type Enum) like it is explained in the following sections.
It is best practice to edit the templates in the preferences page, export them into the templates.xml-file and put this one into the templates folder of your UI-plug-in. However, these templates will not be visible by default. To fix it, you have to manually edit the xml-file and insert an ID attribute for each template element.
Xtext comes with a specific template variable resolver TemplateVariableResolver called CrossReferenceTemplateVariableResolver, which can be used to place cross-references within a template.
The syntax is as follows:
${<displayText>:CrossReference([<MyPackageName>.]<MyType>.<myRef>)}
For example the following template:
<template name="transition" description="event transition" id="transition"
context="org.eclipse.xtext.example.FowlerDsl.Transition" enabled="true">
${event:CrossReference('Transition.event')} =>
${state:CrossReference('Transition.state')
</template>
yields the text event => state and allows selecting any events and states using a drop down.
The EnumTemplateVariableResolver resolves a template variable to EEnumLiteral literals which are assignment-compatible to the enumeration type declared as the first parameter of the the Enum TemplateVariable.
The syntax is as follows:
${<displayText>:Enum([<MyPackage>.]<EnumType>)
For example the following template (taken from the domainmodel example):
<template name="Operation" description="template for an Operation"
id="org.eclipse.xtext.example.Domainmodel.Operation"
context="org.eclipse.xtext.example.Domainmodel.Operation"
enabled="true">
${visibility:Enum('Visibility')} op ${name}(${cursor}):
${type:CrossReference('Operation.type')}
</template>
yields the text public op name(): type where the display text ‘public’ is replaced with a drop down filled with the literal values as defined in the EEnum Visibility. Also, name and type are template variables.