Tuesday, March 24, 2009

IntelliJ Live Templates - For Each over a Collection

As a recent convert to IntelliJ, I'm doing my best to take full advantage of the IDE's time saving measures. I thought that IntelliJ was missing a Live Template that creates a for each control block over a given collection. Here is how I created this Live Template:

  1. Open Settings (Command-,)
  2. Select Live Templates and click the "Add" button
  3. Complete the dialog fields
  1. Click "Edit variables" and complete the dialog


To use this template, simply type Command-J in your editor and begin typing "feco" until the template name is highlighted in your list. Press TAB to accept the template and fill in the values that you need.

--Update--
It turns out that there is already a template to do this identified by "iter" (thanks yole). Fortunately the exercise was useful in getting started with writing my own templates. I've since written one for creating a standard TestNG test method that includes the "groups" parameter for the @Test annotation. My team organizes our tests into groups so that we can execute different tests in different levels of our builds. Tests in the group "unit" are run as part of our commit build, while tests in other groups are run as part of our integration test build.

Here's the template defintion:


And the variable assignments:
.

2 comments:

  1. The built-in "iter" template does exactly that.

    ReplyDelete
  2. So it does! Do you happen to know where there is any documentation on the Live Template syntax?

    ReplyDelete