diff --git a/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/BaseConvertToLocalQuickFix.java b/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/BaseConvertToLocalQuickFix.java index 4c22a1939aa7..2d55c4d63817 100644 --- a/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/BaseConvertToLocalQuickFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/BaseConvertToLocalQuickFix.java @@ -33,6 +33,7 @@ import com.intellij.util.IJSwingUtilities; import com.intellij.util.IncorrectOperationException; import com.intellij.util.NotNullFunction; import com.intellij.util.containers.HashSet; +import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -154,14 +155,20 @@ public abstract class BaseConvertToLocalQuickFix implemen return WriteAction.compute(() -> { final PsiElement newDeclaration = moveDeclaration(elementFactory, localName, variable, initializer, action, references); if (delete) { - beforeDelete(project, variable, newDeclaration); - variable.normalizeDeclaration(); - variable.delete(); + deleteSourceVariable(project, variable, newDeclaration); } return newDeclaration; }); } + protected void deleteSourceVariable(@NotNull Project project, @NotNull V variable, PsiElement newDeclaration) { + CommentTracker tracker = new CommentTracker(); + beforeDelete(project, variable, newDeclaration); + variable.normalizeDeclaration(); + tracker.delete(variable); + tracker.insertCommentsBefore(newDeclaration); + } + protected PsiElement moveDeclaration(PsiElementFactory elementFactory, String localName, V variable, diff --git a/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/FieldCanBeLocalInspection.java b/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/FieldCanBeLocalInspection.java index ce527ab84af5..d49df52c8512 100644 --- a/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/FieldCanBeLocalInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/varScopeCanBeNarrowed/FieldCanBeLocalInspection.java @@ -70,12 +70,7 @@ public class FieldCanBeLocalInspection extends FieldCanBeLocalInspectionBase { } if (element != null) { final PsiElement finalElement = element; - Runnable runnable = () -> { - beforeDelete(project, variable, finalElement); - variable.normalizeDeclaration(); - variable.delete(); - }; - ApplicationManager.getApplication().runWriteAction(runnable); + ApplicationManager.getApplication().runWriteAction(() -> deleteSourceVariable(project, variable, finalElement)); } return element; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/afterBlocksInSameMethod.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/afterBlocksInSameMethod.java index 9f5a1571289b..c0ba31a383ca 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/afterBlocksInSameMethod.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/afterBlocksInSameMethod.java @@ -5,6 +5,7 @@ class ITest { public IntelliJBugConvertToLocal(int x, int z) { + //my comment to keep in code ArrayList mayBeLocal = new ArrayList(); if (x == 5) { mayBeLocal.add("jjj"); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/beforeBlocksInSameMethod.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/beforeBlocksInSameMethod.java index b4e42ef32dcf..3e7bdaf51ee7 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/beforeBlocksInSameMethod.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convert2Local/beforeBlocksInSameMethod.java @@ -3,7 +3,7 @@ import java.util.ArrayList; class ITest { - private ArrayList mayBeLocal = new ArrayList(); + private ArrayList mayBeLocal = new ArrayList(); //my comment to keep in code public IntelliJBugConvertToLocal(int x, int z) {