From 46ab338ea6a54e614a44ff219cad0326b546f722 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 3 Apr 2020 17:37:38 +0900 Subject: [PATCH] "Convert anonymous function to lambda expression" intention: add necessary lambda type parameter #KT-37748 Fixed --- .../AnonymousFunctionToLambdaIntention.kt | 19 +++++++++++++++++++ .../typeParameter.kt | 9 +++++++++ .../typeParameter.kt.after | 7 +++++++ .../typeParameter2.kt | 9 +++++++++ .../typeParameter2.kt.after | 7 +++++++ .../typeParameter3.kt | 9 +++++++++ .../typeParameter3.kt.after | 7 +++++++ .../intentions/IntentionTestGenerated.java | 15 +++++++++++++++ .../toKotlinClasses/LibraryFunctions.kt | 2 +- 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt.after create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt.after create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt create mode 100644 idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AnonymousFunctionToLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AnonymousFunctionToLambdaIntention.kt index 71d5c04929d..a52de9af71d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AnonymousFunctionToLambdaIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AnonymousFunctionToLambdaIntention.kt @@ -9,7 +9,9 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.search.LocalSearchScope import com.intellij.psi.search.searches.ReferencesSearch +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall import org.jetbrains.kotlin.idea.core.getLastLambdaExpression import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParenthesesIfPossible import org.jetbrains.kotlin.idea.core.replaced @@ -17,8 +19,10 @@ import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.contentRange import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny +import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention( @@ -40,6 +44,14 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention() ?: return + val callElement = argument.getStrictParentOfType() ?: return + val functionParameterTypes = if (callElement.typeArgumentList == null) { + callElement.resolveToCall()?.getParameterForArgument(argument)?.original?.type?.arguments.orEmpty() + } else { + emptyList() + } + val commentSaver = CommentSaver(element) val returnSaver = ReturnSaver(element) @@ -58,6 +70,13 @@ class AnonymousFunctionToLambdaIntention : SelfTargetingRangeIntention") diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt new file mode 100644 index 00000000000..a36cc81adb9 --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt @@ -0,0 +1,9 @@ +fun foo(p: String) {} + +fun bar(fn: (T) -> Unit) {} + +fun test() { + bar(fun(x: String) { + foo(x) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt.after b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt.after new file mode 100644 index 00000000000..7216513b269 --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt.after @@ -0,0 +1,7 @@ +fun foo(p: String) {} + +fun bar(fn: (T) -> Unit) {} + +fun test() { + bar { x: String -> foo(x) } +} \ No newline at end of file diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt new file mode 100644 index 00000000000..dd639a10fae --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt @@ -0,0 +1,9 @@ +fun foo(p: String, q: Int, r: Long) {} + +fun bar(fn: (T, Int, U) -> Unit) {} + +fun test() { + bar(fun(x: String, y: Int, z: Long) { + foo(x, y, z) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt.after b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt.after new file mode 100644 index 00000000000..d66270db404 --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt.after @@ -0,0 +1,7 @@ +fun foo(p: String, q: Int, r: Long) {} + +fun bar(fn: (T, Int, U) -> Unit) {} + +fun test() { + bar { x: String, y, z: Long -> foo(x, y, z) } +} \ No newline at end of file diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt new file mode 100644 index 00000000000..0fe258e2c9d --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt @@ -0,0 +1,9 @@ +fun foo(p: String) {} + +fun bar(fn: (T) -> Unit) {} + +fun test() { + bar(fun(x: String) { + foo(x) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt.after b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt.after new file mode 100644 index 00000000000..97461eb5ac8 --- /dev/null +++ b/idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt.after @@ -0,0 +1,7 @@ +fun foo(p: String) {} + +fun bar(fn: (T) -> Unit) {} + +fun test() { + bar { x -> foo(x) } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index c0d2531a5a5..e84426b1f15 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2100,6 +2100,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { public void testSimple() throws Exception { runTest("idea/testData/intentions/anonymousFunctionToLambda/simple.kt"); } + + @TestMetadata("typeParameter.kt") + public void testTypeParameter() throws Exception { + runTest("idea/testData/intentions/anonymousFunctionToLambda/typeParameter.kt"); + } + + @TestMetadata("typeParameter2.kt") + public void testTypeParameter2() throws Exception { + runTest("idea/testData/intentions/anonymousFunctionToLambda/typeParameter2.kt"); + } + + @TestMetadata("typeParameter3.kt") + public void testTypeParameter3() throws Exception { + runTest("idea/testData/intentions/anonymousFunctionToLambda/typeParameter3.kt"); + } } @TestMetadata("idea/testData/intentions/branched") diff --git a/j2k/testData/fileOrElement/toKotlinClasses/LibraryFunctions.kt b/j2k/testData/fileOrElement/toKotlinClasses/LibraryFunctions.kt index 3bcf25a7483..ec1329c3065 100644 --- a/j2k/testData/fileOrElement/toKotlinClasses/LibraryFunctions.kt +++ b/j2k/testData/fileOrElement/toKotlinClasses/LibraryFunctions.kt @@ -12,7 +12,7 @@ class TestJava { @JvmStatic fun main(args: Array) { val x = ArrayList() - x.filter { o -> o == "a" } + x.filter { o: String -> o == "a" } val lazy = lazy(LazyThreadSafetyMode.NONE) { "aaa" } } }