[Intellij IDEA Plugin Development]How to force return in AnAction? Follow
Answered
Hello!
I am developing an IDEA Plugin. My AnAction has to check some preconditions and if not match any preconditions, the AnAction will force return and exit. The code like this:
class MyAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
Preconditions.check(e)
// do something useful.
}
}
object Preconditions {
fun check(e: AnActionEvent) {
if (!match) {
// force return MyAction
throw IllegalArgumentException("Preconditions not match!")
}
}
}
My code is work but it so ugly...How to force return in AnAction in a better way? Thank you!
Best Regard
Please sign in to leave a comment.
Not sure I understand the problem, why not simply "return;" if the action should not run?
Yann Cebron Appreciate for your reply! Maybe I didn’t describe the problem clearly. My Plugin has to check the code in current PsiFile. Because the code checking is complex, the method calling will be very deep. if the deepest method find that there is something not meet expectations, I want to stop the AnAction execution. One way to stop the execution of AnAction is add return on the method layer by layer, Other way is throw an exception at the deepest method. These two methods are not elegant and I want to find a correct way to stop the execution of AnAction elegantly. Could you give me some suggestion on this problem? Thank you!
You should run such long actions via ProgressManager API then. It will show progress bar, allow showing progress % and messages and allow user to interrupt process.
Yann Cebron Appreciate for your helpful answers! I will try to use ProgressManager to solve my problem.