I want to select an Iterable, press Alt+Shift+Z to get the "Surround With" context menu, and have a foreachwrap template displayed that will wrap the selection in a for each loop with the appropriate format.
For example, i want to select
someObject.getSomeList()
and generate this
for (SomeListType someListType : someObject.getSomeList()) {
}
I've tried something like this, but it doesn't seem to work:
for( ${t:elemType(ls)} ${:name(t)} : ${ls:line_selection} )
{
${cursor}
}
Use QuickFix (Ctrl+1
on Win/Lin or Cmd+1
on the Mac).
You can get the desired behavior with the following approach:
Write the statement that returns the iterable collection, e.g.
someObject.getSomeList()
Press Ctrl+1
(Cmd+1
on the Mac) and select Assign statement to a new local variable
(there is even a direct combination for this action (Cmd+2 L on the Mac), however, if you want to use it, it probably depends on how much different combinations do you want to remember)
Write no more than
fore
and press Ctrl+1
(Cmd+1
) again, then select Iterate over an array or iterable
(simply pressing Enter right after the quickfix menu appears is usually enough at this point) and you get something like this:
for (Content content : someList) {
}
You can now even inline the usage of the local variable to get rid of it again (using QuickFix of course ;). QuickFix is really powerful, I've hardly ever used any templates since that feature was published.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With