Convert lambda to reference produces: fix case with qualified this

So #KT-19977 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-11-28 14:00:03 +03:00
committed by Mikhail Glukhikh
parent 50dc9a14a6
commit 0071ca64c7
6 changed files with 69 additions and 4 deletions
@@ -29,11 +29,13 @@ import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.approximateFlexibleTypes
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.psi.*
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.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
import org.jetbrains.kotlin.types.isDynamic
import org.jetbrains.kotlin.types.isError
@@ -222,10 +224,13 @@ open class ConvertLambdaToReferenceIntention(text: String) :
val calleeReferenceExpression = singleStatement.calleeExpression as? KtNameReferenceExpression ?: return null
val context = singleStatement.analyze()
val resolvedCall = calleeReferenceExpression.getResolvedCall(context) ?: return null
if (resolvedCall.dispatchReceiver != null || resolvedCall.extensionReceiver != null)
"this::${singleStatement.getCallReferencedName()}"
else
"::${singleStatement.getCallReferencedName()}"
val receiver = resolvedCall.dispatchReceiver ?: resolvedCall.extensionReceiver
val receiverText = when {
receiver == null -> ""
lambdaExpression.getResolutionScope().getImplicitReceiversHierarchy().size == 1 -> "this"
else -> receiver.type.constructor.declarationDescriptor?.name?.let { "this@$it" } ?: return null
}
"$receiverText::${singleStatement.getCallReferencedName()}"
}
is KtDotQualifiedExpression -> {
val selector = singleStatement.selectorExpression
@@ -0,0 +1,13 @@
// IS_APPLICABLE: true
// WITH_RUNTIME
class Test {
fun test() {
with(Any()) {
val f = { s: String<caret> -> foo(s) }
}
}
}
fun Test.foo(s: String) {}
@@ -0,0 +1,13 @@
// IS_APPLICABLE: true
// WITH_RUNTIME
class Test {
fun test() {
with(Any()) {
val f = this@Test::foo
}
}
}
fun Test.foo(s: String) {}
@@ -0,0 +1,11 @@
// IS_APPLICABLE: true
// WITH_RUNTIME
class Test {
fun test() {
with(Any()) {
val f = { s: String<caret> -> foo(s) }
}
}
fun foo(s: String) {}
}
@@ -0,0 +1,11 @@
// IS_APPLICABLE: true
// WITH_RUNTIME
class Test {
fun test() {
with(Any()) {
val f = this@Test::foo
}
}
fun foo(s: String) {}
}
@@ -5120,6 +5120,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("extentionOuterScope.kt")
public void testExtentionOuterScope() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertLambdaToReference/extentionOuterScope.kt");
doTest(fileName);
}
@TestMetadata("fqNameForReceiver.kt")
public void testFqNameForReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertLambdaToReference/fqNameForReceiver.kt");
@@ -5198,6 +5204,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("memberOuterScope.kt")
public void testMemberOuterScope() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertLambdaToReference/memberOuterScope.kt");
doTest(fileName);
}
@TestMetadata("memberViaThis.kt")
public void testMemberViaThis() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertLambdaToReference/memberViaThis.kt");