しおしお

IntelliJ IDEAのことなんかを書いてます

IntelliJを使ってfor文をforEachに置き換えしてみた

IntelliJのStructural Replaceでfor文をforEachに置き換えてみた。

メニューのEdit -> Find -> Replace Structurally...で、「Structural Replace」を開く。
置き換え前、置き換え後のテンプレートには以下を設定する。

Search template

for ($type$ $var$ : $collection$) {
    $body$;
}

Edit Variablesから以下の設定を行う。

  • $collection$

Text/regexp for java expression typeに「java.lang.Iterable」を設定する。

Apply constraint within type hierarchyのチェックをオンにする。

  • $body$

Minimum countに「0」を設定する。

unlimitedのチェックをオンにする。

Replacement template

$collection$.forEach(($var$) -> {
    $body$;
});

forEach以外への置換えはなかなか難しそう。

おわり。