<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DaveOnCode &#187; extensions</title>
	<atom:link href="http://www.daveoncode.com/tag/extensions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>coder and technology lover</description>
	<lastBuildDate>Sun, 16 May 2010 16:55:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Extending Eclipse using JavaScript and Monkey Script engine</title>
		<link>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/</link>
		<comments>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:20:50 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=555</guid>
		<description><![CDATA[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&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my]]></description>
			<content:encoded><![CDATA[<p>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&#8217;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&#8217;t find a complete and exhaustive reference for this project, which seems forgotten by authors, so I learnt what I know reading different posts.<br />
Basically, Monkey Script allow developers to access editor&#8217;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&#8230; and finally the scripts created will be accessible from Eclipse menu (under &#8220;Scripts&#8221;) and invokable through user defined shortcut&#8230; really nice!<br />
So, backing to my experiment, I created this monkey script:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* <br />
* Key: M1+M2+C<br />
* Menu: Custom Scripts &gt; Wrap with double quotes<br />
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript<br />
* Kudos: Davide Zanotti<br />
*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// no editor... exit</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> editors.<span style="color: #660066;">activeEditor</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;No active editor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// get a reference to the editor in use</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> editor <span style="color: #339933;">=</span> editors.<span style="color: #660066;">activeEditor</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// beginning of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">startingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// end of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">endingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// selected text</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> editor.<span style="color: #660066;">source</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// surround selection with quotes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;editor.<span style="color: #660066;">applyEdit</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset <span style="color: #339933;">-</span> startOffset<span style="color: #339933;">,</span> <span style="color: #3366CC;">'&quot;'</span> <span style="color: #339933;">+</span> selection <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">code</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s important to notice some points, first you MUST write an &#8220;header&#8221; using the comment syntax &#8220;/* */&#8221; into which you MUST declare at least 2 parameter: the &#8220;<strong>Menu</strong>&#8221; path (you can nest several menu items by writing several &#8220;&gt;&#8221;) and the &#8220;<strong>DOM</strong>&#8221; package (which allows you to access specific objects like &#8220;<strong>editors</strong>&#8220;). You can write several js functions, but you must provide a <strong>main()</strong> 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 &#8220;OnLoad&#8221; param in the header, specifying which function will be called.<br />
To define a shortcut you can use the &#8220;<strong>Key</strong>&#8221; 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 &#8220;<strong>Kudos</strong>&#8221; is used to declare the author of the monkey script.<br />
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: &#8220;custom-extensions&#8221;), then create a &#8220;scripts&#8221; folder into which you will save your .js files&#8230; that&#8217;s all, try yourself and enjoy :)</p>
<p>This is how it looks:</p>
<div id="attachment_558" class="wp-caption alignnone" style="width: 519px"><img src="http://www.daveoncode.com/wp-content/uploads/2009/12/Screen-shot-2009-12-16-at-17.08.56.jpg" alt="eclipse monkey script menu" title="eclipse-monkey-script-menu" width="509" height="254" class="size-full wp-image-558" /><p class="wp-caption-text">eclipse monkey script menu</p></div>
<p>ps: I&#8217;m going to explore the Monkey Script API in order to write more complex and useful extensions :P</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
