Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to quickly generate a foreach for an existing variable using live templates?

I have the following code snippet:

Set<Company> companiesByUserName = companyUserService.getCompaniesByUserName(username);

Using IntelliJ live templates, I know I can type "itco" and it will generate the following for me:

for (Iterator<Company> iterator = companiesByUserName.iterator(); iterator.hasNext(); ) {
    Company next =  iterator.next();
}

However, how can I automatically create a foreach with the 'companiesByUserName' variable instead? So I want it to generate this automatically:

for (Company company: companiesByUserName) {
}

Because the foreach is a lot cleaner that iterating through the collection in a for loop, I generally use those instead, so would like to auto generate them if possible.

like image 420
dleerob Avatar asked Oct 13 '16 10:10

dleerob


2 Answers

There is also a new feature introduced with IJ 13, called postfix completion. With that, you can type companiesByUserName.for and hit TAB (and obviously much more as per your defined templates):

postfix completion

like image 84
Morfic Avatar answered Sep 25 '22 00:09

Morfic


Nevermind, found out how to do it. Just type "iter" and press enter.

like image 20
dleerob Avatar answered Sep 23 '22 00:09

dleerob