EclipseScript is an Eclipse plug-in providing support for scripting the development environment using javascript. It provides a simple API to interact with the editor while also exposing the full Eclipse platform and plug-in system.
Eclipse 3.6 or later and java 1.6 is required. Update site URL:
EclipseScript scripts are javascript files inside the workspace with the file extension .eclipse.js. As a start, create a new file with the name count-js.eclipse.js and put the following content in it:
To run a script, open the EclipseScript launch dialog by using the shortcut Ctrl+4 (or Cmd+4 on Mac). Start writing the name of the script and execute it by pressing return when the script is selected in the list.
Scripting is implemented using version 1.7R2 of the rhino javascript engine. For more information of using java from rhino, see the rhino documentation on the subject.
Besides the general java <-> javascript bridging described above, the EclipseScript plug-in injects a global object with the name eclipse exposing a simplified API to interact with the Eclipse development environment. See the below documentation for the eclipse global object.
Besides using the eclipse global object scripts may access eclipse plug-in classes just as normal java code. Plug-in loading is on demand - when first accessing a class unknown to the runtime, the EclipseScript plug-in will resolve a plug-in providing the class and load it.
Accessing and replacing the currently selected text:
SWT may be used directly:
A plug-in such as JDT is loaded just by using a package from the plug-in:
Debug output to the console:
Operations that may take some time should be run in a background job to avoid blocking the user interface thread:
A script to post the current selection to gisthub:
Example using the java AST and the eclipse markers API:
Finally an example using the java AST to wrap method bodies in timing statements, adding statements to void myMethod() { ... } resulting in void myMethod() { long _startTime = System.currentTimeMillis(); try { ... } finally { long _stopTime = System.currentTimeMillis() - _startTime; System.out.println("Time executing MyClass#myMethod: " + _passedTime) }:
The eclipse object is a global object injected by the EclipseScript plug-in and provides a simplified API to interact with the Eclipse environment compared to accessing the API directly.
get() method to get the text of the document and the set(String) method to set the text.text property for the text, as well as the offset, length, startLine and endLine properties. This property is read-only, use eclipse.editors.replaceSelection(String) to change the content of the current selection. Example: eclipse.window.alert('Selection is ' + eclipse.editors.selection.text + ' and starts at line ' + eclipse.editors.selection.startLine);eclipse.resources.workspace.root as starting point to examine the whole workspace.if (eclipse.editor.selection == null) die('Nothing is selected').Source code is available at http://github.com/fornwall/eclipsescript/.
Comments, ideas and bug reports can be filed at the issue tracker.