Editor Templates in Eclipse
Join the DZone community and get the full member experience.
Join For Freeeclipse 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:
then i added a ‘for’:
and now the magic: i pressed ctrl+space to open the content assist:
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:
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 (!!!):
next i press tab which moves me to the ‘max’ variable:
i type in my loop max variable and press tab again: this moves me to the body of the loop:
now i enter my 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:
then i press ctrl+space and select the for-loop template. this wraps a for() loop around it:
template positions
let’s have a look at the stage when i pressed ctrl+space and selected that template:
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:
if i do not like a template, i can create a new one or edit an existing one:
variables are of the form:
${variablename}
pressing the ‘insert variable…’ button offers a set o built-in variables:
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.
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:
and within seconds i have created the framework for a new task:
comment templates
another way to use templates is for comments. for example i can define a comment like this:
then i type
/**fct
and press ctrl+tab, and it will replace it with my template
.
eclox for doxygen comes with other built-in comment templates.
exporting/importing
to share templates, i use the import and export buttons:
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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments