introduce variable placed inside lambda body: ensure initializer not changed (IDEA-142361)

This commit is contained in:
Anna Kozlova
2015-07-08 20:37:15 +03:00
parent 56d75da60a
commit 6804eceb62
4 changed files with 38 additions and 3 deletions
@@ -288,7 +288,7 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
LOG.assertTrue(expression.isValid(), expression.getText());
stringUsages.add(Pair.<PsiElement, TextRange>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.<PsiElement, TextRange>create(expr, new TextRange(0, expr.getTextLength())));
@@ -297,8 +297,11 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
@Override
protected void addReferenceAtCaret(Collection<PsiReference> 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);
}
@@ -0,0 +1,10 @@
class Test {
public void test(final DocumentDataHookUp hookUp) {
Runnable r = () -> hookUp.getDocument().replace("".leng<caret>th());
}
private class DocumentDataHookUp {
public String replace(int i1) {
return null;
}
}
}
@@ -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;
}
}
}
@@ -64,6 +64,15 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
});
}
public void testPlaceInsideLambda() throws Exception {
doTest(new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
type("expr");
}
});
}
public void testRanges() throws Exception {
doTest(new Pass<AbstractInplaceIntroducer>() {
@Override