Skip to content

Commit 769e285

Browse files
shirajimglukhikh
authored andcommitted
Implement intention to remove labeled return from last lambda expression
So #KT-20439 Fixed
1 parent 35ce30a commit 769e285

File tree

17 files changed

+201
-0
lines changed

17 files changed

+201
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fun foo() {
2+
listOf(1,2,3).find {
3+
true
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fun foo() {
2+
listOf(1,2,3).find {
3+
<spot>return@find</spot> true
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
This intention removes labeled return from last expression in a lambda.
4+
</body>
5+
</html>

idea/src/META-INF/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,11 @@
15571557
<category>Kotlin</category>
15581558
</intentionAction>
15591559

1560+
<intentionAction>
1561+
<className>org.jetbrains.kotlin.idea.intentions.RemoveLabeledReturnInLambdaIntention</className>
1562+
<category>Kotlin</category>
1563+
</intentionAction>
1564+
15601565
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
15611566
displayName="Object literal can be converted to lambda"
15621567
groupPath="Kotlin"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
3+
* that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.idea.intentions
7+
8+
import com.intellij.codeInsight.intention.LowPriorityAction
9+
import com.intellij.openapi.editor.Editor
10+
import org.jetbrains.kotlin.psi.*
11+
import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression
12+
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
13+
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
14+
15+
class RemoveLabeledReturnInLambdaIntention : SelfTargetingIntention<KtReturnExpression>(
16+
KtReturnExpression::class.java,
17+
"Remove labeled return from last expression in a lambda"
18+
), LowPriorityAction {
19+
override fun isApplicableTo(element: KtReturnExpression, caretOffset: Int): Boolean {
20+
val labelName = element.getLabelName() ?: return false
21+
val block = element.getStrictParentOfType<KtBlockExpression>() ?: return false
22+
if (block.statements.lastOrNull() != element) return false
23+
val callExpression = block.getStrictParentOfType<KtCallExpression>() ?: return false
24+
val lambdaArgument = callExpression.lambdaArguments.firstOrNull {
25+
val argumentExpression = it.getArgumentExpression()
26+
val lambda = when (argumentExpression) {
27+
is KtLambdaExpression -> argumentExpression
28+
is KtLabeledExpression -> argumentExpression.baseExpression as? KtLambdaExpression
29+
else -> null
30+
}
31+
lambda?.bodyExpression === block
32+
} ?: return false
33+
val callName = (lambdaArgument.getArgumentExpression() as? KtLabeledExpression)?.getLabelName()
34+
?: callExpression.getCallNameExpression()?.text ?: return false
35+
if (labelName != callName) return false
36+
text = "Remove return@$labelName"
37+
return true
38+
}
39+
40+
override fun applyTo(element: KtReturnExpression, editor: Editor?) {
41+
val returnedExpression = element.returnedExpression
42+
if (returnedExpression == null) {
43+
element.delete()
44+
} else {
45+
element.replace(returnedExpression)
46+
}
47+
}
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.jetbrains.kotlin.idea.intentions.RemoveLabeledReturnInLambdaIntention
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@label"
3+
4+
fun foo() {
5+
listOf(1,2,3).find label@{
6+
return@label <caret>true
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@label"
3+
4+
fun foo() {
5+
listOf(1,2,3).find label@{
6+
true
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@find"
3+
4+
fun foo() {
5+
listOf(1,2,3).find {
6+
return@find <caret>true
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@find"
3+
4+
fun foo() {
5+
listOf(1,2,3).find {
6+
true
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// WITH_RUNTIME
2+
// IS_APPLICABLE: FALSE
3+
4+
fun foo(): Boolean {
5+
return@foo <caret>true
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// WITH_RUNTIME
2+
// IS_APPLICABLE: FALSE
3+
4+
fun foo(): Boolean {
5+
listOf(1,2,3).find {
6+
return <caret>true
7+
}
8+
return false
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// WITH_RUNTIME
2+
// IS_APPLICABLE: FALSE
3+
4+
fun foo() {
5+
listOf(1,2,3).find {
6+
<caret>1
7+
return@find true
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// WITH_RUNTIME
2+
// IS_APPLICABLE: FALSE
3+
4+
fun foo() {
5+
listOf(1,2,3).forEach {
6+
listOf(1,2,3).find {
7+
return@forEach<caret>
8+
}
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@forEach"
3+
4+
fun foo() {
5+
listOf(1,2,3).forEach {
6+
<caret>return@forEach
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// WITH_RUNTIME
2+
// INTENTION_TEXT: "Remove return@forEach"
3+
4+
fun foo() {
5+
listOf(1,2,3).forEach {
6+
}
7+
}

idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)