Content Assist

The Xtext generator, amongst other things, generates the following two content assist (CA) related artifacts:

First we will investigate the generated Abstract[Language]ProposalProvider with methods that look like this:

ProposalProvider

public void complete[TypeName]_[FeatureName](
  EObject model, 
  Assignment assignment, 
  ContentAssistContext context, 
  ICompletionProposalAcceptor acceptor) {
  // clients may override
}

public void complete_[RuleName](
  EObject model, 
  RuleCall ruleCall, 
  ContentAssistContext context, 
  ICompletionProposalAcceptor acceptor) {
  // clients may override
}

The snippet above indicates that the generated ProposalProvider class contains a complete*-method for each assigned feature in the grammar and for each rule. The brackets are place-holders that should give a clue about the naming scheme used to create the various entry points for clients. The generated proposal provider falls back to some default behavior for cross-references and keywords. Furthermore it inherits the logic that was introduced in reused grammars.

Clients who want to customize the behavior may override the methods from the AbstractProposalProvider or introduce new methods with a specialized first parameter. The framework dispatches method calls according to the current context to the most concrete implementation, that can be found.

It is important to know, that for a given offset in a model file, many possible grammar elements exist. The framework dispatches to the method declarations for any valid element. That means, that a bunch of complete* methods may be called.

Sample Implementation

To provide a dummy proposal for the description of a model object, you may introduce a specialization of the generated method and implement it as follows. This will give ‘Description for model #7’ for a model with the intAttribute '7'

public void completeModel_StringDescription (
  Model model, 
  Assignment assignment, 
  ContentAssistContext context, 
  ICompletionProposalAcceptor acceptor) {
  // call implementation in superclass
  super.completeModel_StringDescription(
    model, 
    assignment, 
    context, 
    acceptor);

  // compute the plain proposal
  String proposal = "Description for model #" + model.getIntAttribute();

  // convert it to a valid STRING-terminal
  proposal = getValueConverter().toString(proposal, "STRING");

  // create the completion proposal
  // the result may be null as the createCompletionProposal(..) methods 
  // check for valid prefixes
  // and terminal token conflicts
  ICompletionProposal completionProposal = 
    createCompletionProposal(proposal, context);

  // register the proposal, the acceptor handles null-values gracefully
  acceptor.accept(completionProposal);
}