/live-plugin

IntelliJ plugin for writing plugins at runtime

Primary LanguageGroovy

Build Status

LivePlugin

This is a plugin for IntelliJ IDEs to write plugins at runtime. It uses Groovy as main language and has experimental support for Kotlin.

To install search for "LivePlugin" in IDE Preferences -> Plugins -> Browse Repositories. See also plugin repository page.

demo

Why?

There is great Internal Reprogrammability blog post on this topic by Martin Fowler.

The reasons LivePlugin exists are along the same lines:

  • minimal setup to start writing plugin. LivePlugins can always be edited/executed in LivePlugin toolwindow regardless of the current project in IDE. This takes much less effort than creating new project configured for plugin development.
  • fast feedback loop. LivePlugins are run in the same JVM instance, so there is no need to restart IDE. Typical plugin development involves starting new instance of IDE and restarting it on most code changes. (There is a caveat that some parts of IDE API are not designed to be reconfigured without JVM restart. This is unfortunate and will hopefully change sometime in the future.)
  • customizable IDE. Majority of the development tools are difficult to customize. This plugin is an attempt to improve the situation.

Practical use cases:

  • prototyping of IntelliJ plugins
  • experimenting with IntelliJ API
  • project-specific workflow automation
  • integrating shell scripts with IDE

Plugin Examples

Hello world:

import static liveplugin.PluginUtil.show
show("Hello world") // shows balloon message with "Hello world" text

Insert New Line Above Action:

import com.intellij.openapi.actionSystem.AnActionEvent
import static liveplugin.PluginUtil.*

// This action inserts new line above current line.
// It's a follow-up for these posts:
//   http://martinfowler.com/bliki/InternalReprogrammability.html
//   http://nealford.com/memeagora/2013/01/22/why_everyone_eventually_hates_maven.html
// Note that there is "Start New Line Before Current" action (ctrl + alt + enter) which does almost the same thing.

registerAction("InsertNewLineAbove", "alt shift ENTER") { AnActionEvent event ->
	runDocumentWriteAction(event.project) {
		currentEditorIn(event.project).with {
			def offset = caretModel.offset
			def currentLine = caretModel.logicalPosition.line
			def lineStartOffset = document.getLineStartOffset(currentLine)

			document.insertString(lineStartOffset, "\n")
			caretModel.moveToOffset(offset + 1)
		}
	}
}
show("Loaded 'InsertNewLineAbove' action<br/>Use 'Alt+Shift+Enter' to run it")

How to start writing plugins

  • open Plugins tool window
  • select one of the plugin entries in the panel
    (entries are folders, and plugin.groovy are startup scripts for plugins)
  • click Run icon to execute plugin (or use keyboard shortcuts alt+C, alt+E or ctrl+shift+L)

If the above worked fine:

  • modify plugin.groovy and rerun plugin to see results
  • add built-in plugin examples and experiment with them (in Plugins toolwindow header + button -> Examples)

If something doesn't work, report an issue.

(To use alt+... shortcuts on OSX you might need a workaround, please see this wiki page .)

The main idea

LivePlugin basically runs Groovy code in JVM. Conceptually it's quite simple:

ClassLoader classLoader = createClassLoader(ideClassloader, ...);
GroovyScriptEngine scriptEngine = new GroovyScriptEngine(pluginFolderUrl, classLoader);
scriptEngine.run(mainScriptUrl, createGroovyBinding(binding));

This means that your code is executed in the same environment as IDE internal code. You can use any internal API and observe/change state of any object inside IDE. There are some limitations of course, like final fields and complex APIs not designed to be re-initialized.

To simplify usage of IntelliJ API for practical purposes some parts of IntelliJ API are wrapped in PluginUtil class. This is essentially a layer on top standard IntelliJ API. If you find yourself writing interesting IDE scripts, feel free to create pull request or send a gist to include your code into PluginUtil. This is experimental API and there is no intention to keep it minimal. PluginUtil is not required though and you can always use IntelliJ classes directly.

Also note that:

  • plugins are evaluated with new classloader on each run
  • plugins are stored in $HOME/.$INTELLIJ_VERSION/config/live-plugins (on Mac $HOME/Library/Application Support/IntelliJIdea15/live-plugins) Note that you can use ctrl+shift+C shortcut to copy file/folder path.
  • if available, Groovy library bundled with IDE is used

Misc tips

  • if your plugins are stable enough, you can enable Settings -> Run All Live Plugins on IDE Startup option. If some of the plugins are not meant to be executed at startup, add if (isIdeStartup) return statement at the top.
  • it helps to have JetGroovy plugin installed (only available in IDEs with Java support)
  • you can get auto-completion and code navigation in plugins code
    • install/enable Groovy plugin
    • Plugin toolwindow -> Settings -> Add LivePlugin and IDE Jars to Project
      (adding jars unrelated to your project is a hack but there seems to be no major problems with it)
  • it helps to be familiar with IntelliJ API
  • when plugin seems to be big enough, you can move it to proper plugin project and still use live plugin See liveplugin as an entry point for standard plugins.

More examples

Similar plugins

The idea of running code inside IntelliJ is not original. There are/were similar plugins:

Contributing

Please see CONTRIBUTING.md.