Peter Friese

Developer Advocate / Mobile Developer / Public Speaker

Xtext Tricks #1: Enhancing Completion Proposals

| Comments

Those of you who follow me on Twitter might have noticed I am working on an Xtext based DSL for Behaviour Driven Development. Part of the DSL allows the DSL user to define actors and the verbs these actors can execute. Actors can have a hierarchy (much like a class hierarchy), meaning an actor will inherit all verbs of it’s super actors. As the list of verbs can grow quite a bit, the content assist drop down menu becomes a bit overwhelming.




To alleviate ┬áthis situation, I decided to display the “owner” of a verb along with the name of the verb in the content assist drop down box. Here is my first try:

public class StoryDslProposalProvider extends AbstractStoryDslProposalProvider {
  protected ICompletionProposal createCompletionProposal(EObject element, String proposal, String displayString, ContentAssistContext contentAssistContext) 
    if (element instanceof Verb) {
      Verb verb = (Verb) element;
      Subject owner = ((Subject)verb.eContainer());
      String myDisplayString = verb.getName() + ": " + owner.getName();
      return super.createCompletionProposal(element, proposal, myDisplayString, contentAssistContext);
    return super.createCompletionProposal(element, proposal,  displayString, contentAssistContext);

With this piece of code in place, the content assist proposals for verbs will now display the name of the “owner” to the right of the replacement text:




Sebastian pointed out that there is a getDisplayString() in AbstractContentProposalProvider that you should override in order to create a custom display string. It turned out, however, that this method will not be called as expected. I filed a bug, so expect to see a fix in one of the next milestones of the Xtext 0.8.0 stream.