Marcin has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

Scripting Eclipse with the Monkey Project: An Introduction

02.04.2008
| 50196 views |
  • submit to reddit

Your own script

You are ready to write your very first script. Here is the idea: Whenever I'm stuck while coding I use one of code search websites to find examples, related to subject I'm working on. We will use Koders website. These are goals for the script:

  • take current editor's selection as an input
  • search at Koders website for selected phrase
  • show the result in Eclipse view.
  • be available in "Scripts" menu, as > Search > Google Code item.

We start with script's header. This is what we need:

/* 
* Menu: Search > Koders
* Kudos: OnJava.com
* License: EPL 1.0
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
*/

Store the script in a file your workspace. Create a new project named "OnJavaMonkey" and new "scripts" folder within. Create new file CodersSearch.js beneath and paste lines above. Go and check "Scripts" menu, similar items should be there:

 

 

 

 

 


 

 

 

 

 

Figure 5. Example script's location.

Monkey plug-in reads open projects, and checks for "scripts" folder. Any file with js extension is read, parsed and placed in the menu. Script is available but not runnable. We add the function main:

 

function main() 
{
var sourceEditor = editors.activeEditor;

var range = sourceEditor.selectionRange;
var deleteLength = range.endingOffset - range.startingOffset;
var selection = sourceEditor.source.substring(range.startingOffset,range.endingOffset);

webView = views.getView("KodersWebView");
webView.showView(true);
webView.setTitle("Koders");
webView.url = "http://www.koders.com/default.aspx?btn=Search&la=*&li=*&s="+selection;
}

Now I want you to open a new editor (New > Untitled Text File) and type String.substring. Select what you have typed and run the script from Search > Koders; menu.

 

 

 

 

 

 

 

 

 

 

 


Figure 6. Search result.

The result shouldn't be much different from what you see on the screen shot: List of all "String.substring"occurences in all code indexed by Koders

Sharing scripts

Scripts may be shared very easily. When you publish on your blog or a wiki page, anyone may copy and paste script into his workspace. There is only one rule you need to know. Scripts have to be enclosed by following Jabberwocky-inspired lines:

  --- Came wiffling through the eclipsey wood ---
... script goes here ...
--- And burbled as it ran! ---}

Once you remember that, you are free to share your scripts. For your convenience there is "Copy for publication" menu available when you right click on a script inside the project navigator.

 

 

 

 

 

 

Figure 7. Scripts publishing made easy.

You may want to check the following scripts marketplace: http://monkey.brain-bakery.com/. This is the website when you can easily publish your work and search through others submissions.

GroovyMonkey

GroovyMonkey is a fork project started by James E. Ervin in May 2006. The project was initially created to test support of many additional languages: BeanShell, Python, Groovy and Ruby. Today it is more like test bed for future features, here is some of them:

  • contribution to outline view, giving you a handy tree view with a list of objects available in your scripts
  • enhanced header fields giving you more control over script execution (e.g. you may mark your script as UI Job)
  • additional DOM trees


 

 

 

 

 

 

 

 

 

 

 

Figure 8. GrooveMonkey in action.

The set of GroovyMonkey features is far more than just a few tweaks, it deserves own article. This is definitively a project you want to try. Just remember to uninstall or disable the Monkey, before you start installing GroovyMonkey.

Summary

As I mentioned at the begining, the project is still evolving. To keep yourself in sync with the latest changes you should take a look at the sources. Installing them is easier than you may think. Go to project's homepage and check.

Now when you are familiar with the Monkey, it is the time to start creating your own scripts. You have learned script's syntax and concept of DOMs contribution and sharing. This knowledge enables you to make development more productive. Start making your life easier and remember to share your scripts with others.

Resources

References
Published at DZone with permission of its author, Marcin Maciukiewicz. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)