views:

240

answers:

5

Is there a way to auto complete a statement in eclipse? If I type

if (condition) 

it should, after pressing some key, become

if (condition) {
    // putting cursor here
}
A: 

Try Ctrl-Space

Andy
+1  A: 

Ctrl + Space is default for code assist.
For more customization you need templates.

Padmarag
+2  A: 

You can just continue to type the { and enter, and the closing bracket will be completed automatically without any special command:

if (  condition) { /* cursor here , enter */


if (  condition) { 
  /* cursor here */
}

There is also autocompletion (Crtl-Space), which can do the following

if /* cursor here , Ctrl-Space */

becomes

if ( /* cursor here */ condition) {

}

or

if ( /* cursor here */ condition) {

} else {

}
Thilo
+1  A: 

This kind of thing cannot be autocompleted the conventional way, since you're not adding to a class or variable name. However, this should be possible using eclipse templates (Window->Preferences->Java->Editor->Templates)

if (${condition:var(boolean)}) {
 ${cursor}
}

You can then access the template by typing its name (most likely just 'if') and pressing Ctrl-space

Jorn
+1  A: 

Ctrl + Space is used to auto complete words, variable names, classes etc, if you want eclipse to complete your if statement, do the following:
- write your if condition along with the first bracket

if(condition){

- hit enter, then eclipse will do its job.

marcos