Introduce Variable: Compare receivers (both implicit and explicit) when looking for expression occurrences

#KT-4819 Fixed
This commit is contained in:
Alexey Sedunov
2014-04-18 17:31:35 +04:00
parent 40ee454e67
commit e052a64767
4 changed files with 50 additions and 8 deletions
@@ -40,6 +40,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.ObservableBindingTrace;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.PackageType;
@@ -465,14 +466,15 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
element2.getParent() instanceof JetSimpleNameExpression) {
JetSimpleNameExpression expr1 = (JetSimpleNameExpression)element1.getParent();
JetSimpleNameExpression expr2 = (JetSimpleNameExpression)element2.getParent();
DeclarationDescriptor descr1 = bindingContext.get(BindingContext.REFERENCE_TARGET, expr1);
DeclarationDescriptor descr2 = bindingContext.get(BindingContext.REFERENCE_TARGET, expr2);
if (descr1 != descr2) {
return 1;
}
else {
return 0;
}
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;
}
}
if (!element1.textMatches(element2)) {
@@ -0,0 +1,17 @@
class A {
val value : Int = 0
}
fun foo(body: A.() -> Unit) {}
fun bar() {
foo {
print(<selection>value</selection>)
print(value)
}
foo {
print(value)
print(value)
}
}
@@ -0,0 +1,18 @@
class A {
val value : Int = 0
}
fun foo(body: A.() -> Unit) {}
fun bar() {
foo {
val i = value
print(i)
print(i)
}
foo {
print(value)
print(value)
}
}
@@ -128,6 +128,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/ManyOccurrences.kt");
}
@TestMetadata("nonEquivalentReceivers.kt")
public void testNonEquivalentReceivers() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/nonEquivalentReceivers.kt");
}
@TestMetadata("ReplaceOccurence.kt")
public void testReplaceOccurence() throws Exception {
doIntroduceVariableTest("idea/testData/refactoring/introduceVariable/ReplaceOccurence.kt");