krasa / intellij_eval

IntelliJ plugin for writing simple plugins in Groovy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

toolwindow

What is this?

This is experimental IntelliJ IDEA plugin for writing plugins in Groovy at runtime
(or running Groovy code inside IntelliJ).

Why?

  • it should be possible to write a simple plugin without setting up new project
  • plugins source code should be easily available and editable
  • time between writing code and seeing how it works should be short

Example plugin

import com.intellij.openapi.actionSystem.AnActionEvent
import static intellijeval.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 install

Through IntelliJ plugin manager. Search for "eval". (Just in case this is the plugin page.)

How to start

  • 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

It might be useful

  • to have auto-completion by adding IDEA and IntelliJEval jars to project (can be done in "Settings" drop-down at the top of "Plugins" tool window).
  • install Groovy plugin
  • look at PluginUtil class
  • get IntelliJ source code

More examples

Under the hood

  • this is essentially a host plugin for other plugins
  • each plugin is evaluated with its own classloader using GroovyScriptEngine
  • it uses Groovy bundled with IntelliJ (v1.8.5 at the moment)
  • plugins are stored in "$HOME/.$INTELLIJ_VERSION/config/intellij-eval-plugins" (on Mac "$HOME/Library/Application Support/IntelliJIdea12/intellij-eval-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:

It would be interesting

  • to have nice object tree pattern-matching API for Groovy (can be good for writing inspections/intentions to match/replace syntax tree). Or may be there is one and I just don't know about it.
  • use another language (e.g. Scala or Ruby).
  • go meta! Rewrite this plugin as its own plugin. This is really how it was started (loads of fun with classloaders). The old meta-version was too broken to be released and two years later was replaced with this.

About

IntelliJ plugin for writing simple plugins in Groovy

License:Other


Languages

Language:Java 64.9%Language:Groovy 33.9%Language:Ruby 1.2%