"Convert lambda to reference": do not remove required backticks

#KT-15700 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-09-09 15:46:06 +09:00
committed by Dmitry Gridin
parent 44edd94fea
commit 0e1e1e350f
6 changed files with 35 additions and 6 deletions
@@ -32,6 +32,7 @@ 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.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext.FUNCTION
import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
@@ -229,8 +230,7 @@ open class ConvertLambdaToReferenceIntention(text: String) :
companion object {
private fun buildReferenceText(lambdaExpression: KtLambdaExpression, shortTypes: Boolean): String? {
val singleStatement = lambdaExpression.singleStatementOrNull()
return when (singleStatement) {
return when (val singleStatement = lambdaExpression.singleStatementOrNull()) {
is KtCallExpression -> {
val calleeReferenceExpression = singleStatement.calleeExpression as? KtNameReferenceExpression ?: return null
val resolvedCall = calleeReferenceExpression.resolveToCall() ?: return null
@@ -244,10 +244,9 @@ open class ConvertLambdaToReferenceIntention(text: String) :
"$receiverText::${singleStatement.getCallReferencedName()}"
}
is KtDotQualifiedExpression -> {
val selector = singleStatement.selectorExpression
val selectorReferenceName = when (selector) {
val selectorReferenceName = when (val selector = singleStatement.selectorExpression) {
is KtCallExpression -> selector.getCallReferencedName() ?: return null
is KtNameReferenceExpression -> selector.getReferencedName()
is KtNameReferenceExpression -> selector.getSafeReferencedName()
else -> return null
}
val receiver = singleStatement.receiverExpression
@@ -278,7 +277,9 @@ open class ConvertLambdaToReferenceIntention(text: String) :
}
}
private fun KtCallExpression.getCallReferencedName() = (calleeExpression as? KtNameReferenceExpression)?.getReferencedName()
private fun KtCallExpression.getCallReferencedName() = (calleeExpression as? KtNameReferenceExpression)?.getSafeReferencedName()
private fun KtNameReferenceExpression.getSafeReferencedName() = getReferencedNameAsName().render()
private fun KtLambdaExpression.singleStatementOrNull() = bodyExpression?.statements?.singleOrNull()
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun `super`(x: Int): Int = TODO()
fun main(args: Array<String>) {
listOf(1).map { <caret>`super`(it) }
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun `super`(x: Int): Int = TODO()
fun main(args: Array<String>) {
listOf(1).map(::`super`)
}
@@ -0,0 +1,3 @@
class Person(val `super`: String)
val reader = { p: Person -> p.`super`<caret> }
@@ -0,0 +1,3 @@
class Person(val `super`: String)
val reader = Person::`super`
@@ -4700,6 +4700,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertLambdaToReference/argumentWithReceiver.kt");
}
@TestMetadata("backtickEscaped.kt")
public void testBacktickEscaped() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/backtickEscaped.kt");
}
@TestMetadata("backtickEscaped2.kt")
public void testBacktickEscaped2() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/backtickEscaped2.kt");
}
@TestMetadata("classReference.kt")
public void testClassReference() throws Exception {
runTest("idea/testData/intentions/convertLambdaToReference/classReference.kt");