Implement intention to remove labeled return from last lambda expression
So #KT-20439 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
35ce30aedc
commit
769e28519e
+5
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
<spot>return@find</spot> true
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This intention removes labeled return from last expression in a lambda.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1557,6 +1557,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.RemoveLabeledReturnInLambdaIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||||
displayName="Object literal can be converted to lambda"
|
displayName="Object literal can be converted to lambda"
|
||||||
groupPath="Kotlin"
|
groupPath="Kotlin"
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
|
||||||
|
class RemoveLabeledReturnInLambdaIntention : SelfTargetingIntention<KtReturnExpression>(
|
||||||
|
KtReturnExpression::class.java,
|
||||||
|
"Remove labeled return from last expression in a lambda"
|
||||||
|
), LowPriorityAction {
|
||||||
|
override fun isApplicableTo(element: KtReturnExpression, caretOffset: Int): Boolean {
|
||||||
|
val labelName = element.getLabelName() ?: return false
|
||||||
|
val block = element.getStrictParentOfType<KtBlockExpression>() ?: return false
|
||||||
|
if (block.statements.lastOrNull() != element) return false
|
||||||
|
val callExpression = block.getStrictParentOfType<KtCallExpression>() ?: return false
|
||||||
|
val lambdaArgument = callExpression.lambdaArguments.firstOrNull {
|
||||||
|
val argumentExpression = it.getArgumentExpression()
|
||||||
|
val lambda = when (argumentExpression) {
|
||||||
|
is KtLambdaExpression -> argumentExpression
|
||||||
|
is KtLabeledExpression -> argumentExpression.baseExpression as? KtLambdaExpression
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
lambda?.bodyExpression === block
|
||||||
|
} ?: return false
|
||||||
|
val callName = (lambdaArgument.getArgumentExpression() as? KtLabeledExpression)?.getLabelName()
|
||||||
|
?: callExpression.getCallNameExpression()?.text ?: return false
|
||||||
|
if (labelName != callName) return false
|
||||||
|
text = "Remove return@$labelName"
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtReturnExpression, editor: Editor?) {
|
||||||
|
val returnedExpression = element.returnedExpression
|
||||||
|
if (returnedExpression == null) {
|
||||||
|
element.delete()
|
||||||
|
} else {
|
||||||
|
element.replace(returnedExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.RemoveLabeledReturnInLambdaIntention
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@label"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find label@{
|
||||||
|
return@label <caret>true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@label"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find label@{
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@find"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
return@find <caret>true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@find"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
fun foo(): Boolean {
|
||||||
|
return@foo <caret>true
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
fun foo(): Boolean {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
return <caret>true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
<caret>1
|
||||||
|
return@find true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: FALSE
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).forEach {
|
||||||
|
listOf(1,2,3).find {
|
||||||
|
return@forEach<caret>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@forEach"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).forEach {
|
||||||
|
<caret>return@forEach
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Remove return@forEach"
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
listOf(1,2,3).forEach {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13216,6 +13216,57 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/removeLabeledReturnInLambda")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class RemoveLabeledReturnInLambda extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInRemoveLabeledReturnInLambda() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeLabeledReturnInLambda"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("labeledLambda.kt")
|
||||||
|
public void testLabeledLambda() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/labeledLambda.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("normal.kt")
|
||||||
|
public void testNormal() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/normal.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notInsideLabeled.kt")
|
||||||
|
public void testNotInsideLabeled() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/notInsideLabeled.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notLabeled.kt")
|
||||||
|
public void testNotLabeled() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/notLabeled.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notLastLine.kt")
|
||||||
|
public void testNotLastLine() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/notLastLine.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("outerLambda.kt")
|
||||||
|
public void testOuterLambda() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/outerLambda.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("unit.kt")
|
||||||
|
public void testUnit() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeLabeledReturnInLambda/unit.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods")
|
@TestMetadata("idea/testData/intentions/removeRedundantCallsOfConversionMethods")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user