"Convert lambda to reference": fix case with companion object
So #KT-24385 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0ae2054af4
commit
88a5eb24d5
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.resolve.BindingContext.FUNCTION
|
||||
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isCompanionObject
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
|
||||
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
@@ -230,10 +231,11 @@ open class ConvertLambdaToReferenceIntention(text: String) :
|
||||
val calleeReferenceExpression = singleStatement.calleeExpression as? KtNameReferenceExpression ?: return null
|
||||
val resolvedCall = calleeReferenceExpression.resolveToCall() ?: return null
|
||||
val receiver = resolvedCall.dispatchReceiver ?: resolvedCall.extensionReceiver
|
||||
val descriptor by lazy { receiver?.type?.constructor?.declarationDescriptor }
|
||||
val receiverText = when {
|
||||
receiver == null -> ""
|
||||
receiver == null || descriptor?.isCompanionObject() == true -> ""
|
||||
lambdaExpression.getResolutionScope().getImplicitReceiversHierarchy().size == 1 -> "this"
|
||||
else -> receiver.type.constructor.declarationDescriptor?.name?.let { "this@$it" } ?: return null
|
||||
else -> descriptor?.name?.let { "this@$it" } ?: return null
|
||||
}
|
||||
"$receiverText::${singleStatement.getCallReferencedName()}"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun foo(s: String) = 1
|
||||
}
|
||||
val f = {<caret> s: String -> foo(s) }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun foo(s: String) = 1
|
||||
}
|
||||
val f = ::foo
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun foo(s: String) = 1
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
val f = {<caret> s: String -> C.foo(s) }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
companion object {
|
||||
fun foo(s: String) = 1
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
val f = C.Companion::foo
|
||||
}
|
||||
@@ -5095,6 +5095,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/companion.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("companion2.kt")
|
||||
public void testCompanion2() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/companion2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("companion3.kt")
|
||||
public void testCompanion3() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/companion3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("constructor.kt")
|
||||
public void testConstructor() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/constructor.kt");
|
||||
|
||||
Reference in New Issue
Block a user