Extending Eclipse using JavaScript and Monkey Script engine

I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana’s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my snippet. By googling, I discovered Eclipse Monkey Script engine, that is an extremely powerful tool for every JavaScript developer who wants to extend Eclipse features by writing few lines of JavaScript code. In order to use the js engine, you must have the package org.eclipse.eclipsemonkey (which is installed by default by Aptana) installed. Unfortunately I can’t find a complete and exhaustive reference for this project, which seems forgotten by authors, so I learnt what I know reading different posts.
Basically, Monkey Script allow developers to access editor’s instance, get selected test, get document content, edit it and update it. Moreover is possible to print text to consol, read and create files on the filesystem… and finally the scripts created will be accessible from Eclipse menu (under “Scripts”) and invokable through user defined shortcut… really nice!
So, backing to my experiment, I created this monkey script:

/* 
* Key: M1+M2+C
* Menu: Custom Scripts > Wrap with double quotes
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
* Kudos: Davide Zanotti
*/
function main() {
	
	// no editor... exit
	if (typeof editors.activeEditor == 'undefined') {
		
		return alert('No active editor');
		
	}
		
	// get a reference to the editor in use
	var editor = editors.activeEditor;
	
	// beginning of text selection
	var startOffset = editor.selectionRange.startingOffset;
	
	// end of text selection
	var endOffset = editor.selectionRange.endingOffset;
	
	// selected text
    var selection = editor.source.substring(startOffset, endOffset);
	
	try {
		
		// surround selection with quotes
	   editor.applyEdit(startOffset, endOffset - startOffset, '"' + selection + '"');
		
	} catch (e) {
		
		alert('Error ' + e.code + ': ' + e.message);
		
	}
	
};

It’s important to notice some points, first you MUST write an “header” using the comment syntax “/* */” into which you MUST declare at least 2 parameter: the “Menu” path (you can nest several menu items by writing several “>”) and the “DOM” package (which allows you to access specific objects like “editors“). You can write several js functions, but you must provide a main() function, which is called automatically by Eclipse once you launch your script, otherwise you can call the main function as you like but you must then provide the “OnLoad” param in the header, specifying which function will be called.
To define a shortcut you can use the “Key” parameter and define your own keys combination by choosing among the four modifiers: M1, M2, M3, M4, that are a platform-independent way of representing keys (these stand for ALT, COMMAND, CTRL, and SHIFT). The strange (at least to me) parameter “Kudos” is used to declare the author of the monkey script.
To test my little script or create your own, you have simply to create a new project into Eclipse by calling it as you like (ie: “custom-extensions”), then create a “scripts” folder into which you will save your .js files… that’s all, try yourself and enjoy :)

This is how it looks:

eclipse monkey script menu

eclipse monkey script menu

ps: I’m going to explore the Monkey Script API in order to write more complex and useful extensions :P

  • Great post! Thanks. I managed to create a script that sorts my selected lines. I needed this for big JSON objects( when coding in ExtJS).

  • Thanks for the post. It has helped me to understand better how the engine works. Is there a way to replace the shortcuts with “keywords” like “pf” to call a snippet for a private function, for example; some way to achieve some of the functionality of the SourceMate plugin. ? Thanks in advance.