如何使用重命名重构作为快速修复的一部分? [英] How to use rename refactoring as part of a quickfix?

查看:18
本文介绍了如何使用重命名重构作为快速修复的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 DSL 中添加了一个 quickfix 选项,我想在其中对文档文本进行一些修改 - 包括重命名某些元素.我可以很好地更改该元素中的文本,但我还想重命名其所有引用 - 即重命名重构.我该怎么做?

I've added a quickfix option to my DSL in which I want to make some modifications to the document text - including renaming some element. I can change text in that element just fine, but I want to also rename all of its references - i.e. rename refactoring. How do I do that?

我能否以某种方式从快速修复程序内部触发内置的重命名重构?或者,我如何查看元素的所有引用并更改它们?

Can I somehow trigger the built-in rename refactoring from inside a quickfix? Or alternatively, how do I go over all of the element's references and change them?

推荐答案

于是,我找到了一种以编程方式触发重命名重构的方法.我不知道这是否是正确"的方式 - 我猜不是,因为我必须将 @SuppressWarnings("restriction") 添加到我的代码中 - 但它有效:

So, I found a way to programmatically trigger a rename refactor. I don't know if it's the "proper" way - I guess it isn't, since I had to add @SuppressWarnings("restriction") to my code - but it works:

private void performDirectRenameRefactoring(EObject object, String newName) throws InterruptedException {
    XtextEditor editor = EditorUtils.getActiveXtextEditor();
    IRenameElementContext renameContext = new IRenameElementContext.Impl(
        EcoreUtil.getURI(object),
        object.eClass(),
        editor,
        editor.getSelectionProvider().getSelection(),
        null);
    IRenameSupport rename = renameSupportFactory.create(renameContext, newName);
    rename.startDirectRefactoring();
}

所以要从快速修复中调用它,您需要做的就是获取 EObject 并计算新名称.如果问题占据了 EObject 本身的一部分,则可以通过以下方式检索该对象:

So to call this from a quick fix, all you need to do is to get the EObject and calculate the new name. If the issue occupies a part of the EObject itself, the object could be retrieved by:

private EObject findObject(IXtextDocument doc, final Issue issue) {
    EObject object = doc.readOnly(new IUnitOfWork<EObject, XtextResource>() {
        public EObject exec(XtextResource state) throws Exception {
            return state.getEObject(issue.getUriToProblem().fragment());
        }
    });
}

您可以从 IssueResolutionAcceptor(如果您正在处理问题时应该拥有)或从 IModificationContext(其中如果您提议更改,则应该有.

You can get an IXtextDocument from either IssueResolutionAcceptor (which you should have if you're handling an issue) or from IModificationContext (which you should have if you're proposing a change).

这篇关于如何使用重命名重构作为快速修复的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆