Fixed ShortenReferences for extension on companion object
This commit is contained in:
@@ -538,13 +538,25 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
|
|
||||||
private fun canBePossibleToDropReceiver(element: KtDotQualifiedExpression, bindingContext: BindingContext): Boolean {
|
private fun canBePossibleToDropReceiver(element: KtDotQualifiedExpression, bindingContext: BindingContext): Boolean {
|
||||||
val receiver = element.receiverExpression
|
val receiver = element.receiverExpression
|
||||||
if (receiver is KtThisExpression) return true
|
val nameRef = when (receiver) {
|
||||||
val qualifier = bindingContext[BindingContext.QUALIFIER, receiver] ?: return false
|
is KtThisExpression -> return true
|
||||||
val classDescriptor = qualifier.descriptor as? ClassDescriptor ?: return true
|
is KtNameReferenceExpression -> receiver
|
||||||
if (classDescriptor.kind != ClassKind.OBJECT) return true
|
is KtDotQualifiedExpression -> receiver.selectorExpression as? KtNameReferenceExpression ?: return false
|
||||||
// for object receiver we should additionally check that it's dispatch receiver (that is the member is inside the object)
|
else -> return false
|
||||||
val resolvedCall = element.getResolvedCall(bindingContext) ?: return false
|
}
|
||||||
return resolvedCall.explicitReceiverKind == ExplicitReceiverKind.DISPATCH_RECEIVER
|
val targetDescriptor = bindingContext[BindingContext.REFERENCE_TARGET, nameRef]
|
||||||
|
when (targetDescriptor) {
|
||||||
|
is ClassDescriptor -> {
|
||||||
|
if (targetDescriptor.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
|
||||||
|
}
|
||||||
|
|
||||||
|
is PackageViewDescriptor -> return true
|
||||||
|
|
||||||
|
else -> return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun copyShortenAndAnalyze(
|
private fun copyShortenAndAnalyze(
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package dependency
|
||||||
|
|
||||||
|
class AClass {
|
||||||
|
companion object {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun AClass.Companion.extFun(){}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import dependency.extFun
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
<selection>dependency.AClass.extFun()</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import dependency.AClass
|
||||||
|
import dependency.extFun
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
AClass.extFun()
|
||||||
|
}
|
||||||
@@ -59,6 +59,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
|
|||||||
runTest("idea/testData/shortenRefs/descriptorsChangeAfterImportInsertion.kt");
|
runTest("idea/testData/shortenRefs/descriptorsChangeAfterImportInsertion.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("extensionForCompanionObject.kt")
|
||||||
|
public void testExtensionForCompanionObject() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/extensionForCompanionObject.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("extensionForObject.kt")
|
@TestMetadata("extensionForObject.kt")
|
||||||
public void testExtensionForObject() throws Exception {
|
public void testExtensionForObject() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/extensionForObject.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/extensionForObject.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user