3

我正在使用 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.

4

1 回答 1

2

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

acceptor.accept(issue,
    "Add missing use statement",
    "Add missing use statement",
    "upcase.png") [ element, context |
      ..
    ]
于 2013-09-12T14:31:46.700 回答