[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.tmf] Re: looking for a xtext C/Java like expression language

Here are the relevant parts of the code:

@Override
public void createProposals(ContentAssistContext context, 
ICompletionProposalAcceptor acceptor)
{
 final EObject currentModel = context.getCurrentModel();
 final AbstractNode currentNode = context.getCurrentNode();
 LeafNode leafNode = null;
 if(currentNode instanceof LeafNode)
 {
  leafNode = (LeafNode) currentNode;
 }

 if(context.getOffset() <= ParsetreeUtil.getOffset(currentNode) || 
currentModel == null)
 {
  super.createProposals(context, acceptor);
  return;
 }

 final TemplateMetaModel metaModel = new TemplateMetaModel();
 final ExecutionContextImpl ctx = new ExecutionContextImpl(new 
TypeSystemImpl());
 ctx.registerMetaModel(metaModel);

 final Type contextType = getContextType(currentModel, metaModel, ctx);
 if(contextType == null)
  return;
 createXtendProposals(context, acceptor, contextType);
}

protected Type getContextType(EObject element, EmfRegistryMetaModel 
metaModel, ExecutionContext ectx)
{
 while (true)
 {
  if(element == null)
   return null;

  if(element instanceof Template)
   return metaModel.getTypeForEClassifier(((Template) 
element).getModelElement().getClassifier());

  if(element instanceof TemplatePart)
   return metaModel.getTypeForEClassifier(((TemplatePart) 
element).getContextElement());

  ...
 
  element = element.eContainer();
 }
}

protected void createXtendProposals(ContentAssistContext context, 
ICompletionProposalAcceptor acceptor,
    Type contextType)
{
  final int cursorOffset = context.getOffset();
  final int nodeOffset = ParsetreeUtil.getOffset(context.getCurrentNode());

  // get relevant part of the document
  String part = context.getViewer().getDocument().get().substring(nodeOffset 
+ 1, cursorOffset);

  // prepare factory + extensions
  final ProposalFactory factory = new 
ProposalFactoryEclipseImpl(cursorOffset);
  final List<Extension> extensions = new ArrayList<Extension>();
  // prepare context
  ExecutionContext ctx = new ExecutionContextImpl();
  final TemplateMetaModel mm = new TemplateMetaModel();
  ((ExecutionContextImpl) ctx).registerMetaModel(mm);
  ctx = ctx.cloneWithVariable(new 
Variable(ExecutionContext.IMPLICIT_VARIABLE, contextType));

  ctx = ctx.cloneWithResource(new Resource()
  {
    public String getFullyQualifiedName()
    {
	return null;
    }

    public void setFullyQualifiedName(final String fqn)
    {
	// empty
    }

    public String[] getImportedNamespaces()
    {
	return new String[0];
    }

    public String[] getImportedExtensions()
    {
	return new String[0];
    }
  });
  ctx = FastAnalyzer.computeExecutionContext(part, ctx, extensions);
  final int i = part.lastIndexOf(';');
  if(i != -1)
  {
    part = part.substring(i);
  }

  final List<? extends ICompletionProposal> proposals = (List<? extends 
ICompletionProposal>) new ExpressionProposalComputer().computeProposals(
      part, ctx, factory);

  for(final ICompletionProposal proposal: proposals)
  {
    acceptor.accept(proposal);
  }
}

HTH,
 Achim

Knut Wannheden wrote:

> Achim,
> 
> Achim Demelt wrote:
>> Michael,
>> 
>> We're embedding Xtend expressions in our grammar to do that. The major
>> drawback is that the expressions themselves are not captured by grammar
>> rules, but are instead defined within a String literal. So to the user of
>> the language, it looks a bit like this:
>> 
>> define template 'ClassDescription' for ecore::EClass
>>  if '!this.isAbstract && this.eAttributes.size() > 0'
>>  
>>  heading '"EClass " + this.name'
>>  body
>>   ...
>>  end body
>> end template
>> 
> 
> We're also embedding the Xtend expression language in some of our DSLs by
> letting the DSL extend the expression language. As a result the
> expressions don't need to be parsed as simple tokens. Although we have to
> make sure the grammars don't interfere.
> 
>> But using Xtend has to nice advantages:
>> 
>> 1) We can reuse the Xtend content assist facility. So at the right
>> places, we just delegate from our Xtext language content assist code to
>> Xtend content assist. Works like a charm.
>> 
> 
> Interesting. Would you be able to share that code? Although I am not sure
> it would work quite the same in our case.
> 
> Regards,
> 
> --knut