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 3ceb522cef64..aac8eb5b523f 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -36,6 +36,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.codeStyle.VariableKind; import com.intellij.psi.scope.processor.VariablesProcessor; +import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.TypeConversionUtil; @@ -55,6 +56,7 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -396,6 +398,15 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer final PsiVariable variable = ApplicationManager.getApplication().runWriteAction( IntroduceVariableBase.introduce(myProject, myExpr, myEditor, myChosenAnchor.getElement(), getOccurrences(), mySettings)); PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myEditor.getDocument()); + + if (isReplaceAllOccurrences()) { + List occurrences = new ArrayList<>(); + ReferencesSearch.search(variable).forEach(reference -> { + occurrences.add(createMarker(reference.getElement())); + }); + setOccurrenceMarkers(occurrences); + } + final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(variable, PsiDeclarationStatement.class); myPointer = declarationStatement != null ? SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(declarationStatement) : null; myEditor.putUserData(ReassignVariableUtil.DECLARATION_KEY, myPointer); diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences.java new file mode 100644 index 000000000000..029d6fb846b8 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences.java @@ -0,0 +1,5 @@ +class Test { + { + Runnable r = () -> System.out.println("" + ""); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences_after.java new file mode 100644 index 000000000000..db9ec26fdc87 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambdaBodyMultipleOccurrences_after.java @@ -0,0 +1,8 @@ +class Test { + { + Runnable r = () -> { + String expr = ""; + System.out.println(expr + 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 321a3b1d9970..d9c460cb06c5 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java @@ -115,7 +115,16 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe } }); } - + + public void testPlaceInsideLambdaBodyMultipleOccurrences() throws Exception { + doTestReplaceChoice(OccurrencesChooser.ReplaceChoice.ALL, new Pass() { + @Override + public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) { + type("expr"); + } + }); + } + public void testRanges() throws Exception { doTest(new Pass() { @Override