"Surround with..for each"


Just an idea the occured to me a few times, not sure how useful it would
be in practice.
---
FooBar[] = baz.getFooBar(param1, param2);
names --- Ctrl-Alt-T -> "Surround with...for each". The idea is that this "surround" template would be available when the current selection is "iterable". (Meaning array, or java.lang.Iterable]]>, or whatever foreach() accepts in
addition).

-tt


5 comments

Well, I can hardly imagine any situation where this 'Surround with...'
would be more helpful than the 'iter' live template.

Taras Tielkes wrote:


Just an idea the occured to me a few times, not sure how useful it would
be in practice.
---
FooBar[] = baz.getFooBar(param1, param2);
names<caret>
---
Ctrl-Alt-T -> "Surround with...for each".

The idea is that this "surround" template would be available when the
current selection is "iterable".
(Meaning array, or java.lang.Iterable<T>, or whatever foreach() accepts
in addition).

-tt


--
Martin Fuhrer
Fuhrer Engineering AG
http://www.fuhrer.com

0

I want this constantly. Sometimes I hit ctrlaltv, then "iter", then inline the variable. It just feels better than copying and pasting.

0

Hello Martin,

Well, I can hardly imagine any situation where this 'Surround with...'
would be more helpful than the 'iter' live template.


One situation (for me) is the following.
Let's assume I've got a "foo" object from somewhere.

Using regular completion I navigate to the collection property that I'm interested
in:
foo.getBar.getBazCollection()
At this point it know what I need, and it would be nice to wrap it with foreach.

I use "iter" alot of course, but when expression to be iterated over is more
complex this might be a nice alternative.
Let say I want to iterate the next expression.
FooService.getInstance(myContext).getBars()

Using regular "iter", I would likely need to use ClassName completion and
Smart Completion to write this expression - while the "iter" live template
is active.
Somtimes I "break" the live template by mistake, and I have to start over
again.

So in essence it would be like "iter", but using current selection as a "parameter".

-tt


0

I think you can create your own surround live template for this.

Add this to your .IntellijIdea70/config/templates/user.xml file:

    
    
    
    
      
      
      
      
      
      
      
      
    
  
]]>

0

I think you can do with your own live template.

Add this to /config/templates/user.xml:

    
    
    
    
      
      
      
      
      
      
      
      
    
  
]]>

0

Please sign in to leave a comment.