IntelliJ plugin for writing IntelliJ plugins at runtime ⚡️
Plugin for IntelliJ-based IDEs to create plugins at runtime
using Kotlin and Groovy.
To install search for "LivePlugin" in IDE Preferences -> Plugins -> Marketplace
or use the "Install" button on the Plugin Marketplace website.
Hello world (Groovy):
import static liveplugin.PluginUtil.show
show("Hello world") // Shows balloon notification popup with "Hello world" text
Insert New Line Above action (Kotlin):
import com.intellij.openapi.actionSystem.AnActionEvent
import liveplugin.*
// Action to insert a new line above the current line.
// Based on this post https://martinfowler.com/bliki/InternalReprogrammability.html
// Note that there is also built-in "Start New Line Before Current" action (ctrl+alt+enter).
registerAction(id = "Insert New Line Above", keyStroke = "ctrl alt shift ENTER") { event ->
val project = event.project ?: return@registerAction
val editor = event.editor ?: return@registerAction
executeCommand(editor.document, project) { document ->
val caretModel = editor.caretModel
val lineStartOffset = document.getLineStartOffset(caretModel.logicalPosition.line)
document.insertString(lineStartOffset, "\n")
caretModel.moveToOffset(caretModel.offset + 1)
}
}
show("Loaded 'Insert New Line Above' action<br/>Use 'ctrl+alt+shift+Enter' to run it")
Make sure "hello world" works fine:
Plugins
tool window select "hello-world" plugin and click "Run" button to execute the plugin (Run Plugin
action with ctrl+shift+L
or alt+C, alt+E
shortcut). It should display a message.plugin.groovy
/plugin.kts
and rerun the plugin.
On the second run, the previous version of the plugin will be unloaded before the code is evaluated again.plugin.groovy
/plugin.kts
file again so that it fails to compile/run.
You should see an error message in the Run
tool window.plugin.groovy
or plugin.kts
scripts as entry points.
This means that you can, for example, copy the path to the plugin folder using the Copy Path
action (ctrl/cmd+alt+C
shortcut).Try bundled examples:
Plugins
tool window click the "Plus" button (Add Plugin
action) to add Kotlin or Groovy examples.Take a look at settings in the Plugins
toowindow:
Run Plugins on IDE Start
— run all plugins on IDE start.Run Project Specific Plugins
— run all plugins in .live-plugins
project directory when
the project is opened and unload them when the project is closed.Add LivePlugin and IDE Jars to Project
— useful for Groovy plugins
to get auto-completion and code navigation in plugin code.
(Adding jars unrelated to your project is a bit of a hack but there seems to be no major problems with it.)
Note that Kotlin plugins should have auto-completion and code navigation without it.Learn more about IntelliJ API:
Once your plugin has grown, you can move it to a proper plugin project still using live plugin for reloading and maybe then convert it to become a dynamic plugin.
If something doesn't work or doesn't make sense, please feel free to ask
in #plugin-live-plugin
channel on Jetbrains platform slack
or report an issue
(it's ok to report an issue even if it's just a question).
Overall, the idea is just to load and run plugin Groovy or Kotlin classes in the IDE JVM at runtime. More specifically the steps are:
pluginDisposable
from previous execution, then dispose it (on EDT)This means that plugin code can use any internal IDE API and observe/change IDE state.
There are some limitations of course, such as final
fields and IDE APIs which are not designed to be re-initialized.
Most IntelliJ-based IDEs come with a bundled Groovy jar which is used for loading and running live plugins (otherwise, the groovy-all jar will be downloaded). LivePlugin uses its own version of Kotlin stdlib and compiler because the version bundled with IDEs changes quite often and seems to be harder to rely on.
The idea of running code inside IntelliJ is not original. There are/were similar plugins:
Please see CONTRIBUTING.md.