Xtext – how to reference an element of an element

questions :

Using the simple entity example https://eclipse.org/Xtext/documentation/102_domainmodelwalkthrough.html

How can I add something like the following:

    'map' entityA=[Entity] '.' featureA=[Feature] '=>' entityZ=[Entity] '.' featureZ=[Feature]

So that I can write:

entity A {
entity B {
map A.name => B.name

So that I get content assist when writing A. scoped to the features of A only?




Answer :

you usually do such things by adapting the concept of “scoping”. the content assist ususally respects scoping. so adapt your scope provider like this

package org.xtext.example.mydsl1.scoping

import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EReference
import org.eclipse.xtext.scoping.IScope
import org.xtext.example.mydsl1.myDsl.Map
import org.xtext.example.mydsl1.myDsl.MyDslPackage
import org.eclipse.xtext.scoping.Scopes

class MyDslScopeProvider extends AbstractMyDslScopeProvider {

    override getScope(EObject context, EReference reference) {
        if (reference === MyDslPackage.Literals.MAP__FEATURE_A) {
            if (context instanceof Map) {
                return Scopes.scopeFor(context.entityA.features)
            return IScope.NULLSCOPE
        if (reference === MyDslPackage.Literals.MAP__FEATURE_Z) {
            if (context instanceof Map) {
                return Scopes.scopeFor(context.entityZ.features)
            return IScope.NULLSCOPE

        super.getScope(context, reference)







Facebook Comments

Post a comment