java - 如何从自定义 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
函数作为内部的 lambda调用ApplicationManager.getApplication().runWriteAction
会起作用,但显然不是。我怎样才能让它工作?
这是我使用的完整代码。
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
推荐阅读
- javascript - Chrome 扩展程序:主窗口关闭后 chrome.alarms 不起作用
- reactjs - Redux reducer 没有返回整个状态
- reactjs - 方法“props”仅用于在单个节点上运行。0 在 SingleDatePicker 中找到
- java - 我需要找到一个包含特定文本的元素。如何使文本成为变量并将其传递给 xpath 函数?
- python - Pyspark 合并两个大文本文件
- ios - 无法在 Xcode 中安装 pod。(可可豆荚)
- javascript - mac中的节点js path.join(目录+'\\'+文件)不起作用
- sql - 如何在 Oracle SQL 中将变量设置为 select 语句的结果
- python - python中数据类型“object”和dtype('O')有什么区别
- excel - VBA 找不到值