How to get content assist for "for loop" anywhere in Eclipse?

I followed Ashutosh Jindal's tip and I managed to configure the template that works (tested with Kepler release). Here it is:

for (${iterable_type:elemType(iterable)} ${iterable_element:newName(iterable_type)} :  ${iterable:var(java.lang.Iterable)}) {
    ${cursor}
}

The main point was to change localVar to var in the template definition (the Eclipse docs clearly explain this).

How to use it:

  1. Print the template name (foreach for default template) and hit Enter. The template will be used with default collection selected by Eclipse (the latest collection declared)
  2. Hit TAB twice to jump to collection element. You will get drop down list with all iterable collections that apply.
  3. Use UP/DOWN arrows to select desired collection, hit Enter. Eclipse will adjust element type and name (very cool).

Click for the screenshot

This works almost as good as Intellij templates. The drawbacks are:

  • template does not include arrays (as opposed to default foreach template). For arrays you have to define another template.

I have not tried this myself, but take a look at the code template definition. For example, the foreach code template is defined in Preferences -> Java -> Editor -> Templates as follows :

Definition of foreach

The definition is as follows :

for (${iterable_type} ${iterable_element} : ${iterable}) {
    ${cursor}
}

Notice the variables being used such as iterable_type.

Now take a look at this Eclipse help page.

There is a variable there called ${id:localVar(type[,type]*)} which is described as follows :

Evaluates to a local variable or parameter visible in the current scope that is a subtype of any of the given type. If no type is specified, any non-primitive local variable matches.
${array} is a shortcut for ${array:localVar(java.lang.Object[])}, but also matches arrays of primitive types.
${collection} is a shortcut for ${collection:localVar(java.util.Collection)}.
${iterable} is a shortcut for ${iterable:localVar(java.lang.Iterable)}, but also matches arrays. 

A screenshot of the same :

Variable

I believe that if you wanted to increase the scope from which the foreach template infers it's variables, you may have to edit the template definition with the appropriate variable.

Let me know if this helps. Unfortunately, I have not delved into editing the code templates before so shall not be able to give a concrete example.


What I usually do to solve the content assist with for loop is the following:

  • create a local variable by typing collection variable that is declared far above and a semicolon:

    list;
    
  • press Ctrl+2 L

  • Eclipse generates a new local variable declaration which looks like this:

    List list2 = list;
    
  • type my foreach and autocomplete with Ctrl+space getting the following:

    List list2 = list;
    for (Object object : list2) {
    }
    
  • place cursor on list2 in the for loop declaration and press Alt+Shift+I which stands for inline variable.

  • this results in what you want to achieve. The effort is minimal after some practicing:

    for (Object object : list) {
    }
    

Tags:

Eclipse