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 957718086085..7f446347c425 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -288,7 +288,7 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer LOG.assertTrue(expression.isValid(), expression.getText()); stringUsages.add(Pair.create(expression, new TextRange(0, expression.getTextLength()))); } - } else if (getExpr() != null && !myReplaceSelf) { + } else if (getExpr() != null && !myReplaceSelf && getExpr().getParent() != getVariable()) { final PsiExpression expr = getExpr(); LOG.assertTrue(expr.isValid(), expr.getText()); stringUsages.add(Pair.create(expr, new TextRange(0, expr.getTextLength()))); @@ -297,8 +297,11 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer @Override protected void addReferenceAtCaret(Collection refs) { - if (!isReplaceAllOccurrences() && getExpr() == null && !myReplaceSelf) { - return; + if (!isReplaceAllOccurrences()) { + final PsiExpression expr = getExpr(); + if (expr == null && !myReplaceSelf || expr != null && expr.getParent() == getVariable()) { + return; + } } super.addReferenceAtCaret(refs); } diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda.java new file mode 100644 index 000000000000..2bafe62308c2 --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda.java @@ -0,0 +1,10 @@ +class Test { + public void test(final DocumentDataHookUp hookUp) { + Runnable r = () -> hookUp.getDocument().replace("".length()); + } + private class DocumentDataHookUp { + public String replace(int i1) { + return null; + } + } +} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda_after.java new file mode 100644 index 000000000000..549f1ba4ea4c --- /dev/null +++ b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/PlaceInsideLambda_after.java @@ -0,0 +1,13 @@ +class Test { + public void test(final DocumentDataHookUp hookUp) { + Runnable r = () -> { + int expr = "".length(); + hookUp.getDocument().replace(expr); + }; + } + private class DocumentDataHookUp { + public String replace(int i1) { + return null; + } + } +} diff --git a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java index 3f08022e756f..e0b764dec803 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java @@ -64,6 +64,15 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe }); } + public void testPlaceInsideLambda() throws Exception { + doTest(new Pass() { + @Override + public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) { + type("expr"); + } + }); + } + public void testRanges() throws Exception { doTest(new Pass() { @Override