diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt index d580bf72710..af349948ace 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt @@ -81,9 +81,6 @@ class KtPsiFactory(private val project: Project) { fun createThisExpression() = (createExpression("this.x") as KtQualifiedExpression).receiverExpression as KtThisExpression - fun createClassLiteral(className: String): KtClassLiteralExpression = - createExpression("$className::class") as KtClassLiteralExpression - fun createCallArguments(text: String): KtValueArgumentList { val property = createProperty("val x = foo $text") return (property.initializer as KtCallExpression).valueArgumentList!! @@ -328,9 +325,13 @@ class KtPsiFactory(private val project: Project) { fun createTypeParameter(text: String) = createTypeParameterList("<$text>").parameters.first()!! - fun createFunctionLiteralParameterList(text: String): KtParameterList { - return (createExpression("{ $text -> 0}") as KtLambdaExpression).functionLiteral.valueParameterList!! - } + fun createFunctionLiteralParameterList(text: String) = + createLambdaExpression(text, "0").functionLiteral.valueParameterList!! + + fun createLambdaExpression(parameters: String, body: String): KtLambdaExpression = + (if (parameters.isNotEmpty()) createExpression("{ $parameters -> $body }") + else createExpression("{ $body }")) as KtLambdaExpression + fun createEnumEntry(text: String): KtEnumEntry { return createDeclaration("enum class E {$text}").declarations[0] as KtEnumEntry @@ -387,11 +388,6 @@ class KtPsiFactory(private val project: Project) { return file.importDirectives.first() } - fun createImportDirectiveWithImportList(importPath: ImportPath): KtImportList { - val importDirective = createImportDirective(importPath) - return importDirective.parent as KtImportList - } - fun createPrimaryConstructor(): KtPrimaryConstructor { return createClass("class A()").getPrimaryConstructor()!! } @@ -406,10 +402,6 @@ class KtPsiFactory(private val project: Project) { fun createLabeledExpression(labelName: String): KtLabeledExpression = createExpression("$labelName@ 1") as KtLabeledExpression - fun createFieldIdentifier(fieldName: String): PsiElement { - return (createExpression("$" + fieldName) as KtNameReferenceExpression).getReferencedNameElement() - } - fun createTypeCodeFragment(text: String, context: PsiElement?): KtTypeCodeFragment { return KtTypeCodeFragment(project, "fragment.kt", text, context) } diff --git a/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/after.kt.template b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/after.kt.template new file mode 100644 index 00000000000..db1c5a834b5 --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/after.kt.template @@ -0,0 +1,3 @@ +class Person(val name: String) + +val persons = listOf("Jack", "Tom").map { Person(it) } diff --git a/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/before.kt.template b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/before.kt.template new file mode 100644 index 00000000000..758471000da --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/before.kt.template @@ -0,0 +1,3 @@ +class Person(val name: String) + +val persons = listOf("Jack", "Tom").map(::Person) diff --git a/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/description.html b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/description.html new file mode 100644 index 00000000000..5fdeff7637b --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertReferenceToLambdaIntention/description.html @@ -0,0 +1,5 @@ + + +This intention converts a callable function reference to the lambda expression + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 1d8cbaa39a9..2b338905bcc 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1338,6 +1338,11 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.ConvertReferenceToLambdaIntention + Kotlin + + org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt new file mode 100644 index 00000000000..5be9fe2fc02 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt @@ -0,0 +1,110 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.intentions + +import com.intellij.openapi.editor.Editor +import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.KotlinNameSuggester +import org.jetbrains.kotlin.idea.core.ShortenReferences +import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.BindingContext.DOUBLE_COLON_LHS +import org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.types.expressions.DoubleColonLHS +import org.jetbrains.kotlin.utils.singletonOrEmptyList + +class ConvertReferenceToLambdaIntention : SelfTargetingOffsetIndependentIntention( + KtCallableReferenceExpression::class.java, "Convert reference to lambda" +) { + val SOURCE_RENDERER = IdeDescriptorRenderers.SOURCE_CODE + + override fun applyTo(element: KtCallableReferenceExpression, editor: Editor?) { + val context = element.analyze(BodyResolveMode.PARTIAL) + val reference = element.callableReference + val targetDescriptor = context[REFERENCE_TARGET, reference] as? CallableMemberDescriptor ?: return + val parameterNamesAndTypes = targetDescriptor.valueParameters.map { it.name.asString() to it.type } + val receiverExpression = element.receiverExpression + val receiverType = receiverExpression?.let { + (context[DOUBLE_COLON_LHS, it] as? DoubleColonLHS.Type)?.type ?: return + } + val receiverNameAndType = receiverType?.let { KotlinNameSuggester.suggestNamesByType(it, validator = { + name -> name !in parameterNamesAndTypes.map { it.first } + }, defaultName = "receiver").first() to it } + + val referenceParent = element.parent + val insideCall = referenceParent is KtValueArgument + + val factory = KtPsiFactory(element) + val targetName = reference.text + val lambdaParameterNamesAndTypes = receiverNameAndType.singletonOrEmptyList() + parameterNamesAndTypes + val receiverPrefix = receiverNameAndType?.let { it.first + "." } ?: "" + val lambdaExpression = if (insideCall && lambdaParameterNamesAndTypes.size == 1) { + factory.createLambdaExpression( + parameters = "", + body = when { + receiverNameAndType != null -> + if (targetDescriptor is PropertyDescriptor) "it.$targetName" + else "it.$targetName()" + else -> + "$targetName(it)" + } + ) + } + else { + factory.createLambdaExpression( + parameters = lambdaParameterNamesAndTypes.joinToString(separator = ", ") { + if (insideCall) it.first + else it.first + ": " + SOURCE_RENDERER.renderType(it.second) + }, + body = if (targetDescriptor is PropertyDescriptor) { + "$receiverPrefix$targetName" + } + else { + parameterNamesAndTypes.joinToString( + prefix = "$receiverPrefix$targetName(", + separator = ", ", + postfix = ")" + ) { it.first } + } + ) + } + val lambdaResult = element.replace(lambdaExpression) as KtLambdaExpression + ShortenReferences.DEFAULT.process(lambdaResult) + + if (insideCall) { + val call = referenceParent?.parent?.parent as? KtCallExpression ?: return + val moveOutOfParenthesis = MoveLambdaOutsideParenthesesIntention() + if (moveOutOfParenthesis.isApplicableTo(call, referenceParent.startOffset)) { + moveOutOfParenthesis.applyTo(call, editor) + } + } + } + + override fun isApplicableTo(element: KtCallableReferenceExpression): Boolean { + val context = element.analyze(BodyResolveMode.PARTIAL) + val receiverExpression = element.receiverExpression + if (receiverExpression != null) { + val lhs = context[DOUBLE_COLON_LHS, receiverExpression] + if (lhs is DoubleColonLHS.Expression) return false + } + return true + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/.intention b/idea/testData/intentions/convertReferenceToLambda/.intention new file mode 100644 index 00000000000..683119c0134 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.ConvertReferenceToLambdaIntention diff --git a/idea/testData/intentions/convertReferenceToLambda/boundReference.kt b/idea/testData/intentions/convertReferenceToLambda/boundReference.kt new file mode 100644 index 00000000000..e3aa0cdcbcd --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/boundReference.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false + +val x = 1 +// Not supported yet +val y = x::hashCode \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/constructor.kt b/idea/testData/intentions/convertReferenceToLambda/constructor.kt new file mode 100644 index 00000000000..7df51dee160 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/constructor.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +class Person(val name: String) + +val x = listOf("Jack", "Tom").map(::Person) \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/constructor.kt.after b/idea/testData/intentions/convertReferenceToLambda/constructor.kt.after new file mode 100644 index 00000000000..33e781fffe4 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/constructor.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +class Person(val name: String) + +val x = listOf("Jack", "Tom").map { Person(it) } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/conversion.kt b/idea/testData/intentions/convertReferenceToLambda/conversion.kt new file mode 100644 index 00000000000..2b1ce667e47 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/conversion.kt @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val x = listOf("123", "4567").map(String::toInt) \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/conversion.kt.after b/idea/testData/intentions/convertReferenceToLambda/conversion.kt.after new file mode 100644 index 00000000000..76f6fbd0397 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/conversion.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val x = listOf("123", "4567").map { it.toInt() } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt b/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt new file mode 100644 index 00000000000..3a36cb0ebd3 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt @@ -0,0 +1,3 @@ +val Any.name: String get() = toString() + +val converted = Any::name \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt.after b/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt.after new file mode 100644 index 00000000000..d1ff11cf6bb --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt.after @@ -0,0 +1,3 @@ +val Any.name: String get() = toString() + +val converted = { any: Any -> any.name } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt b/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt new file mode 100644 index 00000000000..4561098fe81 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt @@ -0,0 +1,13 @@ +class Foo { + class Bar { + fun foo() {} + } +} + +class Bar { + fun foo() {} +} + +fun use() { + val f: (Foo.Bar) -> Unit = Foo.Bar::foo +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt.after b/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt.after new file mode 100644 index 00000000000..12ae2a4fe31 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt.after @@ -0,0 +1,13 @@ +class Foo { + class Bar { + fun foo() {} + } +} + +class Bar { + fun foo() {} +} + +fun use() { + val f: (Foo.Bar) -> Unit = { bar: Foo.Bar -> bar.foo() } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/inner.kt b/idea/testData/intentions/convertReferenceToLambda/inner.kt new file mode 100644 index 00000000000..de71887192f --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/inner.kt @@ -0,0 +1,5 @@ +class Owner { + inner class Inner + + val x = Owner::Inner +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/inner.kt.after b/idea/testData/intentions/convertReferenceToLambda/inner.kt.after new file mode 100644 index 00000000000..39c143f6d45 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/inner.kt.after @@ -0,0 +1,5 @@ +class Owner { + inner class Inner + + val x = { owner: Owner -> owner.Inner() } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/length.kt b/idea/testData/intentions/convertReferenceToLambda/length.kt new file mode 100644 index 00000000000..8762fd30490 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/length.kt @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val x = listOf("123", "4567").map(String::length) \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/length.kt.after b/idea/testData/intentions/convertReferenceToLambda/length.kt.after new file mode 100644 index 00000000000..6b97237a519 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/length.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME + +val x = listOf("123", "4567").map { it.length } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/name.kt b/idea/testData/intentions/convertReferenceToLambda/name.kt new file mode 100644 index 00000000000..ea95322f0df --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/name.kt @@ -0,0 +1,3 @@ +class Person(val name: String) + +val reader = Person::name \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/name.kt.after b/idea/testData/intentions/convertReferenceToLambda/name.kt.after new file mode 100644 index 00000000000..6bac8043868 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/name.kt.after @@ -0,0 +1,3 @@ +class Person(val name: String) + +val reader = { person: Person -> person.name } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/nullable.kt b/idea/testData/intentions/convertReferenceToLambda/nullable.kt new file mode 100644 index 00000000000..dc63b33d633 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/nullable.kt @@ -0,0 +1,3 @@ +fun Int?.foo() = this?.hashCode() ?: 0 + +val x = Int?::foo \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/nullable.kt.after b/idea/testData/intentions/convertReferenceToLambda/nullable.kt.after new file mode 100644 index 00000000000..3bd59b8c894 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/nullable.kt.after @@ -0,0 +1,3 @@ +fun Int?.foo() = this?.hashCode() ?: 0 + +val x = { i: Int? -> i.foo() } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt b/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt new file mode 100644 index 00000000000..ccc6c56e121 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt @@ -0,0 +1,3 @@ +fun Int.foo(x: Int) = this - x + +val x = Int::foo \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt.after b/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt.after new file mode 100644 index 00000000000..a54777e6a4c --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt.after @@ -0,0 +1,3 @@ +fun Int.foo(x: Int) = this - x + +val x = { i: Int, x: Int -> i.foo(x) } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/simple.kt b/idea/testData/intentions/convertReferenceToLambda/simple.kt new file mode 100644 index 00000000000..5df8661bde8 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/simple.kt @@ -0,0 +1,3 @@ +fun foo(y: Int) = y + +val x = ::foo \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/simple.kt.after b/idea/testData/intentions/convertReferenceToLambda/simple.kt.after new file mode 100644 index 00000000000..9adc7eeb5cc --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/simple.kt.after @@ -0,0 +1,3 @@ +fun foo(y: Int) = y + +val x = { y: Int -> foo(y) } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt b/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt new file mode 100644 index 00000000000..5dc8585a7be --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt @@ -0,0 +1,3 @@ +fun foo(x: Int, y: Int, z: Int) = x - y / z + +val x = ::foo \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt.after b/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt.after new file mode 100644 index 00000000000..4b6f8c7deae --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/threeParameters.kt.after @@ -0,0 +1,3 @@ +fun foo(x: Int, y: Int, z: Int) = x - y / z + +val x = { x: Int, y: Int, z: Int -> foo(x, y, z) } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/toString.kt b/idea/testData/intentions/convertReferenceToLambda/toString.kt new file mode 100644 index 00000000000..d16b44cf333 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/toString.kt @@ -0,0 +1 @@ +val x = Int::toString \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/toString.kt.after b/idea/testData/intentions/convertReferenceToLambda/toString.kt.after new file mode 100644 index 00000000000..7785a4b2f8e --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/toString.kt.after @@ -0,0 +1 @@ +val x = { i: Int -> i.toString() } \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/unwrap.kt b/idea/testData/intentions/convertReferenceToLambda/unwrap.kt new file mode 100644 index 00000000000..dcacc36f81d --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/unwrap.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +class Wrapper(private val x: T) { + fun unwrap() = x +} + +val unwrapped = listOf(Wrapper(1), Wrapper("B")).map(Wrapper::unwrap) \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/unwrap.kt.after b/idea/testData/intentions/convertReferenceToLambda/unwrap.kt.after new file mode 100644 index 00000000000..d1f6a3008e2 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/unwrap.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME + +class Wrapper(private val x: T) { + fun unwrap() = x +} + +val unwrapped = listOf(Wrapper(1), Wrapper("B")).map { it.unwrap() } \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/callableReferences/noFunctionalType.kt b/idea/testData/quickfix/createFromUsage/createFunction/callableReferences/noFunctionalType.kt index 72b2e951ace..ebba25dd9b3 100644 --- a/idea/testData/quickfix/createFromUsage/createFunction/callableReferences/noFunctionalType.kt +++ b/idea/testData/quickfix/createFromUsage/createFunction/callableReferences/noFunctionalType.kt @@ -1,6 +1,7 @@ // "Create function 'foo'" "false" // ACTION: Convert to expression body // ACTION: Rename reference +// ACTION: Convert reference to lambda // ERROR: Unresolved reference: foo fun bar(n: Int) = "$n" diff --git a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/callableRef.kt b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/callableRef.kt index 7e1bb019349..77eab22f0df 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/callableRef.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/callableRef.kt @@ -1,6 +1,7 @@ // "Create local variable 'foo'" "false" // ACTION: Rename reference // ACTION: Create function 'foo' +// ACTION: Convert reference to lambda // ERROR: Unresolved reference: foo fun test(f: (Int) -> Int) {} diff --git a/idea/testData/quickfix/createFromUsage/createVariable/parameter/callableRef.kt b/idea/testData/quickfix/createFromUsage/createVariable/parameter/callableRef.kt index 7b2174d7a02..45fefee997c 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/parameter/callableRef.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/parameter/callableRef.kt @@ -1,6 +1,7 @@ // "Create parameter 'foo'" "false" // ACTION: Rename reference // ACTION: Create function 'foo' +// ACTION: Convert reference to lambda // ERROR: Unresolved reference: foo fun test(f: (Int) -> Int) {} diff --git a/idea/testData/quickfix/createFromUsage/createVariable/property/callableRef.kt b/idea/testData/quickfix/createFromUsage/createVariable/property/callableRef.kt index 5a7ddad8e02..d2af35bcacf 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/property/callableRef.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/property/callableRef.kt @@ -1,6 +1,7 @@ // "Create property 'foo'" "false" // ACTION: Rename reference // ACTION: Create function 'foo' +// ACTION: Convert reference to lambda // ERROR: Unresolved reference: foo fun test(f: (Int) -> Int) {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index f6c064e8cb6..192f421dc57 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -4878,6 +4878,99 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/convertReferenceToLambda") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ConvertReferenceToLambda extends AbstractIntentionTest { + public void testAllFilesPresentInConvertReferenceToLambda() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertReferenceToLambda"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("boundReference.kt") + public void testBoundReference() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/boundReference.kt"); + doTest(fileName); + } + + @TestMetadata("constructor.kt") + public void testConstructor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/constructor.kt"); + doTest(fileName); + } + + @TestMetadata("conversion.kt") + public void testConversion() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/conversion.kt"); + doTest(fileName); + } + + @TestMetadata("extensionProperty.kt") + public void testExtensionProperty() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/extensionProperty.kt"); + doTest(fileName); + } + + @TestMetadata("fqNameForReceiver.kt") + public void testFqNameForReceiver() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/fqNameForReceiver.kt"); + doTest(fileName); + } + + @TestMetadata("inner.kt") + public void testInner() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/inner.kt"); + doTest(fileName); + } + + @TestMetadata("length.kt") + public void testLength() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/length.kt"); + doTest(fileName); + } + + @TestMetadata("name.kt") + public void testName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/name.kt"); + doTest(fileName); + } + + @TestMetadata("nullable.kt") + public void testNullable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/nullable.kt"); + doTest(fileName); + } + + @TestMetadata("receiverParameter.kt") + public void testReceiverParameter() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/receiverParameter.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/simple.kt"); + doTest(fileName); + } + + @TestMetadata("threeParameters.kt") + public void testThreeParameters() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/threeParameters.kt"); + doTest(fileName); + } + + @TestMetadata("toString.kt") + public void testToString() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/toString.kt"); + doTest(fileName); + } + + @TestMetadata("unwrap.kt") + public void testUnwrap() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/unwrap.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/convertSealedClassToEnum") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)