Search code examples
javauser-interfaceintellij-ideaintellij-plugin

Intellij IDEA plugin development: Cannot get progress indicator for IntentionAction


I would like to display a progress bar when running a custom IntentionAction in my custom IntellIJ IDEA plugin.

However, it is not displayed no matter what I do. To test whether the problem lies in the IntentionAction, I copy-paste the code to a simple implementation of an AnAction. The whole class looks like so:

    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());
    }
}

And it works perfectly. And when I use the same code inside an IntentionAction, nothing is displayed.

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) {}
            }
        });

I tried to run call runWithProgressSynchronously instead of run, tried making Task Modal and Backgroundable - to no avail. I do not know what is wrong besides the fact that ProgressIndicator inside IntentionAction is always an EmptyProgressIndicator


Solution

  • If your intention action is invoked inside WriteAction, you can't show modal UI from there. Overriding startInWriteAction and returning false might help.