Search code examples
eclipsextextscopingcross-reference

scoping: get all instances of specific type in current file xtext


Following to my question: here I am trying to customize the scoping. I want that in scope of 'Predicate' in my language some of objects will be visible in the scope, types like 'typeDef'.

Predicate:
    'predicate' name=ID ('(' params=TypedParamList ')')?
    (':' body=TemporalExpression TOK_SEMI)
    | ('{' body=TemporalExpression '}');

TypeDef:
    'type' name=ID '=' type=VarType TOK_SEMI;

Here is some example of my language:

type
  move = {left, right};

predicate stop(move m1, move m2) : 
  m1=left and m2=right;

It's not recognize left and right.(can't resolve reference)

I tried something like this:

            val allContentsCurrFile  = EcoreUtil2.eAllOfType(context,TypeDef)
            val allContentsCurrFile2  = EcoreUtil2.getAllContentsOfType(context,TypeDef)

I putted this as parameters to the Scopes.scopeFor method (in addition to the params of Predicate) and this isn't worked for me. I don't know how to do it, how to find all the instances of specific type in current file so the cross reference will work in the Predicate scope.

Thanks.


Solution

  • You have to walk up to the root of the from before you an walk down. EcoreUtil2.getContainerOfType(context, YourRootType) might help with this.

    update:

    The grammar and the example model don't fit, but that seems to be a completely different problem, so I can give only some hints.

    You can only reference named elements to put those elements into the scope.

    Grammar:

    TypedParam:
        (module=[Import] '.')? type=[TypeDef] name=ID;
    
    Referrable:
        TypedParam | TypeDef | TypeConstant;
    

    Code:

    val root = EcoreUtil2.getContainerOfType(context, Domainmodel)
    val allContentsCurrFile  = EcoreUtil2.getAllContentsOfType(root,TypeConstant)