From 53c49b633322ee605269b70b46d270e019a8ea76 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 19 Jun 2014 16:17:46 +0400 Subject: [PATCH] Shorten References: Do not shorten qualified expression if receiver values are changed #KT-5180 Fixed --- .../jet/plugin/codeInsight/ShortenReferences.kt | 16 ++++++++++++++-- .../extractThis/explicitThisInExtension.kt.after | 2 +- .../implicitAndExplicitThisInExtension.kt.after | 2 +- idea/testData/shortenRefs/noShortening.kt | 7 +++++++ idea/testData/shortenRefs/noShortening.kt.after | 7 +++++++ .../shortenRefs/ShortenRefsTestGenerated.java | 5 +++++ 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 idea/testData/shortenRefs/noShortening.kt create mode 100644 idea/testData/shortenRefs/noShortening.kt.after diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt index 6d1aa4df7fc..13f1ffa4586 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/ShortenReferences.kt @@ -33,9 +33,11 @@ import org.jetbrains.jet.lang.resolve.java.descriptor.JavaPropertyDescriptor import org.jetbrains.jet.lang.resolve.java.lazy.descriptors.LazyPackageFragmentForJavaClass import org.jetbrains.jet.lang.resolve.java.descriptor.JavaMethodDescriptor import com.intellij.openapi.util.TextRange -import com.intellij.psi.SmartPointerManager import com.intellij.psi.PsiDocumentManager import org.jetbrains.jet.plugin.caches.resolve.getLazyResolveSession +import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall +import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue +import org.jetbrains.jet.renderer.DescriptorRenderer.FQ_NAMES_IN_TYPES public object ShortenReferences { public fun process(element: JetElement) { @@ -313,7 +315,13 @@ public object ShortenReferences { private fun resolveState(referenceExpression: JetReferenceExpression, bindingContext: BindingContext): Any? { val target = bindingContext[BindingContext.REFERENCE_TARGET, referenceExpression] - if (target != null) return target.asString() + if (target != null) { + val resolvedCallKey = (referenceExpression.getParent() as? JetThisExpression) ?: referenceExpression + val resolvedCall = bindingContext[BindingContext.RESOLVED_CALL, resolvedCallKey] + if (resolvedCall != null) return resolvedCall.asString() + + return target.asString() + } val targets = bindingContext[BindingContext.AMBIGUOUS_REFERENCE_TARGET, referenceExpression] if (targets != null) return HashSet(targets.map{it.asString()}) @@ -334,6 +342,10 @@ public object ShortenReferences { private fun DeclarationDescriptor.asString() = DescriptorRenderer.FQ_NAMES_IN_TYPES.render(this) + private fun ResolvedCall<*>.asString(): String { + return "${getReceiverArgument()}, ${getThisObject()} -> ${getResultingDescriptor()?.let {FQ_NAMES_IN_TYPES.render(it)}}" + } + //TODO: do we need this "IfNeeded" check? private fun addImportIfNeeded(descriptor: DeclarationDescriptor, file: JetFile) { ImportInsertHelper.addImportDirectiveIfNeeded(DescriptorUtils.getFqNameSafe(descriptor), file) diff --git a/idea/testData/refactoring/extractFunction/parameters/extractThis/explicitThisInExtension.kt.after b/idea/testData/refactoring/extractFunction/parameters/extractThis/explicitThisInExtension.kt.after index db4d498bd74..bec9686c4dc 100644 --- a/idea/testData/refactoring/extractFunction/parameters/extractThis/explicitThisInExtension.kt.after +++ b/idea/testData/refactoring/extractFunction/parameters/extractThis/explicitThisInExtension.kt.after @@ -6,5 +6,5 @@ fun Z.foo(): Int { } fun Z.i(): Int { - return a + return this.a } diff --git a/idea/testData/refactoring/extractFunction/parameters/extractThis/implicitAndExplicitThisInExtension.kt.after b/idea/testData/refactoring/extractFunction/parameters/extractThis/implicitAndExplicitThisInExtension.kt.after index 5686b9478dd..ffa4e81dd31 100644 --- a/idea/testData/refactoring/extractFunction/parameters/extractThis/implicitAndExplicitThisInExtension.kt.after +++ b/idea/testData/refactoring/extractFunction/parameters/extractThis/implicitAndExplicitThisInExtension.kt.after @@ -6,5 +6,5 @@ fun Z.foo(): Int { } fun Z.i(): Int { - return a + a + return this.a + a } diff --git a/idea/testData/shortenRefs/noShortening.kt b/idea/testData/shortenRefs/noShortening.kt new file mode 100644 index 00000000000..89b75813155 --- /dev/null +++ b/idea/testData/shortenRefs/noShortening.kt @@ -0,0 +1,7 @@ +trait T : Iterable + +abstract class C : T { + fun foo(c: C) { + if (c.any { it.size > 1 }) {} + } +} \ No newline at end of file diff --git a/idea/testData/shortenRefs/noShortening.kt.after b/idea/testData/shortenRefs/noShortening.kt.after new file mode 100644 index 00000000000..bd5285f3b1c --- /dev/null +++ b/idea/testData/shortenRefs/noShortening.kt.after @@ -0,0 +1,7 @@ +trait T : Iterable + +abstract class C : T { + fun foo(c: C) { + if (c.any { it.size > 1 }) {} + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java index 27fad679ecc..b7af568ad50 100644 --- a/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/shortenRefs/ShortenRefsTestGenerated.java @@ -47,6 +47,11 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest { doTest("idea/testData/shortenRefs/JavaStaticMethod.kt"); } + @TestMetadata("noShortening.kt") + public void testNoShortening() throws Exception { + doTest("idea/testData/shortenRefs/noShortening.kt"); + } + @TestMetadata("idea/testData/shortenRefs/constructor") public static class Constructor extends AbstractShortenRefsTest { public void testAllFilesPresentInConstructor() throws Exception {