IDE scripting with Ruby and Eclipse Monkey
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.
Here a sample Eclipse Monkey script in Ruby for converting a String to a Symbol in an editor:
=beginThe 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.
Menu: Ruby > String to Symbol
Kudos: Christopher Williams
License: EPL 1.0
# 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
editor.apply_edit(selection.first, selection.size - 1, symbol)
In the above code sample, for instance, the
$editorsvariable 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_editorwould 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.
Anatole Tresch Mar 03, 2015