"Convert lambda to reference": fix case with extension 'this'
#KT-21999 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
2bf3b435f2
commit
0a5aa2b60c
@@ -37,6 +37,7 @@ 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.receivers.ExtensionReceiver
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
|
||||
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
@@ -234,7 +235,7 @@ open class ConvertLambdaToReferenceIntention(text: String) :
|
||||
val descriptor by lazy { receiver?.type?.constructor?.declarationDescriptor }
|
||||
val receiverText = when {
|
||||
receiver == null || descriptor?.isCompanionObject() == true -> ""
|
||||
lambdaExpression.getResolutionScope().getImplicitReceiversHierarchy().size == 1 -> "this"
|
||||
receiver is ExtensionReceiver || lambdaExpression.getResolutionScope().getImplicitReceiversHierarchy().size == 1 -> "this"
|
||||
else -> descriptor?.name?.let { "this@$it" } ?: return null
|
||||
}
|
||||
"$receiverText::${singleStatement.getCallReferencedName()}"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(f: () -> Unit) {}
|
||||
|
||||
class Bar {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class Test {
|
||||
fun test() {
|
||||
with(Bar()) {
|
||||
foo { <caret>bar() }
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(f: () -> Unit) {}
|
||||
|
||||
class Bar {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class Test {
|
||||
fun test() {
|
||||
with(Bar()) {
|
||||
foo(this::bar)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(f: () -> Unit) {}
|
||||
|
||||
class Bar {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class Test {
|
||||
fun test() {
|
||||
Bar().run {
|
||||
foo { <caret>bar() }
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(f: () -> Unit) {}
|
||||
|
||||
class Bar {
|
||||
fun bar() {}
|
||||
}
|
||||
|
||||
class Test {
|
||||
fun test() {
|
||||
Bar().run {
|
||||
foo(this::bar)
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-5
@@ -5210,16 +5210,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/extension.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extensionOuterScope.kt")
|
||||
public void testExtensionOuterScope() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/extensionOuterScope.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extensionProperty.kt")
|
||||
public void testExtensionProperty() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/extensionProperty.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extentionOuterScope.kt")
|
||||
public void testExtentionOuterScope() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/extentionOuterScope.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fqNameForReceiver.kt")
|
||||
public void testFqNameForReceiver() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/fqNameForReceiver.kt");
|
||||
@@ -5285,6 +5285,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/memberInLambdaArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("memberInScopeFunction.kt")
|
||||
public void testMemberInScopeFunction() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/memberInScopeFunction.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("memberInScopeFunction2.kt")
|
||||
public void testMemberInScopeFunction2() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/memberInScopeFunction2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("memberOuterScope.kt")
|
||||
public void testMemberOuterScope() throws Exception {
|
||||
runTest("idea/testData/intentions/convertLambdaToReference/memberOuterScope.kt");
|
||||
|
||||
Reference in New Issue
Block a user