Insert brackets in "Convert reference to lambda" if necessary
So #KT-16394 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0348561cd2
commit
ec64a7e422
@@ -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()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1)
|
||||
<caret>this::bar
|
||||
else
|
||||
this::bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1)
|
||||
({ this.bar() })
|
||||
else
|
||||
this::bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1)
|
||||
this::bar
|
||||
else
|
||||
<caret>this::bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1)
|
||||
this::bar
|
||||
else
|
||||
({ this.bar() })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1) {
|
||||
this::bar
|
||||
} else {
|
||||
<caret>this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = if (x == 1) {
|
||||
this::bar
|
||||
} else {
|
||||
{ this.bar() }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <caret>this::bar
|
||||
}
|
||||
}
|
||||
@@ -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) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> <caret>this::bar
|
||||
else -> this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> ({ this.bar() })
|
||||
else -> this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> this::bar
|
||||
else -> <caret>this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> this::bar
|
||||
else -> ({ this.bar() })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> {
|
||||
<caret>this::bar
|
||||
}
|
||||
else -> {
|
||||
this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
class Test {
|
||||
fun bar() = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: () -> Int = when (x) {
|
||||
1 -> {
|
||||
{ this.bar() }
|
||||
}
|
||||
else -> {
|
||||
this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class Test {
|
||||
fun bar(a: String) = 1
|
||||
|
||||
fun test(x: Int) {
|
||||
val foo: (a: String) -> Int = when (x) {
|
||||
1 -> <caret>this::bar
|
||||
else -> this::bar
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user