personal experiences and code :)

Sunday, September 16, 2007

productivity with dash in eclipse

by now, most people should have heard bout Dash nee Eclipse monkey, released with europa, which allows you to script your eclipse-based ides with javascript (more languages to come), to perform common/repetitive tasks.

I am writing some flex components, and the mxml and actionscript tends to be fairly generic; for instance, if you are creating components, and going by the 'standard' way of doing things, you are going to create some properties that have public getters and setters as the property names, and those same property names starting with (cringe) underscores as the member variable names.

(or if this is Java, you write a few getFoo, setFoo methods on all foo member variables; the JDT has a "Generate Getters and Setters" menu option for this, if you are editing Java code)

What I normally do is create my variable names and their return types, select them, and use a find replace (with regex) to generate the getters and setters.

so if I want to generate x, y, width, height for my component, I will type:


select them, and do a search/replace as mentioned above. This is the type of job that I should clearly ship off to eclipse monkey. I want monkey to give me this output:

        public function get x():Number {
return _x;

public function set x(o:Number):void {
_x = o;

public function get y():Number {
return _y;

public function set y(o:Number):void {
_y = o;

public function get width():Number {
return _width;

public function set width(o:Number):void {
_width = o;

public function get height():Number {
return _height;

public function set height(o:Number):void {
_height = o;

protected var _x:Number;
protected var _y:Number;
protected var _width:Number;
protected var _height:Number;

Advantages of using Dash include:

- No more find/replace dialog box.
- Quick shortcut key to apply this same transformation over and over again, quickly.
- Code will be easy to update, should I desire changes to the formatting of the output.
- If other people in a team have monkey installed, you can share scripts
- ...

To install and use dash (I am doing this in standalone flex builder 2):

  1. add the remote update url; update and restart.
  2. when you restart you should see a Scripts menu item. monkey works.
  3. Create a project, called Monkey
  4. Create a scripts folder under your monkey project
  5. Create a new javascript file (with a .js extension) in your scripts folder
  6. Write you monkey script in there

Note: Dash looks for scripts in a top-level scripts folder, in an open project in your workspace. Because I am doing this in standalone Flex builder, I have just created the Monkey project as a repository for all my flex/actionscript related scripts, when using fb2. Also, under the Scripts menu item, you can click "Paste New Script", and it will create the project and scripts folder we created above.

The code below does the setter/getter transformations:
* Menu: Actionscript > Generate Properties
* Key: M3+9
* DOM:

function main()
var editor = editors.activeEditor
var source = editor.source

if (editor.selectionRange) {
var range = editor.selectionRange
var offset = range.startingOffset

var text = source.substring (offset, range.endingOffset)
var result = text.match(/(\w+:\w+)/g);

o = ""

for (var i = 0, n = result.length; i < n; ++i)
o += props(result[i])

for (var i = 0, n = result.length; i < n; ++i)
o += getvar(result[i])

o += "\n"

// debug(o)
editor.applyEdit(offset, range.endingOffset - offset, o)

function props(v) {
var parts = v.split(":");

if (2 != parts.length)
return "";

var s = "\n\t\tpublic function get " + parts[0] + "():" + parts[1] + " {";
s += "\n\t\t\treturn _" + parts[0] + ";" ;
s += "\n\t\t}\n\n";

s += "\t\tpublic function set " + parts[0] + "(o:" + parts[1] + "):void {";
s += "\n\t\t\t_" + parts[0] + " = o;" ;
s += "\n\t\t}\n";

return s;

function getvar(s) {
if (1 > s.length)
return "";

return "\n\t\tprotected var _" + s + ";";

function debug(s) {, "Monkey Debugging", s);

The Menu and Key that appear as comments in the js code are meta data for declaring the shortcut key, and menu structure for your script. So for this, you should see a "Generate Properties" menu item, under Scripts/Actionscript, and that menu Item should be bound to Alt + 9.

As you can see, you can simplify some of the cookie-cutter stuff you have to do with these monkey scripts. Hopefully, you can write some of your own, and find them increase your productivity in your day-to-day coding assignments.



Project Dash
Creating a new Eclipse Monkey script
Adding metadata to an Eclipse Monkey script