Eclipse configuration for Syntax Highlighting, Template proposals and Refactoring in Sahi scripts
You can use any text editor to write your sahi scripts. ButEclipse
as an editor provides advantages like Syntax highlighting
, Template proposals
and Refactoring
. This article explains how to use Eclipse
as editor for Sahi scripts.
Sahi scripts - .sah Vs .js
Sahi scripts carry a.sah
extension. But a .js
extension can be used in place of .sah
. Sahi treats a .js
file the same as a .sah
file.
To write scripts in Eclipse, one needs to have the
JSDT
Eclipse plug-in installed. The JSDT
Eclipse plug-in provides native support for JavaScript (.js)
files. One can add support for .sah
files as well. But the JSDT
plugin provides some additional features for .js
files as compared to .sah
files. Hence it is possible to rename .sah
files as .js
files and get better Eclipse support without losing any Sahi functionality.
danger
We are looking at ways to make the script authoring experience easier and better for end users. At the time of writing this document, Sahi scripts carry a
.sah
extension by default. We are undecided on whether we will change the default extension to .js
in a future release. We leave it to the end user to decide whether to use scripts with a .sah
extension or change them to have a .js
extension. This document explains how to deal with both extensions.
info
If any configuration step is specific to
This document explains features available for both
.sah
or .js
files, it is explicitly mentioned. Unless explicitly mentioned, the step applies to both .sah
and .js
files.
This document explains features available for both
.js
and .sah
files. If some feature is not available for .sah
files, it is explicitly mentioned.
danger
It is assumed that your scripts will either have a
.sah
or .js
extension.
danger
The steps have been documented for
Eclipse Kepler (Eclipse Standard 4.3.1 - Windows 64 Bit version)
and Eclipse Mars (Eclipse Java EE IDE for Web Developers 4.5.2 - Windows 64 Bit version)
versions. It is possible that you may face difficulties with a different Eclipse versions.
Eclipse configuration
-
Open Eclipse. It will ask for the workspace path. We will assume that the workspace is (to be) created at the following location:
C:\Sahi\SampleWorkspace
. Specify the workspace path as indicated below, and clickOK
. -
Ensure that the
JSDT
Eclipse plug-in has been installed. Click onHelp | About Eclipse SDK | Installation Details
.
-
If not installed, click on
Help | Install New Software
. Use the settings displayed in the image below. If you are using an older version of Eclipse, you should see a corresponding update site in theWork with
dropdown box.Proceed with the installation.
Restart Eclipse after installation. Give the same workspace path as before -C:\Sahi\SampleWorkspace.
-
Adding support for
.sah
filesdanger This step applies only if you are using.sah
files. This is not needed for.js
files.- Click on Windows | Preferences | General | Content Types
-
In
Content types:
section, selectText | JavaScript Source File
and then inFile associations:
section, click onAdd
. -
Make association for
*.sah
and clickOK
.
-
Let us now create a new JavaScript Project.
Click onFile | New | Project...
Search for JavaScript Project in Wizards and click on
Next
Give a Project name of your choice. We have given it the name:
SampleProject
. ClickNext
You should see the following two JavaScript Libraries in the Libraries tab
- ECMAScript Built-in Library
- ECMA 3 Browser Support Library
Finish
If this is the first JavaScript project you created, an
Open Associated Perspective
dialog box will be opened, prompting you to use the JavaScript perspective.
ClickYes
. -
You can create new files and folders under the project created. We will place all Sahi scripts inside a
scripts
folder. To create a new folder, right click on the project, and then click onNew | Folder
. Give the folder name asscripts
and click onFinish
.To create a script file inside the
scripts
folder, right click on the folder, and then click onNew | File
. Give the new file its appropriate name and click onFinish
. Let us name the file assampleFile.js
. If you intend to use.sah
extension, name it assampleFile.sah
You can copy your existing scripts into the scripts folder of the current project by pasting them at the appropriate location -
C:\Sahi\SampleWorkspace\SampleProject\scripts
. The screenshot shows scripts as.js
files. If you intend to use.sah
extension, the scripts will be.sah
files.Refresh the project to see the copied files in your Eclipse IDE.
-
The
JSDT
plugin provides various templates for auto completion. Example:for
,if
etc.info These templates can be used inSahi ships with a readymade template file, SahiEclipseEditorTemplates.xml that customizes some of these templates for better use in Sahi scripts. Download SahiEclipseEditorTemplates.xml to a folder location, say.js
as well as.sah
files.C:\Sahi\SampleWorkspace\EditorTemplates
To install this templates file, click onWindow | Preferences
.Now go to
Javascript | Editor | Templates
as shown in the figure and click onImport
on the right.Select the templates file
SahiEclipseEditorTemplates.xml
that we downloaded above, and pressOpen
.Press
OK
on theTemplates
window.SahiEclipseEditorTemplates.xml
has now been successfully imported.
To make use of any template, type the first few letters of the template and pressCtrl + Space
to bring up the template proposal.
Here are a few examples.
Example: Type the first few letters of the templatetestcase
and pressCtrl + Space
to bring up the template proposal for_testcase
.Example: Type the first few letters of the
for
loop command and pressCtrl + Space
to bring up template proposals related tofor
.Likewise you can expand other templates listed at the location below.
-
Template functionality can also be added for
Sahi APIs
.info These templates can be used in.js
as well as.sah
files.-
If you are using
.sah
extension, copyapis.sah
from<SAHI_INSTALL_FOLDER>/userdata/scripts
into thescripts
folder in the project. -
If you are using
.js
extension, copyapis.sah
from<SAHI_INSTALL_FOLDER>/userdata/scripts
into thescripts
folder in the project and rename it asapis.js
.
.js
(or.sah
) file from the JavaScript project, and you should be able to seeSyntax highlighting
.
Type the first few letters of a Sahi function and pressCtrl + Space
to bring up template proposals for Sahi APIs.
-
If you are using
Refactoring a script into functions
info
This feature is available in
Once you have a recorded script, Eclipse can help in refactoring the linear script into functions.
.js
as well as .sah
files.
We have taken the following script to demonstrate refactoring using Eclipse

Select all the lines of code,
Right Click | Surround With | function (function)
as shown in the figure. Give the function an appropriate name (we have named it sampleApp
). We will remove this function at the end of the Refactoring.

Select the lines of code | Right Click | Refactor | Extract Function
.

OK
, as shown in the figure.


- You can see the following changes in the script:
- The selected lines of code have been replaced by functions and calls to the respective functions.
- The refactored function code may not be formatted correctly. If so, you will have to format it manually.
- The created functions do not have any parameters. We will fix this in the next step.
Select the value | Right Click | Refactor | Introduce Parameter
.

$
and press OK
. Here we have named the parameter as $java



sampleApp
. The script now looks like this:

login
, addAndAssertTotalCost
and logout
to a different file so that they can be included from multiple scripts.
warning
The functions have to be moved manually.
Add a new file called sampleLib.js
(or sampleLib.sah
if you are using .sah
extension). Copy the functions to this file. This will have to be done manually.

sampleFile.js
(or sampleFile.sah
if you are using .sah
extension). Include sampleLib.js
(or sampleLib.sah
) using _include
manually. sampleFile.js
now looks like this

Renaming a function or variable name
info
This feature is available in
.js
as well as .sah
files.
danger
However, it appears that it is more reliable in
There are times when one needs to rename a variable or function name. On doing so, the name should get changed at all places of usage, in all scripts.
.js
files than in .sah
files.
Eclipse allows for such refactoring. Let us see how to rename a function name. We will rename the function
addAndAssertTotalCost
to addBooksAndAssertTotalCost
.
To do this, open
sampleLib.js
, select addAndAssertTotalCost
, Right Click | Refactor | Rename
(or select addAndAssertTotalCost
and press Alt + Shift + R
).

addBooksAndAssertTotalCost
and press Enter
.

addBooksAndAssertTotalCost
.

sampleFile.js
. You will notice that the function name has been changed automatically in this file as well.

Navigate to functions
danger
This works only for
One often needs to open a function declaration to view its contents. The function declaration could be present inside another script altogether. For example, let us try and look at the function declaration of .js
files and not for .sah
files.
addBooksAndAssertTotalCost
from inside sampleFile.js
.
One can open a function declaration in the following ways.
-
Press the
Ctrl
key andhover the mouse over the function
that you want to open. The function name will appear as alink
.Click on the
link
while keeping theCtrl
key pressed. -
Or select the function,
Right Click | Open Declaration
. -
Or select the function, and press
F3
.
addBooksAndAssertTotalCost
in sampleLib.js
.
