-
-
Notifications
You must be signed in to change notification settings - Fork 67
Scripting a macros
Dmitry Kandalov edited this page Sep 2, 2021
·
14 revisions
Almost all user actions in IntelliJ are implemented as instances of
AnAction
class (including basic things like moving cursor or deleting characters).
This means it is possible to simulate user actions in a script
similar to recording a macros (Main Menu -> Edit -> Macros
) but in a more flexible way.
There are several ways to find existing actions in IDE:
import static liveplugin.PluginUtil.*
import static liveplugin.implementation.ActionSearch.allActionIds
if (isIdeStartup) return
show(allActions().findAll { it.class.simpleName.contains("ToolWindow") })
show(allActionIds().findAll { it.contains("ToolWindow") })
def action = actionById("ActivateProjectToolWindow")
// or ActionManager.instance.getAction("ActivateProjectToolWindow")
// Once you have instance of an action, you can invoke it like this
action.actionPerformed(anActionEvent())
As an example of a non-linear macro, here is an automated version of Null Pointer Driven Development enterprise best practice (to be clear, this is sarcasm):
- write some enterprise code
- run it and wait for NPE
- stop the process and fix NPE
- write some more enterprise code
import static liveplugin.PluginUtil.*
if (isIdeStartup) return
doInBackground {
registerConsoleListener("NPEConsoleListener") { consoleText ->
if (consoleText.contains("NullPointerException")) {
invokeOnEDT { actionById("Stop").actionPerformed(anActionEvent()) }
show("The process was stopped because of NullPointerException")
unregisterConsoleListener("NPEConsoleListener")
}
}
invokeOnEDT { executeRunConfiguration("VeryEnterpriseProjectMain", project) }
}