Christoph's Blog

Archive for the ‘Uncategorized’ Category

Hover support in Xtext 2.0

with 9 comments

From the time I was starting with Xtext 2-3 years ago I recall a statement from Sven Efftinge saying “I want a JDT for all my languages”. Well, Xtext is great and it is coming closer and closer to the JDT level. The guys are really doing a fantastic job. But something I was missing was the support for hovers like the JDT offers for java-doc comments. So I started to work on this topic and had the chance to contribute my implementation back to Xtext. Some of you may have already noticed it in the latest milestones of Xtext 2.0. Let’s take a closer look at it.


When editing a mwe2 file with Xtext 2.0 you can see the hovers in action. Just move your mouse over a reference to a mwe2 variable and a hover will show up.

I use mwe2 as an example language here. There is no special configuration in mwe2 to show these hovers. It should work  for any language out of the box. The hover displays the text of the leading multi line comment (java style). If you want to display different content or your language may have a different comment syntax, just replace the default implementation with your own and adopt the Guice configuration.

The hover will appear whenever you move your mouse on a cross-reference or on a declaration of an element. It is a 3-step procedure:

  1. Get the element on the mouse hover location: If it is a cross-reference get the referenced element. If it is a name of a declaration, get the declared element (element means an EObject). This is implemented in AbstractEObjectHover.
  2. Get the documentation for the element in step 1 (see interface IEObjectDocumentationProvider). The default implementation retrieves the documentation from the leading multi line comment (see MultiLineCommentDocumentationProvider). The returned documentation is of type String. Please note that the IEObjectDocumentationProvider interface is contained in Xtext’s non-ui plug-in. It may be useful without the user interface, e.g. generating documents from your model.
  3. Display the documentation of step 2 (see DispatchingEObjectTextHover and IEObjectHoverProvider and DefaultEObjectHoverProvider). The default hover implementation is implemented by a Browser widget. This means the documentation can contain any html formatting and it will be displayed correct (e.g. you can include images). The default implementation adds the type name and the element name in front of the actual documentation.

The hover will appear in content assist as well:

Support of multiple languages

Xtext supports references across different languages (or meta models). A file may can contain references to elements of other languages. What does this mean for hovers? Well, each language can have it’s own type of hover. Therefore an editor has to support different types of hovers. This is supported by utilizing the IGlobalServiceProvider (implemented by Sven). Each language/metamodel can register it’s own IEObjectHoverProvider via a shared module. Look at DispatchingEObjectTextHover for reference. This mechanism is used to display java-doc hovers for references to the JavaTypes meta model.

Java References

You can experience this mechanism in mwe2. Just move your mouse over a java reference. It looks like:

This is the original JDT hover, luckily no code duplication is required to make it available. Code completion works as well:

I expect this to work the same way for the upcoming Xtend2. I’m looking forward to it.

Quick fixes

I’ve also added a quick fix hover as known from JDT. It should work out of the box for any language which is providing quick fixes. Again, no additional configuration is required to make this work for your language.


That’s it. You get hovers for free with Xtext 2.0. You don’t have to work with the ITextHover interfaces, you can just provide your own IEObjectDocumentationProvider to change the hover content. It supports rich formatting via html. The Guice configuration still allows you to use your own ITextHover implementation where it is appropriate. The JDT javadoc hover is supported out of the box for java references.

Please feel free to add any comments. I would be happy to hear from you.

Finally I would like to thank the Xtext team for supporting me and applying my contribution. I enjoyed to work with you guys!

Written by Christoph Kulla

February 6, 2011 at 1:06 pm

Posted in Uncategorized