diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java index 04042c004add..9a5a59ff8a80 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -52,6 +52,7 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.Collection; import java.util.List; /** @@ -272,6 +273,14 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer } } + @Override + protected void addReferenceAtCaret(Collection refs) { + if (!isReplaceAllOccurrences() && getExpr() == null && !myReplaceSelf) { + return; + } + super.addReferenceAtCaret(refs); + } + private static void appendTypeCasts(List occurrenceMarkers, PsiFile file, Project project, diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename.java new file mode 100644 index 000000000000..eeb6ae4c7329 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename.java @@ -0,0 +1,5 @@ +class C { + { + Runnable r = () -> System.out.println("extract me"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename_after.java new file mode 100644 index 000000000000..e9ef5b66af89 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/placeInsideLoopAndRename_after.java @@ -0,0 +1,8 @@ +class C { + { + Runnable r = () -> { + String expr = "extract me"; + System.out.println(expr); + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java index 6e75fbb2bdd8..4c434ed86517 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java @@ -55,6 +55,15 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe }); } + public void testPlaceInsideLoopAndRename() throws Exception { + doTest(new Pass() { + @Override + public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) { + type("expr"); + } + }); + } + public void testRanges() throws Exception { doTest(new Pass() { @Override