Dec 16 2009

Extending Eclipse using JavaScript and Monkey Script engine

Category: javascriptDavide Zanotti @ 9:20 am

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* 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

Tags: , ,


Nov 04 2009

Installing Eclipse + Aptana + Subclipse SVN

Category: ideDavide Zanotti @ 2:18 pm

Recently I’ve updated my Eclipse version and I installed certain plugin which has created some kind of conflict and confusion in my workspace. What I was trying to do was installing an SVN plugin in order to work on a google code SVN repository, but I had several errors and I lost several hours trying to figure out what was wrong. So I decided to do a fresh and clean installation, once understood the problem. So, I would like to write a sort of tutorial which will explain how to get a sound and working installation of Eclipse, Aptana and Subclipse (which as far I read, is actually the best plugin available for SVN on Eclipse).

Continue reading “Installing Eclipse + Aptana + Subclipse SVN”

Tags: , , ,


Aug 25 2009

Eclipse: convert upper case text to lower case and viceversa with a simple shortcut

Category: ideDavide Zanotti @ 8:02 am

This is just a quick post to share these 2 little shortcuts to convert text from lower case to uppercase and viceversa in Eclipse.

Lower case: CTRL+SHIFT+Y (CMD+SHIFT+Y on Mac OS X)
Upper case: CTRL+SHIFT+X (CMD+SHIFT+X on Mac OS X)

In both the combination you can select one or more character to convert.

Tags: , ,


Mar 06 2009

Aptana’s Javascript editor is too cool!

Category: ideDavide Zanotti @ 8:03 am

I’ve just discovered that the last version of the Aptana Studio (in my case the Eclipse plugin version) has an integrated support for javadoc syntax inside js files. The beauty of this feature is that, once you have defined a function, you can just type /** and press enter and Aptana will generate automatically all the comments for you:

js_javadoc_aptana_1

Furthermore it will show tips including parameters description when you will use your previous defined function:

js_javadoc_aptana_2

…and if you want to add extra “@tag”, the editor will suggest you all tags available:

js_javadoc_aptana_3

Too cool!!! I love Aptana :-)

Tags: , , , , ,


Nov 24 2008

Associate custom file extensions to the default Aptana’s text editor

Category: ideDavide Zanotti @ 1:06 pm

aptana

In a project which I’m working on, the team make an intensive use of text file to include with SSI, and these files are named conventionally  .inc, Aptana by default opens these custom extensions with the default system’s editor (notepad on windows). We can tell Aptana to open the .inc or whatever extension we want with the Aptana’s text editor by simply doing the following: Choose window/preferences/Aptana/Editors/Generic Text, click add and then type our desired extension (in my case *.inc). Now if we double click over a .inc file in the project tree view, it will be opened inside Aptana.
ps. The path to the settings panel may change according to the version of Aptana/Eclipse installed (I’m using the Aptana plugin for Eclipse 3.4.1)

Tags: , , , , , ,