Xtend lambda 表达式重载 Xtext QuickFix API



我正在使用Xtend为我的Xtext语言的Eclipse UI插件编写自定义QuickFixes。这个想法是扩展org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider并使用调用org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptoraccept方法。

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);
}

现在,我想编写第二个使用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 总是认为我想传递一个IModification而不是一个ISemanticModification,并在编译的 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

ISemanticModification接受两个参数,其中IModization只使用一个参数。要实现语义修改,您应该分别使用具有两个参数的 lambda:

acceptor.accept(issue,
    "Add missing use statement",
    "Add missing use statement",
    "upcase.png") [ element, context |
      ..
    ]

相关内容

  • 没有找到相关文章

最新更新