InfoQ

News

IDE scripting with Ruby and Eclipse Monkey

Posted by Werner Schuster on Oct 30, 2007 09:00 AM

Community
Ruby
Topics
Scripting ,
Dynamic Languages ,
JRuby
Tags
IDEs ,
Eclipse ,
JRuby ,
RadRails
IDEs such as Eclipse have gained a lot of users in the past by taking ideas from Smalltalk IDEs - incremental compilation and local version management being just a few of them. But one area where Smalltalk IDEs (or editors such as Emacs) still have an edge is automation of the IDE itself: scripting tedious tasks in the IDE. Where Smalltalk users just use Smalltalk to access IDE objects, Emacs users use LISP to do the same - and can do that interactively and at runtime.

Eclipse has a solution for this problem with Eclipse Monkey - the name comes from the Firefox plugin GreaseMonkey.  While the first versions used Javascript (Java-based Rhino) as scripting language, Christopher Williams announced recently that Ruby (using JRuby) is now supported as scripting language as well:
If you're unfamiliar with EclipseMonkey, it is a set of plugins for Eclipse that allows users to interact with and script the IDE using languages other than Java (initially just Javascript). For more details you may want to take a peek at http://www.eclipse.org/dash/monkey-help.php?key=writing.
The EclipseMonkey Ruby extension opens up the ability to script the IDE in Ruby code.

This initial version exposes an Editor DOM, that allows you to manipulate editors with your scripts. We already have some Help pages up on the wiki that describe the details and give more information about the API of the Editor DOM. We've also included some sample scripts that show how to use this feature.
The design of Eclipse Monkey is an exercise in simplicity - for a reason: one of the project's creators and designers was Ward Cunningham, who - among other things - came up with Wikis in the 1990s. Scripts are text files in a project; any kind of metadata is included in the file itself. Metadata can be dependencies, or a declaration of a shortcut that will run the script. This can  go as far as hooking a script up to any event in Eclipse, for instance a file change, file save or the launch of a program.

Here a sample Eclipse Monkey script in Ruby for converting a String to a Symbol in an editor:
=begin
 Menu: Ruby > String to Symbol
 Kudos: Christopher Williams
 Key: M1+Shift+;
 License: EPL 1.0
 DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.ruby
=end
 # If the current token is a string containing alnums, change it to
 # a symbol
 editor = $editors.get_active_editor
 selection = editor.selection_range
 selected_src = editor.source[selection]
 # if entire selection is a string with no dynamic content, then
 # convert the string to a symbol
 match = selected_src.match(/(['"])([_a-zA-Z][_\w]*)\1/)
 return if match.nil? || match.size != 3
 symbol = ":" + match[2]
 editor.apply_edit(selection.first, selection.size - 1, symbol)
The other concept of Eclipse Monkey is the DOM, not to be confused with the HTML or XML DOM, which stands for  Domain Object Model. It is an object that provides a simpler interface to some functionality for a particular domain.

In the above code sample, for instance, the $editors variable points to a DOM for handling everything to do with Editors. DOMs are necessary to make scripting an IDE such as Eclipse easy. It would be possible to do the same things by directly accessing Eclipse objects, systems and APIs, but the scripts would often be more complicated. This stems from the fact that the Eclipse design is very flexible, and has various concepts that make it as extensible as it is - but this means that a call such as $editors.get_active_editor would be 2-3 lines of code instead of a single expression.

The DOMs are ordinary Eclipse plugins that contribute to an extension point org.eclipse.eclipsemonkey.dom, so writing a simplified interface to functionality is very easy, given Eclipse's Plugin Development Environment (PDE). Eclipse Monkey metadata also helps with configuration management for DOMs: a script's metadata section can specify the update site for the required DOM(s), which are fetched by the system when the script runs.

Since Eclipse Monkey scripts run in the JVM of Eclipse, it's also possible to write scripts that make use of all of Java's APIs and all JRuby's standard libraries. Many Eclipse Monkey DOMs are available, and give access to the wealth of code and functionality that Eclipse and the Eclipse plugin ecosystem provide.

For instance, controlling a JVM via JMX is done with the JMX DOM. This allows to call all JMX operations or query JMX attributes with a simple script. Ideas for this would be to run an application from Eclipse, and set attributes - such as log levels - or invoke operations - such as running the Garbage Collector - in one go. Other DOMs provide access to Eclipse plugins for CVS/SVN, the Workspace, the JDT, and much more.

Further resources can be found Aptana's Eclipse Monkey site or the official pages at the Eclipse Monkey site.
Looks interesting by Sebastien Auvray Posted Oct 30, 2007 5:34 PM
  1. Back to top

    Looks interesting

    Oct 30, 2007 5:34 PM by Sebastien Auvray

    At least some interesting feature Intellij Idea is missing from Eclipse!

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.