8000 GitHub - mub/live-plugin: IntelliJ plugin for writing plugins at runtime
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mub/live-plugin

 
 

Repository files navigation

LivePlugin

This is IntelliJ plugin for writing IDE plugins at runtime in Groovy. (See also plugin repository page.)

demo

Why?

It's difficult to explain better than Martin Fowler does in this blog post but in short:

  • to make writing plugins easier. There is no need to set up and configure a separate project.
  • faster feedback loop. There is no need to start new IDE instance to run a plugin. If you change plugin code, there is no need to restart IDE.
  • great goodness of customized IDE. In a way even Excel can be "customized" at runtime with VB script. This is an attempt to fix this and have easy-to-extend IDE.

Example plugin

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")

See also Scala plugin example and Clojure plugin example.

How to install

Through IntelliJ plugin manager. In "Preferences -> Plugins -> Browse Repositories" search for "liveplugin". Alternatively, download LivePlugin.zip and use "Install plugin from disk".

How to start writing plugins

  • open "Plugins" tool window on the right side
  • select "helloWorld" plugin and press "alt + C, alt + E" to execute it ("plugin.groovy" are plugin entry points)
  • add plugin examples and experiment with them

Advanced usage

  • it helps to be familiar with IntelliJ API (e.g. look at plugin development wiki page). Some parts of it which seem to be useful for small plugins are in PluginUtil class. Even if you don't use it, it might be a good place to look up bits of IntelliJ API.
  • it helps to have JetGroovy plugin installed (available only for IntelliJ IDEA).
  • you can get auto-completion in plugins code by adding IDEA and LivePlugin jars to project (in "Settings" drop-down at the top of "Plugins" tool window). This is a bit of a hack, but seems much easier than setting up a new project for every tiny experiment.
  • get IntelliJ source code, find out how your favorite feature is implemented, steal the code and adapt it for your needs
  • if your plugins are stable enough, you can enable "Settings -> Run All Live Plugins on IDE Startup" option. If some of them are not meant to be executed at startup, add "if (isIdeStartup) return" statement at the top.
  • when plugin seems to be big enough, you can create a separate project for it but still use live plugin for loading. Or you can use liveplugin with existing plugins, the only thing is to make it reloadable. See liveplugin as an entry point for standard plugins.

More examples

How this plugin works?

It just runs your code in JVM, like this:

GroovyScriptEngine scriptEngine = new GroovyScriptEngine(pluginFolderUrl, classLoader);
scriptEngine.run(mainScriptUrl, createGroovyBinding(binding));
  • each plugin is evaluated with its own classloader
  • it uses Groovy bundled with IntelliJ
  • plugins are stored in "$HOME/.$INTELLIJ_VERSION/config/live-plugins" (on Mac "$HOME/Library/Application Support/IntelliJIdea12/live-plugins"). You can also use standard "ctrl + shift + C" shortcut to copy file/folder path.

Similar plugins

The idea of running code inside IntelliJ is not original. There are similar plugins (although I wasn't too happy with them):

It would be interesting

  • to try writing a language plugin
  • to have nice object tree pattern-matching API for Groovy (can be good for writing inspections/intentions to match/replace syntax tree).
  • add more languages, e.g. Ruby, Kotlin or Java.

Contributing

Please see CONTRIBUTING.md.

About

IntelliJ plugin for writing plugins at runtime

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 60.5%
  • Groovy 38.2%
  • Other 1.3%
0