8

我正在寻找如何在用户定义的对象成员上实现代码完成的示例(在 XText 中)。据我所知,我需要使用 IScope,但所有这些连线是如何结合在一起的还不清楚。

鉴于这trait是一个用户定义的类型,我该如何构建一个语法来完成/验证String我键入时包含的方法name.

trait String {
    def toLowerCase(): String
    def toUpperCase(): String
}

val name = new String()
name.toLowerCase()

谢谢

4

2 回答 2

17

这在很大程度上取决于您的语法,您必须做什么才能采用范围界定。假设你有这样的语法

Model:
    statements+=Statement+
;

Statement:
    Trait | VarDef | Call
;

Trait:
    "trait" name=ID "{"
        ops+=Operation*
    "}"
;

Operation:
    "def" name=ID "()" ":" type=[Trait]
;

VarDef:
    "val" name=ID "=" "new" type=[Trait] "()"
;

Call:
    var=[VarDef] "." op=[Operation] "()"
;

那么你的 scopeprovider 看起来像

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {

    IScope scope_Call_op(Call call, EReference ref) {
        return Scopes.scopeFor(call.getVar().getType().getOps());
    }
}    

您可以在此处找到有关该主题的博客系列:

https://web.archive.org/web/20130129085620/http://blogs.itemis.de/stundzig/archives/773

于 2011-10-18T19:36:43.060 回答
3

在我关于 Xtext 的书中,“使用 Xtext 和 Xtend 实现特定领域的语言”,https: //www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend ,有一章关于“较小”Java 语言的范围(也处理继承)。您可以在此处找到示例的来源:https ://github.com/LorenzoBettini/packtpub-xtext-book-examples

于 2014-09-12T08:33:06.773 回答