Introduce Variable: Do not compare explicit receivers using ReceiverValue

#KT-5319 Fixed
This commit is contained in:
Alexey Sedunov
2014-06-25 13:56:33 +04:00
parent 1452ffec6d
commit cd44bc3fb0
10 changed files with 116 additions and 6 deletions
@@ -461,6 +461,23 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
@Override
public void visitExpression(@NotNull JetExpression expression) {
if (PsiEquivalenceUtil.areElementsEquivalent(expression, actualExpression, null, new Comparator<PsiElement>() {
private boolean compareCalleesAndReceivers(@NotNull ResolvedCall<?> rc1, @NotNull ResolvedCall<?> rc2) {
if (rc1.getResultingDescriptor() != rc2.getResultingDescriptor() ||
rc1.getExplicitReceiverKind() != rc2.getExplicitReceiverKind()) return false;
switch (rc1.getExplicitReceiverKind()) {
case NO_EXPLICIT_RECEIVER:
return rc1.getReceiverArgument() == rc2.getReceiverArgument()
&& rc1.getThisObject() == rc2.getThisObject();
case RECEIVER_ARGUMENT:
return rc1.getThisObject() == rc2.getThisObject();
case THIS_OBJECT:
return rc1.getReceiverArgument() == rc2.getReceiverArgument();
default:
return true;
}
}
@Override
public int compare(@NotNull PsiElement element1, @NotNull PsiElement element2) {
if (element1.getNode().getElementType() == JetTokens.IDENTIFIER &&
@@ -472,12 +489,7 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
ResolvedCall<?> rc1 = bindingContext.get(BindingContext.RESOLVED_CALL, expr1);
ResolvedCall<?> rc2 = bindingContext.get(BindingContext.RESOLVED_CALL, expr2);
if (rc1 == null || rc2 == null) return rc1 == rc2 ? 0 : 1;
return rc1.getResultingDescriptor() == rc2.getResultingDescriptor()
&& rc1.getExplicitReceiverKind() == rc2.getExplicitReceiverKind()
&& rc1.getReceiverArgument() == rc2.getReceiverArgument()
&& rc1.getThisObject() == rc2.getThisObject() ? 0 : 1;
return (rc1 != null && rc2 != null) && compareCalleesAndReceivers(rc1, rc2) ? 0 : 1;
}
}
if (!element1.textMatches(element2)) {
@@ -0,0 +1,12 @@
fun main(args: Array<String>) {
with(A()) {
println(<selection>prop</selection>)
println(prop)
}
}
class A {
val prop = 1
}
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
@@ -0,0 +1,13 @@
fun main(args: Array<String>) {
with(A()) {
val i = prop
println(i)
println(i)
}
}
class A {
val prop = 1
}
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
@@ -0,0 +1,4 @@
fun main(args: Array<String>) {
println(<selection>a1</selection>)
println(a2)
}
@@ -0,0 +1,5 @@
fun main(args: Array<String>) {
val a = a1
println(a)
println(a2)
}
@@ -0,0 +1,9 @@
fun main(args: Array<String>) {
val myA = A()
println(<selection>myA.prop</selection>)
println(myA.prop)
}
class A {
val prop = 1
}
@@ -0,0 +1,10 @@
fun main(args: Array<String>) {
val myA = A()
val i = myA.prop
println(i)
println(i)
}
class A {
val prop = 1
}
@@ -0,0 +1,12 @@
class Bar {
}
class Foo() {
fun Bar.invoke(): Int = 1
}
fun main(args: Array<String>) {
val f = Foo()
println(<selection>Bar().f()</selection>)
println(Bar().f())
}
@@ -0,0 +1,13 @@
class Bar {
}
class Foo() {
fun Bar.invoke(): Int = 1
}
fun main(args: Array<String>) {
val f = Foo()
val i = Bar().f()
println(i)
println(i)
}
@@ -133,11 +133,26 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/noConflictWithInnerVariable.kt");
}
@TestMetadata("NoExplicitReceivers.kt")
public void testNoExplicitReceivers() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/NoExplicitReceivers.kt");
}
@TestMetadata("NoExplicitReceiversUnresolved.kt")
public void testNoExplicitReceiversUnresolved() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/NoExplicitReceiversUnresolved.kt");
}
@TestMetadata("nonEquivalentReceivers.kt")
public void testNonEquivalentReceivers() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt");
}
@TestMetadata("OneExplicitReceiver.kt")
public void testOneExplicitReceiver() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/OneExplicitReceiver.kt");
}
@TestMetadata("ReplaceOccurence.kt")
public void testReplaceOccurence() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/ReplaceOccurence.kt");
@@ -158,6 +173,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/StringInjection.kt");
}
@TestMetadata("TwoExplicitReceivers.kt")
public void testTwoExplicitReceivers() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/TwoExplicitReceivers.kt");
}
@TestMetadata("WhenAddBlock.kt")
public void testWhenAddBlock() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/WhenAddBlock.kt");