我正在使用 Xtend 为我的 Xtext 语言的 Eclipse UI 插件编写自定义 QuickFixes。这个想法是扩展org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider
和使用 call 的accept
方法org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor
。
accept
有几个重载的签名,包括:
void accept(Issue issue, String label, String description, String image, IModification modification)
void accept(Issue issue, String label, String description, String image, ISemanticModification semanticModification)
使用 Xtend 的 lambda 表达式语法很容易使用这些签名中的第一个:
@Fix(MyDSLValidator::INVALID_TYPE_NAME)
def removeInitialUnderscore(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue,
"Remove initial underscore",
"Remove initial underscore",
'upcase.png')
[
context |
context.xtextDocument.replace(issue.offset, 1, "")
]
}
Xtend 编译器生成以下 Java 代码:
public void removeInitialUnderscore(final Issue issue, final IssueResolutionAcceptor acceptor) {
final IModification _function = new IModification() {
public void apply(final IModificationContext context) throws Exception {
IXtextDocument _xtextDocument = context.getXtextDocument();
Integer _offset = issue.getOffset();
_xtextDocument.replace((_offset).intValue(), 1, "");
}
};
acceptor.accept(issue,
"Remove initial underscore",
"Remove initial underscore",
"upcase.png", _function);
}
现在我想编写第二个 QuickFix 使用ISemanticModification
而不是,IModification
这样我就可以访问底层 EMF 模型,而不仅仅是字符串。但是,我很难告诉 Xtend 编译器我想使用哪种方法重载:
@Fix(MyDSLValidator::MISSING_USAGE)
def addMissingUseStatement(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue,
"Add missing use statement",
"Add missing use statement",
"upcase.png",
[
EObject element |
(element.eContainer as MyContainer).usages.add(
(element as MyElement).myattr.eContainer as MyOtherContainer
)
])
}
Xtend 总是认为我想传递 anIModification
而不是 anISemanticModification
并在编译的 java 代码中产生错误,因为这两种apply
方法具有不同的签名,IModification
需要一段IModificationContext
时间ISemanticModification
需要一个EObject
.
@Fix(MyDSLValidator.MISSING_USAGE)
public void addMissingUseStatement(final Issue issue, final IssueResolutionAcceptor acceptor) {
final IModification _function = new IModification() {
public void apply(final IModificationContext element) throws Exception {
EObject _eContainer = element.eContainer(); // COMPILE ERROR
EList<Circuit> _usages = ((MyContainer) _eContainer).getUsages();
MyAttr _myattr = ((MyAttr ) element).getMyattr();
EObject _eContainer_1 = _myattr.eContainer();
_usages.add(
((MyOtherContainer) _eContainer_1));
}
};
acceptor.accept(issue,
"Add missing use statement",
"Add missing use statement",
"upcase.png", _function);
}
我如何告诉 Xtend 使用哪个重载?据我所知,在 Xtend 中创建匿名对象是不可能的,所以需要另一种方法来调用IssueResolutionAcceptor.accept
传递一个ISemanticModification
.