diff --git a/idea/idea-completion/testData/handlers/basic/JavaSAM.kt b/idea/idea-completion/testData/handlers/basic/JavaSAM.kt new file mode 100644 index 00000000000..33fd57be87f --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/JavaSAM.kt @@ -0,0 +1,5 @@ +fun f() { + Cal +} + +// ELEMENT: "Callable" \ No newline at end of file diff --git a/idea/idea-completion/testData/handlers/basic/JavaSAM.kt.after b/idea/idea-completion/testData/handlers/basic/JavaSAM.kt.after new file mode 100644 index 00000000000..8c6ab3735a1 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/JavaSAM.kt.after @@ -0,0 +1,7 @@ +import java.util.concurrent.Callable + +fun f() { + Callable +} + +// ELEMENT: "Callable" \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java index 5cc6e06501c..f5a4f298879 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java @@ -113,6 +113,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion doTest(fileName); } + @TestMetadata("JavaSAM.kt") + public void testJavaSAM() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/JavaSAM.kt"); + doTest(fileName); + } + @TestMetadata("KT11633.kt") public void testKT11633() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/KT11633.kt"); diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt index a764c61ea77..f54c790d63a 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.resolve.BindingContext @@ -432,8 +433,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT val varAsFunResolvedCall = callee.getResolvedCall(bindingContext) as? VariableAsFunctionResolvedCall if (targets.isEmpty()) return Skip - val selectorCopy = selector.copy() as KtReferenceExpression - val newContext = selectorCopy.analyzeAsReplacement(element, bindingContext, resolutionFacade) + val (newContext, selectorCopy) = copyAndAnalyzeSelector(element, bindingContext) val newCallee = selectorCopy.getCalleeExpressionIfAny() as KtReferenceExpression val targetsWhenShort = newCallee.targets(newContext) val varAsFunResolvedCallWhenShort = newCallee.getResolvedCall(newContext) as? VariableAsFunctionResolvedCall @@ -465,6 +465,22 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT } } + private fun copyAndAnalyzeSelector(element: KtDotQualifiedExpression, bindingContext: BindingContext): Pair { + val selector = element.selectorExpression!! + val qualifiedAbove = element.getQualifiedExpressionForReceiver() + if (qualifiedAbove == null) { + val copied = selector.copied() + val newBindingContext = copied.analyzeAsReplacement(element, bindingContext, resolutionFacade) + return newBindingContext to copied + } + else { + val qualifiedAboveCopy = qualifiedAbove.copied() + qualifiedAboveCopy.receiverExpression.replace(selector) + val newBindingContext = qualifiedAboveCopy.analyzeAsReplacement(qualifiedAbove, bindingContext, resolutionFacade) + return newBindingContext to qualifiedAboveCopy.receiverExpression + } + } + private fun targetsMatch(targets1: Collection, targets2: Collection): Boolean { if (targets1.size != targets2.size) return false if (targets1.size == 1) { diff --git a/idea/testData/shortenRefs/kt14370.dependency.kt b/idea/testData/shortenRefs/kt14370.dependency.kt new file mode 100644 index 00000000000..2f0c278bc56 --- /dev/null +++ b/idea/testData/shortenRefs/kt14370.dependency.kt @@ -0,0 +1,5 @@ +package dependency + +interface PseudoSAM + +fun PseudoSAM(p: Int): PseudoSAM = TODO() \ No newline at end of file diff --git a/idea/testData/shortenRefs/kt14370.kt b/idea/testData/shortenRefs/kt14370.kt new file mode 100644 index 00000000000..a5347d75539 --- /dev/null +++ b/idea/testData/shortenRefs/kt14370.kt @@ -0,0 +1,3 @@ +fun foo() { + dependency.PseudoSAM.xxx +} \ No newline at end of file diff --git a/idea/testData/shortenRefs/kt14370.kt.after b/idea/testData/shortenRefs/kt14370.kt.after new file mode 100644 index 00000000000..fa0536ff91b --- /dev/null +++ b/idea/testData/shortenRefs/kt14370.kt.after @@ -0,0 +1,5 @@ +import dependency.PseudoSAM + +fun foo() { + PseudoSAM.xxx +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java b/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java index 31b15f0da10..fdd82294e2c 100644 --- a/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java @@ -89,6 +89,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest { doTest(fileName); } + @TestMetadata("kt14370.kt") + public void testKt14370() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/kt14370.kt"); + doTest(fileName); + } + @TestMetadata("noShortening.kt") public void testNoShortening() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/noShortening.kt");