Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Surround with"-template in Eclipse: foreach

I am new to Eclipse which I use primarily for Java. I have previously used IntelliJ Idea in which it is possible to select a variable which extends Iteratable (Collection, List etc) and have it produce a correct foreach loop.

I know Eclipse does something similar with the foreach template, where it guesses which variable to iterate over, but I can't get it to the same thing with a selected variable. But what if the variable is not in the current scope and what if Eclipse guesses wrong?

So what I am trying to do is being able to select a variable (or function which returns a variable) which implements Iterator and have it return:

Selection:

functionWhichReturnsList()   (which returns List<TypeOfItemsInList>)

Result:

for (TypeOfItemsInList item : functionWhichReturnsList()) {  
   ${cursor}  
}

Any ideas?

like image 388
Casper Avatar asked Feb 28 '10 13:02

Casper


2 Answers

I typically create code like this by following these steps:

Call the function and use Ctrl-1 to create a local variable holding the return value:

List<TypeOfItemsInList> list = functionWhichReturnsList()

Type fore[Ctrl-space] to insert the for loop (since eclipse usually chooses the closest iterable when constructing the loop):

List<TypeOfItemsInList> list = functionWhichReturnsList()

for (TypeOfItemsInList item : list) {
}

Inline the local variable by putting the cursor on the list variable and typing Alt+Shift+I:

for (TypeOfItemsInList item : functionWhichReturnsList()) {
}

It's not optimal, but it works.

like image 189
ChrisH Avatar answered Nov 15 '22 21:11

ChrisH


Update 2:

In Eclipse 4.4 What's New in Luna (JDT) a QuickFix was added for this issue. This can be also used for arrays, collections and maps (key sets and values).

enter image description here

Update 1

I recently found an eclipse plugin that provides postfix code completion (https://github.com/trylimits/Eclipse-Postfix-Code-Completion). This is how it works for the foreach loop:

enter image description here

Beside, the plugin provides more helpful competitions :)

For Eclipse before 4.4:

I'm also having the same issue but I wasn't able to find a solution. So, for the moment I perform following steps to get the desired loop.

fore[Ctrl-space] and select the foreach template, I get the following:

for (iterable_type iterable_element : iterable) {

}

then I perform a double click on iterable and replace it with the method:

for (iterable_type iterable_element : functionWhichReturnsList()) {

}

on the next step just click on functionWhichReturnsList() and hit Ctrl-1. Eclipse will suggest to change the type iterable_element to TypeOfItemsInList. This is want you get at the end:

for (TypeOfItemsInList iterable_element : functionWhichReturnsList()) {

}

Now, you just need to find a proper name for iterable_element. Just double click on it and start typing.

like image 40
kon Avatar answered Nov 15 '22 21:11

kon