Xtext 2.9范围提供者 [英] Xtext 2.9 scope provider

查看:94
本文介绍了Xtext 2.9范围提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xtext 2.9更改了范围提供程序的工作方式,我不知道它们现在如何工作.

Xtext 2.9 changed the way scope providers work and I don't understand how they work now.

假设我有以下语法:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
    ((things+=Thing) | (refs+=Reference))*
;

Thing:
    'thing' name=ID '{'
        stuff += Stuff* 
    '}'
;

Stuff:
    'stuff' name=ID
;

Reference:
    'reference' thing=[Thing] stuff=[Stuff] 
;

要使Ref​​erence子句起作用,我需要一个作用域提供程序.

For the Reference clause to work, I need a scope provider.

XText 2.9为您生成以下范围提供程序代码(在MyDslScopeProvider.xtend中):

XText 2.9 generates the following scope provider code for you (in MyDslScopeProvider.xtend):

class MyDslScopeProvider extends AbstractMyDslScopeProvider {
}

AbstractMyDslScopeProvider没有自己的方法,它仅继承自DelegatingScopeProvider.

AbstractMyDslScopeProvider has no methods of it's own, it just inherits from DelegatingScopeProvider.

我无法确定它是如何工作的,或者范围查找的代码应该放在哪里. 文档"并没有真正的帮助,因为只有无用的代码片段,而不是完整的可用示例.

I can't wrap my head around how this works or where the code for the scope lookup should go. The "documentation" doesn't really help, because there's only useless code snippets instead of a complete working example.

XText的早期版本使用AbstractDeclarativeScopeProvider,并且很容易理解和使用,在2.9之前的版本应该是:

Earlier versions of XText used AbstractDeclarativeScopeProvider and that was quite easy to understand and use, pre 2.9 it would have been:

class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
    def IScope scope_Reference_stuff(Reference reference, EReference ref) {
        scopeFor(reference?.thing.stuff)
    }
}

推荐答案

您需要实现getScope方法

override getScope(EObject ctx, EReference ref) {
   if (ref == MyDslPackage.Literals.REFERENCE_THING) {
      return createScopeForThings()
   } else if (ref == MyDslPackage.Literals.REFERENCE_STUFF) {
      return createScopeForStuff()
   }
}

在您的情况下,您将收到一个呼叫,其中EObject是Reference的实例,而EReference是MyDslPackage.Literals.REFERENCE_THING或MyDslPackage.Literals.REFERENCE_STUFF.

In your case you will get a call where the EObject is an instanceof Reference and the EReference is either MyDslPackage.Literals.REFERENCE_THING or MyDslPackage.Literals.REFERENCE_STUFF.

您需要创建并返回IScope的实例,链接器和内容助手可以使用它.有关更多详细信息,请参见IScopeProviderIScope的JavaDoc.

You need to create and return an instance of IScope, which can be used by the linker and content assist. See the JavaDoc of IScopeProvider and IScope for more details.

这篇关于Xtext 2.9范围提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆