Xtext - how to reference an element of an element

回眸只為那壹抹淺笑 提交于 2019-12-11 11:30:03

问题


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

How can I add something like the following:

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

So that I can write:

entity A {
  name:String
}
entity B {
  name:String
}
map A.name => B.name

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

thanks


回答1:


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)

    }

}


来源:https://stackoverflow.com/questions/37664825/xtext-how-to-reference-an-element-of-an-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!