April 19, 2006

Eclipse Monkey

Eclipse Monkey, from Bjorn Freeman-Benson and Ward Cunningham, lets you script the Eclipse IDE.

An Example - cut and paste of multi-line strings in Java.

In Eclipse (version 3.1.2 - maybe this is fixed in other versions) when you paste multiple lines into a java file, it doesn't automatically convert the string into multiple lines for you. This came up when pairing yesterday, so I've written the following script, which converts whatever text is in your copy buffer into a multi-line string ready for pasting into java:

--- Came wiffling through the eclipsey wood ---
/*
 * Menu: Convert multi-line string
 * Kudos: Ivan Moore
 * License: EPL
 */
 
function main() {
	systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
	contents = systemClipboard.getContents(null);
	msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
	msg = msg.replace('\\','\\\\');
	msg = msg.replace('"','\\"');
	msg = msg.replace('\n','\\n" +\n\t"');
	newContents  = new Packages.java.awt.datatransfer.StringSelection('"' + msg + '"');
	systemClipboard.setContents(newContents, newContents);
}
--- And burbled as it ran! ---

To install this script, install eclipse monkey, copy the script above (including the "---" lines at the beginning and end), then from the "Monkey" menu in eclipse, select "Paste new script".

To run the script, copy some text, and then from the "Monkey" menu in eclipse select "Convert multi-line string". Then paste, and admire the multi-line goodness.

If you know of a better way of doing this in Eclipse (don't say "IntelliJ IDEA"), then please comment - although that wasn't the only reason for writing this script - it was also educational to write a Monkey script with a definite goal rather than just hacking around, which is all I've done with Monkey before yesterday.

Thanks to Monkey, it's much easier to extend Eclipse than it used to be; you don't have to write a plug-in for simple things like this example. I'll be using it more.

Posted by ivan at April 19, 2006 4:50 PM
Copyright (c) 2004-2007 Ivan Moore
Comments

There is a setting buried somewhere in eclipse that does this automagically for you. Window -> Preferences -> ?? Can't remember, but it works!

Posted by: Damian at April 19, 2006 6:43 PM

Hi Damian,

many thanks - I didn't know such a thing existed. After some digging around, I've found it:

window -> preferences -> java -> typing

"Escape text when pasting into a string literal"

which is (suprisingly, IMHO) not checked by default,

Ivan

Posted by: ivan at April 19, 2006 7:25 PM

From an ease of development point of view, it is very tempting to suggest that a future version of java should steal c sharp's multiline string. Ease of use is what Java needs to work on. Things like multiline strings, being able to read a string from a file as a one liner and .net's anchor layout property would IMHO make all the difference to making development in java easier. Instead of which we spend another year setting perfered sizes in vain... Have a good weekend.

Posted by: Giles Cope at April 21, 2006 5:36 PM