Sample code @ github: Different ways to run code on startup.
Application startup
It happens quite often that some code must be run as soon as the plugin is loaded. There are several ways to achive this.
If you need init & dispose methods, then implement an application component. If you don’t need dispose functionality, then a preloading activity could be implemented. If you need to access the current project in that code, please look below for further explanations.
Application components
An application component provides hooks to run code on application startup and shutdown. Make sure that you don’t block the IDE by performing time-consuming actions.
Application components of your plugin are always loaded, even if no other component is actually using it. If init-on-demand is possible you should implement an application service.
If you would like to run long-running actions but don’t need to dispose on shutdown you could do this by implementing a
preloadingActivity
extension instead.
|
|
|
|
Links
- MyApplicationComponent.java
- Sample code on github
- ApplicationComponent.java
- The interface definition as provided by JetBrains
Preloading activity extension points
If you would like to run some time-consuming or possibly blocking background activity on startup you could implement the preloadingActivity
extension point.
Implementations of PreloadingActivity
are executed in a separate thread in IntelliJ, one at a time. A ProgressIndicator
is available to make sure that the application has not been shutdown.
|
|
|
|
Links
- MyPreloadingActivity.java
- Sample code on github
- PreloadingActivity.java
- The interface definition of the preloadingActivity extension.
- Preloader.java
- IntelliJ’s class which calls all available implementations of
preloadingActivity
.
Application shutdown
To execute code on application shutdown implement the disposeComponent
method of an application component.
Project initialization
The application is only initialized once, but there may be multiple projects open at the same time. It’s also possible that no project is opened, e.g. on first startup. If you need access to the current project you must implement either a project component or a startup activity extension.
If you need to dispose data after a project is closed you have to implement a component. If just the initialization is needed then a startup activity extension is enough.
Project components
A project component is similar to an application component. It is created when a new project is opened and disposed after the project is closed.
The current project is available by dependency injection as constructor parameter. You can add other application and/or project components as constructor parameters.
|
|
|
|
Links
- MyProjectComponent.java
- Sample code on github
- ProjectComponent.java
- Interface definition of a project component
Startup activity after a project was opened
If you would like to run some code after a project has been opened you could implement the postStartupActivity
extension point.
If your extension implements the DumbAware
interface, then the runActivity
method is run just after the project was opened.
If your extension doees not implement that interface, then runActivity
is delayed until the dumb mode is finished, i.e. as soon as
the background indexing of IntelliJ is done.
Be careful though! I don’t see any guarantee that the execution is done in the background, so take extra care that you don’t block the whole application. IntelliJ’s own implementations of this extension point usually trigger some more background action or show a notification after the project has been opened.
|
|
|
|
Links
- MyPostStartupActivity.java
- Sample code on github
- StartupActivity.java
- Interface definition of the StartupActivity extension
- ProjectManagerImpl.java
- IntelliJ’s implementation where all postStartupActivity extension points are executed
Project shutdown
Implement the disposeProject
method of your ProjectComponent
to execute some code after a project has been closed.