Howardism Musings from my Awakening Dementia
My collected thoughts flamed by hubris
Home PageSend Comment

Groovy Monkey

The Eclipse Monkey has been an absolute life-saver since integrating it into my daily Eclipse development life. Often, I find I need to change my source code in a complex, but repetitive manner where a single regular expression search/replace just won't cut it.

However, I have also recently been getting into Groovy. A fab language that is a fusion of Java and Lisp… essentially Java on "Lisp Steroids" (Lispoids, perhaps?). So, the Groovy Monkey looks really intriguing, however, if I thought the documentation on the Eclipse Monkey was bad, the Groovy Monkey is even worse.

So… I've been delving into the Eclipse API, and have put together a script where you can write Groovy code to modify the text in your editor, and thought I would share in such bountiful wisdom.

From my previous discussion, it is clear that what we need are the following four pointers:

  1. A reference to the current editor
  2. A reference to the selective text position or current cursor position
  3. Ability to grab the text at this point
  4. Ability to change the text at this point

So, let's make a little script that will uppercase some text. The first thing you need to realize is that the Groovy Monkey is closer to the Eclipse iron, and in order to get access to the editor, and the second key issue is that the comments at the top are much more important. To make a long story longer, make sure you put the following comments at the top:

/*
 * Menu: Examples > Uppercase Text
 * Script-Path: /website.howardism/datafiles/howardism/Technical/Eclipse/uppercase_text.gm
 * Kudos: Howard Abrams (http://www.howardism.org)
 * License: EPL 1.0
 * Job: UIJob
 * Exec-Mode: Foreground
 * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
 */

Some notes about this. The UIJob is important if you attempt to access anything in the UI … like the editor and its text. If we were doing something lengthy, we really ought to be create a background thread, but for simple cases, just associating our script with the UI thread works fine.

If you don't, you will end up with the following error:

exception from Groovy: org.eclipse.swt.SWTException: Invalid thread access

Reason:
  exception from Groovy: org.eclipse.swt.SWTException: Invalid thread access

Yeah, probably not the most explanatory of all error messages, but still…

First, let's get a reference to the goods through the Eclipse API:

def editor = window.activePage.activeEditor
def source = editor.sourceViewer.document

The source variable has a get() method that returns the entire text of the editor, as it returns the IDocument interface.

Next, let's get the range. The offset property is the start of the selected text, but if no text is selected, then it returns the position of the current cursor position. The length property returns the number of characters selected or 0 if nothing is selected:

def range = window.activePage.selection

Let's get the text by using the alternative get method:

def text = source.get( range.offset, range.length )

We can now do anything we want to the text … analyze it, string it up, and convert it to our religion. In our case, we are going to do something pretty simple:

if ( text =~ /^[A-Z]/ )
    text = text.toLowerCase()
else
    text = text.toUpperCase()

Lastly, we write our new text back into the editor. To make this truly robust, we should check to see if the range.length is 0 and then search for the next space or something and convert the current word … but I'll leave that as an exercise to the reader.

Here is the script for you to play with.

Tell others about this article:
Click here to submit this page to Stumble It