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 4d4ab0cd8e4..eb437ca0f19 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 @@ -444,8 +444,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT element: KtDotQualifiedExpression, bindingContext: BindingContext ): AnalyzeQualifiedElementResult { - val receiver = element.receiverExpression - if (receiver !is KtThisExpression && bindingContext[BindingContext.QUALIFIER, receiver] == null) return AnalyzeQualifiedElementResult.Skip + if (!canBePossibleToDropReceiver(element, bindingContext)) return AnalyzeQualifiedElementResult.Skip if (PsiTreeUtil.getParentOfType( element, @@ -503,6 +502,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT } + val receiver = element.receiverExpression if (receiver is KtThisExpression) { if (!targetsMatch) return AnalyzeQualifiedElementResult.Skip val originalCall = selector.getResolvedCall(bindingContext) ?: return AnalyzeQualifiedElementResult.Skip @@ -536,6 +536,17 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT } } + private fun canBePossibleToDropReceiver(element: KtDotQualifiedExpression, bindingContext: BindingContext): Boolean { + val receiver = element.receiverExpression + if (receiver is KtThisExpression) return true + val qualifier = bindingContext[BindingContext.QUALIFIER, receiver] ?: return false + val classDescriptor = qualifier.descriptor as? ClassDescriptor ?: return true + if (classDescriptor.kind != ClassKind.OBJECT) return true + // for object receiver we should additionally check that it's dispatch receiver (that is the member is inside the object) + val resolvedCall = element.getResolvedCall(bindingContext) ?: return false + return resolvedCall.explicitReceiverKind == ExplicitReceiverKind.DISPATCH_RECEIVER + } + private fun copyShortenAndAnalyze( element: KtDotQualifiedExpression, bindingContext: BindingContext diff --git a/idea/testData/shortenRefs/extensionForObject.dependency.kt b/idea/testData/shortenRefs/extensionForObject.dependency.kt new file mode 100644 index 00000000000..3f2738e0992 --- /dev/null +++ b/idea/testData/shortenRefs/extensionForObject.dependency.kt @@ -0,0 +1,5 @@ +package dependency + +object AnObject + +fun AnObject.extFun(){} diff --git a/idea/testData/shortenRefs/extensionForObject.kt b/idea/testData/shortenRefs/extensionForObject.kt new file mode 100644 index 00000000000..0709cf1e212 --- /dev/null +++ b/idea/testData/shortenRefs/extensionForObject.kt @@ -0,0 +1,5 @@ +import dependency.extFun + +fun foo() { + dependency.AnObject.extFun() +} \ No newline at end of file diff --git a/idea/testData/shortenRefs/extensionForObject.kt.after b/idea/testData/shortenRefs/extensionForObject.kt.after new file mode 100644 index 00000000000..d967bcfa25b --- /dev/null +++ b/idea/testData/shortenRefs/extensionForObject.kt.after @@ -0,0 +1,6 @@ +import dependency.AnObject +import dependency.extFun + +fun foo() { + AnObject.extFun() +} \ 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 d554132d3ee..4644dea5d4b 100644 --- a/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java @@ -59,6 +59,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest { runTest("idea/testData/shortenRefs/descriptorsChangeAfterImportInsertion.kt"); } + @TestMetadata("extensionForObject.kt") + public void testExtensionForObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/extensionForObject.kt"); + doTest(fileName); + } + @TestMetadata("extensionFunctionVarInvokedWithQualifier.kt") public void testExtensionFunctionVarInvokedWithQualifier() throws Exception { runTest("idea/testData/shortenRefs/extensionFunctionVarInvokedWithQualifier.kt");