diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/ForEachMigration.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/ForEachMigration.java index 7afff77e159b..881b84764b53 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/ForEachMigration.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/ForEachMigration.java @@ -103,8 +103,9 @@ class ForEachMigration extends BaseStreamApiMigration { PsiTypeElement typeElement = tb.getVariable().getTypeElement(); if (typeElement != null) { String typedVariable = typeElement.getText() + " " + tb.getVariable().getName(); - callStatement = (PsiExpressionStatement)callStatement.replace(factory.createStatementFromText( - stream + "(" + typedVariable + ") -> " + wrapInBlock(ct, block) + ");", callStatement)); + ct = new CommentTracker(); + callStatement = (PsiExpressionStatement)ct + .replaceAndRestoreComments(callStatement, stream + "(" + typedVariable + ") -> " + wrapInBlock(ct, block) + ");"); } } return callStatement; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java new file mode 100644 index 000000000000..9aba752fc18f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java @@ -0,0 +1,12 @@ +// "Replace with forEach" "true" + +import java.util.Arrays; + +public class Test { + public static void main(String[] args) { + for (String module : args) { + VirtualFile[] sourceRoots = foo(module); + Arrays.stream(sourceRoots).forEach((VirtualFile sourceRoot) -> sourceRoot.substring()); + } + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/beforeForEachNested.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/beforeForEachNested.java new file mode 100644 index 000000000000..e53af7adefea --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/beforeForEachNested.java @@ -0,0 +1,12 @@ +// "Replace with forEach" "true" + +public class Test { + public static void main(String[] args) { + for (String module : args) { + VirtualFile[] sourceRoots = foo(module); + for (VirtualFile sourceRoot : sourceRoots) { + sourceRoot.substring(); + } + } + } +}