Convert lambda to reference: fix it works correctly in anonymous object

#KT-31682 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-05-28 09:32:18 +09:00
committed by Yan Zhulanow
parent b9fab1123d
commit 8f3392d635
6 changed files with 76 additions and 0 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContext.FUNCTION
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
@@ -248,6 +249,7 @@ open class ConvertLambdaToReferenceIntention(textGetter: () -> String) : SelfTar
lambdaParameterType.getReceiverTypeFromFunctionType()?.fqName?.asString()
receiver == null || descriptor?.isCompanionObject() == true -> ""
receiver is ExtensionReceiver ||
descriptor?.let { DescriptorUtils.isAnonymousObject(it) } == true ||
lambdaExpression.getResolutionScope().getImplicitReceiversHierarchy().size == 1 -> "this"
else -> descriptor?.name?.let { "this@$it" }
} ?: return null
@@ -0,0 +1,15 @@
interface I {
fun foo(i: Int)
}
fun create(): I {
return object : I {
override fun foo(i: Int) {
bar {<caret> baz(it) }
}
fun bar(f: (Int) -> Unit) {}
fun baz(i: Int) {}
}
}
@@ -0,0 +1,15 @@
interface I {
fun foo(i: Int)
}
fun create(): I {
return object : I {
override fun foo(i: Int) {
bar(this::baz)
}
fun bar(f: (Int) -> Unit) {}
fun baz(i: Int) {}
}
}
@@ -0,0 +1,17 @@
interface I {
fun foo(i: Int)
}
class C {
fun create(): I {
return object : I {
override fun foo(i: Int) {
bar {<caret> baz(it) }
}
fun bar(f: (Int) -> Unit) {}
fun baz(i: Int) {}
}
}
}
@@ -0,0 +1,17 @@
interface I {
fun foo(i: Int)
}
class C {
fun create(): I {
return object : I {
override fun foo(i: Int) {
bar(this::baz)
}
fun bar(f: (Int) -> Unit) {}
fun baz(i: Int) {}
}
}
}
@@ -5060,6 +5060,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertLambdaToReference/goodNamedOrder.kt");
}
@TestMetadata("inAnonymousObject.kt")
public void testInAnonymousObject() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/inAnonymousObject.kt");
}
@TestMetadata("inAnonymousObject2.kt")
public void testInAnonymousObject2() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/inAnonymousObject2.kt");
}
@TestMetadata("inner.kt")
public void testInner() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/inner.kt");