我正在寻找一个如何在用户定义的对象成员上实现代码完成的示例(在XText中)。据我所知,我需要使用IScope,但是如何将所有这些连接在一起还不清楚。
鉴于trait
是用户定义的类型,当我键入String
时,我该如何构建一种语法来完成/验证name.
中包含的方法?
trait String {
def toLowerCase(): String
def toUpperCase(): String
}
val name = new String()
name.toLowerCase()
谢谢
最佳答案
这很大程度上取决于您的语法,采用范围界定所必须要做的事情。
假设您有一个类似的语法
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] "()"
;
那么您的范围提供者将看起来像
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
关于scope - 范围对象的Xtext示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791871/