August Tip - Eclipse Templates Speed Servoy Development

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

August Tip - Eclipse Templates Speed Servoy Development

Postby cbenassi » Thu Jul 24, 2008 7:45 pm

Servoy would like to thank Darren Clarke from The Support Group for this month's Tip -- Eclipse templates speed Servoy development.

How many times have you written some variation of a "for" loop?

for(var i = 1; i <= someNumber; i++)
{

}

Sure it seems like a small task, but it's just tedious when you type it for the 100th or 1000th time. With the templates feature of Eclipse (Servoy's new development environment), you can banish this kind of repetitiveness from your coding forever.

The template functionality is part of the Eclipse code completion engine. To activate it, first type a couple of characters and then hit control-spacebar. You will be shown a list of possible completions for the letters you have typed. If the word you typed matches the name of a template, the contents of that template will be inserted into your method.

To see the default templates and to create your own, open the Servoy/Eclipse preferences and type "Templates" into the filter field. We want to look at the "Templates" node that appears in the "Javascript" section. You will see that by default all of the basic control structures (for, while, if-else) have predefined templates that you can use just by typing their names in the method editor.

To create your own template, just hit the "New" button, give it a name (the shorter the better) and add the text that will be substituted into the method. One exceedingly common snippet of code in solutions I have written has to do with querying a database using the databaseManager object. The code usually looks something like this:

var theQuery = "SELECT customer_id, first_name, last_name FROM customers WHERE active = ?";
var theArguments = ["Yes"];
var theDataset = databaseManager.getDataSetByQuery(controller.getServerName(); theQuery, theArguments, 100);

We can rewrite this as a template called "dbquery", replacing the most frequently edited sections with placeholders. The template system uses the syntax ${someVariableName} to declare these placeholders. The template version would look like this:

var theQuery = ${query};
var theArguments = [${arguments}];
var theDataset = databaseManager.getDataSetByQuery(controller.getServerName(); theQuery, theArguments, ${maxrows});

Now when I type "dbquery" and press control-spacebar in one of my methods, it will expand into the code above with the named placeholders highlighted. I can then hit the tab key to move between placeholders and fill in the required code.

Thank you, Eclipse.
Carla Benassi
Marketing Coordinator
Servoy USA
cbenassi
 
Posts: 416
Joined: Tue Mar 20, 2007 7:53 pm
Location: Thousand Oaks, California

Return to How To

Who is online

Users browsing this forum: No registered users and 6 guests