Quick tips for setting up Eclipse and Monkey

  • Optional – add whatever plugins that you think is useful for your development work.

Monkey Script

  • To create a monkey script, add a new file under the “monkey” folder (need to create in eclipse). Name the file “author.js” for example (we are going to create a macro to insert developer’s comments). Copy and paste the following code in author.js
/*
 * Menu: Editors > Developer's Comment
 * Key: M3+M2+a
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
var newDate = new Date();
var comment = "@author Bernard Peh "+newDate;

var commentLength = comment.length;

function main() {

	var editor = editors.activeEditor;

	// get range of lines in the selection (or at the cursor position)
	var range = editor.selectionRange;
	var startLine = editor.getLineAtOffset(range.startingOffset);
	var endLine = editor.getLineAtOffset(range.endingOffset);

	// determine if we're adding or removing comments
	var source = editor.source;
	var offset = editor.getOffsetAtLine(startLine);
	var addComment = (source.substring(offset, offset + commentLength) != comment);
	var adjust = 0;

	editor.beginCompoundChange();

	if (addComment) {
		for (var i = startLine; i <= endLine; i++) {
			var offset = range.startingOffset;
                        // var offset = editor.getOffsetAtLine(i);
			editor.applyEdit(offset, 0, comment);
		}
	} else {
		for (var i = startLine; i <= endLine; i++) {
			var offset = range.startingOffset;
                        // var offset = editor.getOffsetAtLine(i);

			if (source.substring(offset + adjust, offset + adjust + commentLength) == comment) {
				editor.applyEdit(offset, commentLength, "");
				adjust += commentLength;
			}
		}
	}

	editor.endCompoundChange();
}

Everything in author.js is javascript. Modify as you see fit. More examples can be seen in the plugins dir (org.eclipse.eclipsemonkey_x.x.x.xxxxxx). Now in any document, just press alt+shift+a to insert the author’s comment. Imagine what you can do with this technique!!

 // eg. to add phpdoc using the shortcut, modify the variables
/*
 * Menu: Editors > php class header
 * Key: M1+M2+c
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
var newDate = new Date();
var comment = "\t/*\n\t * shor desc\n\t * \n\t * long comments\n\t * @author Bernard Peh "+
newDate+"\n\t */";

/*
 * Menu: Editors > php function header
 * Key: M1+M2+f
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
var newDate = new Date();
var comment = "\t/*\n\t * shor desc\n\t * \n\t * long comments\n\t * @author Bernard Peh "+
newDate+"\n\t * @param string $var desc\n\t * @return integer\n\t */";

Note

  • If the monkey shortcut key doesn’t work, check for keys conflict. Try out new key combination.
  • M1 is the COMMAND key on MacOS X, and the CTRL key on most other platforms. M2 is the SHIFT key. M3 is the Option key on MacOS X, and the ALT key on most other platforms. M4 is the CTRL key on MacOS X, and is undefined on other platforms.

Other Tips

  • CTRL + SHIFT + L : Shows you a list of your currently defined shortcut keys.
  • mouse over a function or class to see quick description.
  • CTRL + mouse over a function or class to see snippets of the class or function. While hovering the text, press F2 to see full code snippets.
M1 is the COMMAND key on MacOS X, and the CTRL key on most other platforms. M2 is the SHIFT key. M3 is the Option key on MacOS X, and the ALT key on most other platforms. M4 is the CTRL key on MacOS X, and is undefined on other platforms.
Like it.? Share it:

Comments are closed.