Xtext End User / Domain Experts Cheat Sheet

The basic idea of domain specific languages is to provide domain experts with an easy to use language for their modeling needs. However, a domain specific language is always tied to a tool for editing the model. With Xtext, this is obviously Eclipse. Eclipse provides some rich editing features, especially for navigating in models.

Most existing cheat sheets refer to the Java IDE of Eclipse, which you cannot give to domain experts because it contains elements not relevant to them (debugging) or have a slightly different meaning.

So here is an attempt of a small cheat sheet for Xtext end users (domain experts):

 

Editing

ALT-Left and ALT-Right Back / forward Go back and forward in history of where you were in editors
CTRL-PgUp and CTRL-PgDown Cycle tabs  Cycle through tabs of open editors
CTRL-Up and CTRL-Down Scroll  Scroll line up and down
CTRL-M Maximize  Maximize / Restore current editor window
CTRL-W Close Close Current Editor
CTRL-D Delete Line Delete Current Line
CTRL-/ Toggle Comment Toggle Comment for line / selection
CTRL+SHIFT-F Format Auto format
ALT-UP / ALT-Down Move Move current line / selection one line up / down
CTRL-Q Last Edit Go to last edit location
CTRL-L Goto Go to line

Model Editing

Ctrl-Shift-G Find References Find all elements that refer to the current element
F3 or CTRL-MouseClick Follow Link Follow reference under cursor
Ctrl-O Pop up Outline Pops up an outline for easy navigation / filtering
CTRL-1 Quick Fix Quick Fix of Errors (where provided by DSL designer)
CTRL-SPACE Content Assist Get suggestions of possible values
ALT-SHIFT-R Rename Rename current element (will rename other occurrences as well.
CTRL-SHIFT-F3 Open Model element Locate a model element in your workspace (only exported elements are listed)
ALT-SHIFT-Up / Down Expand selection Expand selection to containing element

Xtext Global and Local Scoping – overview

Just recently I posted a small article with an overview on the Xtext scoping architecture. One of the things that took me a while to understand is the difference of global and local scoping. Several people I discussed this had their own ways to explain these. Here is my current way of explaining some types of scoping in Xtext.

Assume that we have a language that supports defining classes, much like in UML: A class can have a name, attributes and it can inherit from another class. And we want to support models that consist of several Xtext files.

Suppose we have the following two files:

A simple definition of two classes, with B inheriting from A. So, obviously we are having a reference from B to A (inheritance). In the grammar that could look like

and Xtext supports us by providing all the automatic linking when it sees “inherits”. But how does it know what to link? If all our models were restricted to be contained in one single file / resource, it would be easy: Just get the current resource and traverse all the contained model elements to find one that matches “A” and set up the link.

Global Scoping

But now, with models split over several files, what do we have to do:

  1. First, find all the model files candidates that could be relevant for our search
  2. Define a strategy to decide if a model file is really considered for traversal
  3. Traverse the model file and find all the candidates that could match our reference to “A”

We wouldn’t want to to this manually. Luckily, Xtext does it for you – actually, it does it a little different, since the approach above would be inefficient – traversing all the model files for each reference would be slow.

So, every time a file changes, Xtext collects the objects and the names that they are known by and puts this information into the index. So instead of traversing all models in all candidate files, Xtext just has to have a look if there is a matching entry in the index. Assuming that the classes above would be contained in a package called “P”. The index could contain:

name Object
P Package “P”
P.A Class A
P.A.a Attribute b
P.B Class B
P.B.b Attribute b

But would we want to see all the possible candidate elements in our reference (types are automatically checked). In most cases no. So Xtext provides several mechanisms to “restrict” the search. Please the the Xtext documentation for details:

  • URI imports: You can have import statements, that actually reference the full URIs of the other files to be considered.
  • Namespace import: You can define a namespace import. If you than provide something like “import P.*” in your grammar, the search will be restricted to those elements whose fully qualified name starts with “P.”
  • Java class path based

There is additional ways to tweak and modify Xtext behavior and there is more data stored in the index (references) etc. But I hope this gives you the general idea. However, the story is not finished.

 Local Scoping

Now suppose we want to have a second grammar, where we define instances of the classes and values for their attributes, just like this:

Since the classes are in a separate file, it is obvious that the link from the instance to its defining class is done in the way described above. But also the attributes values are linked to the attribute definition in the class model, so how is this done?

Obviously, we could also just use the index. But we have to restrict the possible references as well, since in instance “a”, a reference to attribute “b” would not be valid. So what we could do is to get the list of possible references from the index, traverse the class hierarchy and filter out all attributes that are not valid.

But, for finding all valid attributes, we are traversing all attributes in the class hierarchy anyway. So why not forget about the index, and just build up the list of possible references while we traverse the model? This is done by local scoping, which you can easily customize to modify the list of reference candidates. See the Xtext documentation on how to do that.

So, then why do we need the attributes in the index? Simple, we don’t. The list of possible attribute values is calculated by us, so we don’t need them there. For our own Xtext grammars, we can override the strategy of what is put into the index and what not. So we decide to not put attributes in the index. Xtext per default cannot know what you want to put in the index, so it puts in everything from your model that has a “name” attribute. It might be worthwhile customizing this behavior, since it could save us a lot of CPU and memory.

 Summary

For me, it is useful to divide scoping in two different aspects:

  1. I can find out the possible candidates of a link by traversing references in my model. The traversal can easily span multiple resources/files, as long as the traversal can be done by standard EMF means.
  2. To find the possible candidates, I would have to think about a strategy on which model files to load etc. I cannot access these candidates by traversing the references that I already have, because I might not even reach those models –> global scoping.

 

 

My mental Xtext high-level model

While Xtext is a powerful tool, it is sometimes easy to loose the big picture of the linking, scoping, global scope etc. For my own mental relief, I have made a little diagram, which might be useful for others (no guarantee for correctness or level of detail). It applies only to a specific configuration (using Xbase and Imported Namespace Aware Scoping).

 

So basically what happens is:

  1. Parsing: The framework parses the model files (Xtext-files). Links between elements are not resolved yet.
  2. Index Building: The model is traversed, and basically everything reachable that has a name attribute is packed into an EObjectDescription and that is put into an index. What is put there can be customized. Caveat: EObjectDescription allows you to have some user data stored with your description in the index. Often the first thought is, to store e.g. some type information or, e.g. in case of class models, some inheritance information. However, references are not yet set up / linked. So you cannot put anything in the user data that would traverse link references!
  3. Lazy Linking / Scoping: This is where the actual linking is done. In our case (Xbase-based language), this scoping is based on XbaseScopeProvider. This actually delegates to some Guice-configured class, which in our case is an ImportedNamespaceAwareLocalScopeProvider. This looks for model elements with the attribute “importedNamespace” in your model and pulls the objects from the index, that match any of the importedNamespace values (you can specify wildcards in the imports).
    In addition, in these classes you can add your own scopes, the local scopes, which are derived from the existing model information.
  4. Content assist: Uses the scoping data for suggesting alternatives.

The index is transient in memory and is loaded from / stored to disk on start / stop of Eclipse. During run-time there is no up-to-date representation on disk. The org.eclipse.xtext.ui.shared.Access class can be used as a handle to index data.