Erich is Professor at Lucerne University of Applied Sciences and Arts and Distinguished Member of Technical Staff at Freescale Semiconductor. Erich has a MsCS degree and 18+ years of experience in the embedded software and tools world. He created many embedded cross C/C++ compilers and debuggers. Additionally he is researching in the domain of programming languages, real time and mechatronic systems. Erich is a DZone MVB and is not an employee of DZone and has posted 82 posts at DZone. You can read more from them at their website. View Full User Profile

Editor Templates in Eclipse

03.12.2013
| 5758 views |
  • submit to reddit

Eclipse is a good IDE. At a first glance, it does the job. Good enough. At least for myself, I was not that much excited when I used it the first time. I came from the Microsoft Visual Studio world, and have used many other proprietary IDE’s. So Eclipse was just ‘yet another one’. But what Eclipse makes really great is the incredible wealth of functionality which is not visible right away. Yes, this is the same for any other software tool: it takes time to explore, and once you know things well, you do not want to switch or even consider something different. Same for me.

The other thing is: after some time, I get used to things, and I do not appreciate it that much any more. Only until someone reminds me that maybe things are not that well-known? This is what happened to me two days ago: I did some editing in Eclipse, while a colleague was watching me doing this. Then he said something like this

“Hey, what did you do? What was that?!? How did you do that?”

I do not remember his exact words, as I was surprised as well. I did not do anything special? Kinda standard Eclipse thing. Well, maybe not.

:!: Warning: Using this Eclipse feature here might be highly addictive :-) .

The For Loop

What I did was something like this: I started writing a function like this:

writing a function

writing a function

Then I added a ‘for’:

adding a for

adding a for

And now the magic: I pressed Ctrl+Space to open the Content Assist:

Content Assist

Content Assist

:idea: Pressing Ctrl+Space reduces the list of items to template proposals only.

I selected the ‘for – for loop‘ item and hit enter. This adds the code for the loop:

Added for loop

Added for loop

Notice the blue boxes. My cursor stays on ‘var’, and I can simply type my variable I want to use, and it changes all loop variable instances automatically (!!!):

changed loop variable

changed loop variable

Next I press Tab which moves me to the ‘max’ variable:

on max variable

on max variable

I type in my loop max variable and press Tab again: this moves me to the body of the loop:

entered max variable

entered max variable

Now I enter my loop body:

Entered loop body

Entered loop body

So here again:

for – Ctrl+Space – Enter – i – Tab – bufSize – Tab – buf[i] = 0;

is all what I needed :-) . A cool standard Eclipse editor feature: Editor Templates.

Template on Selection

It is possible to apply a template on a selection. For example I can select one or more source lines:

Selected text

Selected text

Then I press Ctrl+Space and select the for-loop template. This wraps a for() loop around it:

For loop around selection

For loop around selection

Template Positions

Let’s have a look at the stage when I pressed Ctrl+Space and selected that template:

Template Positions

Template Positions

The blue boxes indicate positions where I will jump to if I press Tab. The green line in the for() body indicates the place I will jump to if I press Enter.

Template Preferences

The Eclipse Editor Templates are configured using the menu Window > Preferences > C/C++ > Editor > Templates. The template example I was using above looks like this:

For-Loop Editor Template

For-Loop Editor Template

If I do not like a template, I can create a new one or edit an existing one:

Editing a Template

Editing a Template

Variables are of the form:

${variableName}

Pressing the ‘Insert Variable…’ button offers a set o built-in variables:

Built-In Variables

Built-In Variables

:idea: Use Ctrl+Space while editing the template to get a list of built-in variables.

Anything not a built-in variable is a custom variable (e.g. ${var}). Using the variable name multiple times references the same variable content.

:idea: It is possible to use an ‘empty’ variable as well with ${}. That way I can have things which are not replaced with anything if I want to have it that way.

Custom Editor Templates

Adding new templates is really easy. For example I have a template ‘taskcreate’ to create the source code to create a FreeRTOS task:

if (FRTOS1_xTaskCreate(${name}, (signed portCHAR *)"${name}", configMINIMAL_STACK_SIZE+${size}, NULL, tskIDLE_PRIORITY+${prio}, NULL) != pdPASS) {
  for(;;){} /* error! Out of heap? */
}
${cursor}

Or another template to implement the framework for a task function:

static portTASK_FUNCTION(${taskname}, pvParameters) {
  (void)pvParameters; /* not used */

  for(;;) {
  ${cursor}
  FRTOS1_vTaskDelay(${delayMs}/portTICK_RATE_MS);
  }
}

That way I’m really fast adding a new tasks. I simply type the word ‘task’ followed by Ctrl+Space:

Task Templates

Task Templates

And within seconds I have created the framework for a new task:

Created Task

Created Task

Comment Templates

Another way to use templates is for comments. For example I can define a comment like this:

Comment Template

Comment Template

Then I type

/**fct

and press Ctrl+Tab, and it will replace it with my template :-) .

:idea: Eclox for Doxygen comes with other built-in comment templates.

Exporting/Importing

To share templates, I use the Import and Export buttons:

Import and Export of Editor Templates

Import and Export of Editor Templates

Summary

Editor templates are a powerful way to extend the Eclipse editor and to improve productivity. This is one of the greatest Eclipse features in my view.

Happy Templating :-)

 

Published at DZone with permission of Erich Styger, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)