diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt index b64106279c0..d6b2b4c0434 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertReferenceToLambdaIntention.kt @@ -17,12 +17,14 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor +import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.builtins.isExtensionFunctionType 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.core.replaced import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.psi.* @@ -108,8 +110,14 @@ class ConvertReferenceToLambdaIntention : SelfTargetingOffsetIndependentIntentio } ) } - val lambdaResult = element.replace(lambdaExpression) as KtLambdaExpression - ShortenReferences.DEFAULT.process(lambdaResult) + + val needParentheses = lambdaParameterNamesAndTypes.isEmpty() && when (element.parent.node.elementType) { + KtNodeTypes.WHEN_ENTRY, KtNodeTypes.THEN, KtNodeTypes.ELSE -> true + else -> false + } + val wrappedExpression = + if (needParentheses) factory.createExpressionByPattern("($0)", lambdaExpression) else lambdaExpression + ShortenReferences.DEFAULT.process(element.replaced(wrappedExpression)) if (valueArgumentParent != null && callGrandParent != null) { val moveOutOfParenthesis = MoveLambdaOutsideParenthesesIntention() diff --git a/idea/testData/intentions/convertReferenceToLambda/if1.kt b/idea/testData/intentions/convertReferenceToLambda/if1.kt new file mode 100644 index 00000000000..1ebfb03c327 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if1.kt @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) + this::bar + else + this::bar + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if1.kt.after b/idea/testData/intentions/convertReferenceToLambda/if1.kt.after new file mode 100644 index 00000000000..476cba7c212 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if1.kt.after @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) + ({ this.bar() }) + else + this::bar + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if2.kt b/idea/testData/intentions/convertReferenceToLambda/if2.kt new file mode 100644 index 00000000000..50ea90c1f4d --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if2.kt @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) + this::bar + else + this::bar + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if2.kt.after b/idea/testData/intentions/convertReferenceToLambda/if2.kt.after new file mode 100644 index 00000000000..d3f04b752c8 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if2.kt.after @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) + this::bar + else + ({ this.bar() }) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if3.kt b/idea/testData/intentions/convertReferenceToLambda/if3.kt new file mode 100644 index 00000000000..97d2e712196 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if3.kt @@ -0,0 +1,11 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) { + this::bar + } else { + this::bar + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if3.kt.after b/idea/testData/intentions/convertReferenceToLambda/if3.kt.after new file mode 100644 index 00000000000..0ff04fff880 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if3.kt.after @@ -0,0 +1,11 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = if (x == 1) { + this::bar + } else { + { this.bar() } + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if4.kt b/idea/testData/intentions/convertReferenceToLambda/if4.kt new file mode 100644 index 00000000000..e6cbc23f749 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if4.kt @@ -0,0 +1,7 @@ +class Test { + fun bar(a: String) = 1 + + fun test(x: Int) { + val foo: (a: String) -> Int = if (x == 1) this::bar else this::bar + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/if4.kt.after b/idea/testData/intentions/convertReferenceToLambda/if4.kt.after new file mode 100644 index 00000000000..d6bad6fd334 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/if4.kt.after @@ -0,0 +1,7 @@ +class Test { + fun bar(a: String) = 1 + + fun test(x: Int) { + val foo: (a: String) -> Int = if (x == 1) this::bar else { a: String -> this.bar(a) } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when1.kt b/idea/testData/intentions/convertReferenceToLambda/when1.kt new file mode 100644 index 00000000000..7105fbd2b94 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when1.kt @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> this::bar + else -> this::bar + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when1.kt.after b/idea/testData/intentions/convertReferenceToLambda/when1.kt.after new file mode 100644 index 00000000000..9cadec37492 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when1.kt.after @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> ({ this.bar() }) + else -> this::bar + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when2.kt b/idea/testData/intentions/convertReferenceToLambda/when2.kt new file mode 100644 index 00000000000..b20271c5f9e --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when2.kt @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> this::bar + else -> this::bar + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when2.kt.after b/idea/testData/intentions/convertReferenceToLambda/when2.kt.after new file mode 100644 index 00000000000..39806fa5b19 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when2.kt.after @@ -0,0 +1,10 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> this::bar + else -> ({ this.bar() }) + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when3.kt b/idea/testData/intentions/convertReferenceToLambda/when3.kt new file mode 100644 index 00000000000..1beb36ac104 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when3.kt @@ -0,0 +1,14 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> { + this::bar + } + else -> { + this::bar + } + } + } +} diff --git a/idea/testData/intentions/convertReferenceToLambda/when3.kt.after b/idea/testData/intentions/convertReferenceToLambda/when3.kt.after new file mode 100644 index 00000000000..02f3b630133 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when3.kt.after @@ -0,0 +1,14 @@ +class Test { + fun bar() = 1 + + fun test(x: Int) { + val foo: () -> Int = when (x) { + 1 -> { + { this.bar() } + } + else -> { + this::bar + } + } + } +} diff --git a/idea/testData/intentions/convertReferenceToLambda/when4.kt b/idea/testData/intentions/convertReferenceToLambda/when4.kt new file mode 100644 index 00000000000..ff21bc95dc0 --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when4.kt @@ -0,0 +1,10 @@ +class Test { + fun bar(a: String) = 1 + + fun test(x: Int) { + val foo: (a: String) -> Int = when (x) { + 1 -> this::bar + else -> this::bar + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertReferenceToLambda/when4.kt.after b/idea/testData/intentions/convertReferenceToLambda/when4.kt.after new file mode 100644 index 00000000000..0403f33b5cf --- /dev/null +++ b/idea/testData/intentions/convertReferenceToLambda/when4.kt.after @@ -0,0 +1,10 @@ +class Test { + fun bar(a: String) = 1 + + fun test(x: Int) { + val foo: (a: String) -> Int = when (x) { + 1 -> { a: String -> this.bar(a) } + else -> this::bar + } + } +} \ 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 17ef085de0c..75af0ee0077 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -6011,6 +6011,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("if1.kt") + public void testIf1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/if1.kt"); + doTest(fileName); + } + + @TestMetadata("if2.kt") + public void testIf2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/if2.kt"); + doTest(fileName); + } + + @TestMetadata("if3.kt") + public void testIf3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/if3.kt"); + doTest(fileName); + } + + @TestMetadata("if4.kt") + public void testIf4() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/if4.kt"); + doTest(fileName); + } + @TestMetadata("inner.kt") public void testInner() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/inner.kt"); @@ -6100,6 +6124,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/unwrap.kt"); doTest(fileName); } + + @TestMetadata("when1.kt") + public void testWhen1() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/when1.kt"); + doTest(fileName); + } + + @TestMetadata("when2.kt") + public void testWhen2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/when2.kt"); + doTest(fileName); + } + + @TestMetadata("when3.kt") + public void testWhen3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/when3.kt"); + doTest(fileName); + } + + @TestMetadata("when4.kt") + public void testWhen4() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertReferenceToLambda/when4.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/intentions/convertSealedClassToEnum")