如何从自定义 ToolWindow 调用 IntelliJ 重命名重构?

托马斯

我正在开发一个 IntelliJ 插件,它有自己的工具窗口。当用户按下某个键时,该插件应调用 IntelliJ 的内置重命名重构函数来重命名变量。当我运行我的示例时,当我按下一个键来调用重命名重构函数时会抛出以下异常:

2020-05-16 23:03:17,741 [  41062]  ERROR - llij.ide.plugins.PluginManager - EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@574ed46a
Current thread: Thread[JavaFX Application Thread,6,Idea Thread Group] 648578026
Write thread (volatile): Thread[AWT-EventQueue-0,6,Idea Thread Group] 807407917com.intellij.openapi.diagnostic.Attachment@339b1167 
com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@574ed46a
Current thread: Thread[JavaFX Application Thread,6,Idea Thread Group] 648578026
Write thread (volatile): Thread[AWT-EventQueue-0,6,Idea Thread Group] 807407917com.intellij.openapi.diagnostic.Attachment@339b1167
    at com.intellij.openapi.application.impl.ApplicationImpl.assertIsWriteThread(ApplicationImpl.java:1068)
    at com.intellij.openapi.application.impl.ApplicationImpl.startWrite(ApplicationImpl.java:1154)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:974)
    at MyToolWindowFactory.handle(MyToolWindowFactory.java:55)
    at MyToolWindowFactory.handle(MyToolWindowFactory.java:17)

我认为setName在内部ApplicationManager.getApplication().runWriteAction函数作为 lambda调用会起作用,但显然不是。我怎样才能让它工作?

这是我使用的完整代码。

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.psi.*;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class MyToolWindowFactory implements ToolWindowFactory, EventHandler<KeyEvent> {

    PsiField[] variables;

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        final JFXPanel fxPanel = new JFXPanel();
        JComponent component = toolWindow.getComponent();

        ApplicationManager.getApplication().invokeLater(() -> {
            PsiClass currentClass = getCurrentClass(project);
            variables = currentClass.getFields();

            Scene scene = new Scene(new VBox(), 400, 250);
            scene.setOnKeyPressed(this);
            fxPanel.setScene(scene);
            fxPanel.requestFocus();
        });

        component.getParent().add(fxPanel);
    }

    private PsiClass getCurrentClass(Project project) {
        // Get the currently selected file.
        FileEditorManager manager = FileEditorManager.getInstance(project);
        VirtualFile[] files = manager.getSelectedFiles();
        VirtualFile currentFile = files[0];

        // Get the PsiClass for the currently selected file.
        PsiFile psiFile = PsiManager.getInstance(project).findFile(currentFile);
        PsiJavaFile psiJavaFile = (PsiJavaFile)psiFile;
        final PsiClass[] classes = psiJavaFile.getClasses();

        return classes[0];
    }

    @Override
    public void handle(KeyEvent event) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            variables[0].setName("newVariableName");
        });
    }
}
托马斯

让它工作的一种方法是从WriteCommandAction.runWriteCommandAction方法中调用重命名函数此外,调用setName仅重命名变量声明。为了重命名对变量的所有引用,我首先使用该ReferencesSearch.search方法搜索所有变量的引用,然后调用handleElementRename每个引用。

public void handle(KeyEvent event) {
    Runnable r = ()->
    {
        for (PsiReference reference : ReferencesSearch.search(variables[0])) {
            reference.handleElementRename(variableName);
        }
        variables[0].setName(variableName);
    }
    WriteCommandAction.runWriteCommandAction(project, r);
}

WriteCommandAction从这里得到使用提示https : //intellij-support.jetbrains.com/hc/en-us/community/posts/206754235

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何让我在的IntelliJ的自定义自动完成建议?

如何从Linux内核空间(即从自定义系统调用)添加自定义扩展属性

如何使用IntelliJ重构重命名Maven PomartifactId?

如何使IntelliJ颜色预览识别自定义Color类?

如何在Intellij Idea 12中重命名/移动项目?

IntelliJ IDEA:如何重命名项目(在“最近的项目”列表中)?

如何在六个中添加自定义重命名?

在PyCharm中重命名(shift-F6)后,如何使用热键访问“重构”按钮?

如何在IntelliJ IDEA中自定义SonarLint规则?

Intellij IDEA:进行重构>重命名不会选择整个单词

VS代码:如何在Angular模板文件中重构(重命名变量)?

在Intellij IDEA异常断点条件中调用自定义函数

Intellij重构,禁用/自定义生成“ @ org.jetbrains.annotations.NotNull”

Intellij插件开发:在不同项目窗口中打开时,toolWindow扩展为空白

IntelliJ插件:如何使用自定义FrameTitleBuilder?

使用gPodder下载播客后,如何自定义重命名播客?

重构重命名在Intellij IDEA中被破坏

如何在Eclipse C ++中重构/重命名宏

XTEXT 2自定义重命名重构示例

自定义pycurl调用

如何命名自定义(重命名)Info.plist的本地化文件?

调用IContextMenu时如何处理重命名操作?

如何调用自定义Java数组

使用自定义npm脚本重命名文件

如何重命名 React 组件_并且_也重构了“导入”?

你能自定义 IntelliJ 如何在方法声明和方法调用上自动格式化换行吗?

如何使用自定义的“字符串”重命名 x 标签?

如何创建对象的子集,重命名一些键然后重构它?

如何在visual studio ide扩展自定义中调用自定义函数?