Intellij IDEA插件开发:无法获取IntentionAction的进度指示器

时间:2018-04-30 18:43:14

标签: java user-interface intellij-idea intellij-plugin

我想在我的自定义IntellIJ IDEA插件中运行自定义IntentionAction时显示进度条。

然而,无论我做什么,都不会显示。为了测试问题是否在IntentionAction中,我将代码复制粘贴到AnAction的简单实现中。全班看起来像这样:

    public class HelloAction extends AnAction {
    public HelloAction() {
        super("Hello");
    }

    public void actionPerformed(AnActionEvent event) {
        Project project = event.getData(CommonDataKeys.PROJECT);
        ProgressManager.getInstance().run(new Task.Modal(project, "daf", false) {
            public void run(ProgressIndicator indicator) {
                indicator.setText("This is how you update the indicator");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {}
                indicator.setFraction(0.5);  // halfway done
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {}
            }
        });
        Messages.showMessageDialog(project, "Hello world!", "Greeting", Messages.getInformationIcon());
    }
}

它完美无缺。当我在IntentionAction中使用相同的代码时,不会显示任何内容。

public class GenerateIntentionAction extends PsiElementBaseIntentionAction implements IntentionAction {
...
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
        ProgressManager.getInstance().run(new Task.Modal(project, "daf", false) {
            public void run(ProgressIndicator indicator) {
                indicator.setText("This is how you update the indicator");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {}
                indicator.setFraction(0.5);  // halfway done
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {}
            }
        });

我尝试拨打runWithProgressSynchronously而不是run,尝试制作Task ModalBackgroundable - 无济于事。除了ProgressIndicator内的IntentionAction总是EmptyProgressIndicator

之外,我不知道出了什么问题

1 个答案:

答案 0 :(得分:0)

如果在WriteAction内调用了您的意图操作,则无法从那里显示模态UI。覆盖startInWriteAction并返回false可能会有所帮助。