How to get content assist for “for loop” anywhere

2020-07-10 03:34发布

问题:

In Eclipse, after a line like this:

List list = new ArrayList();

Typing "for" just beneath, and followed by "ctrl-space" (by default), will bring several options that can help completing this "for loop":

But if the variable "list" is declared far from here (e.g. as a class field) which may not be directly inferred from this context, or there are many Lists declared,then the assistance doesn't work well:

@@ split line ---

In some cases, Eclipse can assist, but just don't work for member variable. E.g. manually type "another" and ENTER after the ":" didn't persuade Eclipse to guess about it....

(P.S. workable case:

Auto guessed

Entered wanted name, and ENTER, works great

)

Does anyone have any tip to make this assistance work under such scenarios?

回答1:

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) {
    }
    


回答2:

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.


回答3:

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 :

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 :

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.



标签: eclipse