From b435203ca233c1c24412052e157a155732738d2a Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 28 Nov 2017 12:47:36 +0700 Subject: [PATCH] IDEA-182759 CommentTracker already used exception --- .../streamMigration/ForEachMigration.java | 5 +++-- .../foreach/afterForEachNested.java | 12 ++++++++++++ .../foreach/beforeForEachNested.java | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/afterForEachNested.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/streamApiMigration/foreach/beforeForEachNested.java 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(); + } + } + } +}