Home > Xtext > Hover support in Xtext 2.0: Tutorial

Hover support in Xtext 2.0: Tutorial

Xtext 2.0 comes with an all new Hover API (see Christoph’s Blog http://ckulla.wordpress.com/2011/02/06/hover-support-in-xtext-2-0/). I want to give a short introduction on how to use what with Xtext’s Greeting Example.

So first we create a new Xtext project with the wizard and generate the language. We start a runtime application and create a project with a model file. Here is what the default hover looks like:

We want is to adopt the hover support to look like this:

We have to basically implement 2 interfaces: IEObjectHoverProvider to customize the header line and IEObjectDocumentationProvider to customize the content section. Here 2 simple implementations (using appropriate superclasses)

package org.xtext.example.mydsl.ui;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.ui.editor.hover.html.DefaultEObjectHoverProvider;
import org.xtext.example.mydsl.myDsl.Greeting;

public class MyDslEObjectHoverProvider extends DefaultEObjectHoverProvider {

	protected String getFirstLine(EObject o) {
		if (o instanceof Greeting) {
			return "Damn good greeting: " + ((Greeting)o).getName();
		return super.getFirstLine(o);



package org.xtext.example.mydsl.ui;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
import org.xtext.example.mydsl.myDsl.Greeting;

public class MyDslEObjectDocumentationProvider implements IEObjectDocumentationProvider {

	public String getDocumentation(EObject o) {
		if (o instanceof Greeting) {
			return "This is a nice Greeting with nice <b>markup</b> in the <i>documentation</i>";
		return null;


Finally we have to bind these classes in the UiModule of our dsl. 

package org.xtext.example.mydsl.ui;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.documentation.IEObjectDocumentationProvider;
import org.eclipse.xtext.ui.editor.hover.IEObjectHoverProvider;

 * Use this class to register components to be used within the IDE.
public class MyDslUiModule extends org.xtext.example.mydsl.ui.AbstractMyDslUiModule {
	public MyDslUiModule(AbstractUIPlugin plugin) {

	public Class<? extends IEObjectHoverProvider> bindIEObjectHoverProvider() {
		return MyDslEObjectHoverProvider.class;

	public Class<? extends IEObjectDocumentationProvider> bindIEObjectDocumentationProviderr() {
		return MyDslEObjectDocumentationProvider.class;

That is all we have to do to get customized hovers with Xtext 2.0. 

Categories: Xtext Tags:
  1. Daniel
    July 18, 2011 at 15:45

    Are you working with the nighly build? I’m using the itemis indigo distribution (2.0.201107071649) which contains Xtext SDK 2.0.0.v201106070531. In this version is no bindIEObjectHoverProvider or bindIEObjectDocumentationProvider. I have to bind it by myself in using the binder provided in the AbstractMyDslUiModule.configure method.

    • July 18, 2011 at 16:02

      No I use the release version. The bind methods I manually added to my UI module. Regards Christian

  2. Roman Yusupov
    March 20, 2012 at 10:10

    Hello. I am very new to creating DSL and Xtext. I am trying to make a documentation function in the Editor. I am implementin a DSL for testing and the prototype of the language looks something like this:

    * Documentation of test case
    ASSERT http.response 200

    In this example it’s a test case that sends an HTTPGET request to an URL and checks if the response is 200 OK

    So I wonder if you know if there is a way to get same hover and documentation result as in your example, but when I hover over any part of the test case I get the documentation of the test case in the tooltip.

    The EBNF for an Expression and a test case looks like this:

    terminal DOC_COMMENT: ‘/**’ -> ‘*/’;

    (Documentation?) TestCase = TestCase

    GetCommand = GetCommand AssertCommand += (AssertCommand)*

    ‘GET’ name = “STRING”

    code = DOC_COMMENT
    ‘ASSERT’ ‘http.response’ code = INT

    I know that it is probably not the best way to define it, but due to the lack of knowledge this was the best I could conjure. Anyway if you could point me in the right direction I would be grateful!
    Best regards
    Roman Y.

    • March 20, 2012 at 19:01


      have a look at org.eclipse.xtext.ui.editor.hover.AbstractEObjectHover.getXtextElementAt(XtextResource, int)
      and you will see that besides places where a cross reference is located it uses an ILocationInFileProvider
      to getSignificantTextRegion. if you have a look at the default implementation of ILocationInFileProvider
      you will see that it basically will look for features that are named name or id.

      Your grammar is Lacking/Model names and ids there are no “significant regions” => all is one region for the top element

      if you want to change this behavior in general you could

      – override org.eclipse.xtext.ui.editor.hover.AbstractEObjectHover.getXtextElementAt(XtextResource, int) an call org.eclipse.xtext.resource.ILocationInFileProvider.getFullTextRegion(EObject) there
      – override org.eclipse.xtext.resource.DefaultLocationInFileProvider.getSignificantTextRegion(EObject) either by fixing the logic or (i do not really know if this may have unwanted changes at other places) to getFullTextRegion
      – change the grammar to have names or ids (the hovers will occur for the places of the name or id)


  3. Roman Yusupov
    March 26, 2012 at 09:52

    Thank you for you help I have resolved the issue by fixing the model adding ids to it. It also turned out that I did not need to have any particular rule or terminal to have documentation in a matter that I have wanted.
    I’ve discovered that there is a org.eclipse.xtext.documentation.impl.MultiLineCommentDocumentationProvider
    that takes the comment within /** and */ tags and places it in the hover.
    So I have used your example with the slight difference of
    public Class bindIEObjectDocumentationProviderr()
    now returns MultiLineCommentDocumentationProvider.class

    So thanks again for your help and I hope that question wasn’t too silly since I may return with ones like it as I explore more of the subject and Xtext overall.


  4. Mathieu
    June 2, 2015 at 16:22

    Thanks for your help!

  1. No trackbacks yet.

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 )

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