Supported moving a labeled lambda outside parentheses
This commit is contained in:
+12
-15
@@ -20,37 +20,34 @@ import com.intellij.openapi.editor.Editor
|
|||||||
import org.jetbrains.jet.lang.psi.JetCallExpression
|
import org.jetbrains.jet.lang.psi.JetCallExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression
|
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||||
|
import org.jetbrains.jet.lang.psi.JetExpression
|
||||||
|
import org.jetbrains.jet.lang.psi.JetLabeledExpression
|
||||||
|
|
||||||
public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<JetCallExpression>(
|
public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<JetCallExpression>(
|
||||||
"move.lambda.outside.parentheses", javaClass()) {
|
"move.lambda.outside.parentheses", javaClass()) {
|
||||||
|
|
||||||
|
private fun isLambdaOrLabeledLambda(expression: JetExpression?): Boolean =
|
||||||
|
expression is JetFunctionLiteralExpression ||
|
||||||
|
(expression is JetLabeledExpression && isLambdaOrLabeledLambda(expression.getBaseExpression()))
|
||||||
|
|
||||||
override fun isApplicableTo(element: JetCallExpression): Boolean {
|
override fun isApplicableTo(element: JetCallExpression): Boolean {
|
||||||
val args = element.getValueArguments()
|
val args = element.getValueArguments()
|
||||||
return args.size > 0 && args.last?.getArgumentExpression() is JetFunctionLiteralExpression
|
return args.size > 0 && isLambdaOrLabeledLambda(args.last?.getArgumentExpression())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
||||||
val args = element.getValueArguments()
|
val args = element.getValueArguments()
|
||||||
val literal = args.last!!.getArgumentExpression()?.getText()
|
val functionLiteral = args.last!!.getArgumentExpression()?.getText()
|
||||||
val calleeText = element.getCalleeExpression()?.getText()
|
val calleeText = element.getCalleeExpression()?.getText()
|
||||||
if (calleeText == null || literal == null) return
|
if (calleeText == null || functionLiteral == null) return
|
||||||
|
|
||||||
val params =
|
val params = args.subList(0, args.size - 1).map { it?.asElement()?.getText() ?: "" }.makeString(", ", "(", ")")
|
||||||
args.subList(0, args.size - 1).map {
|
|
||||||
val name = it?.getArgumentName()?.getText()
|
|
||||||
val arg = it?.getArgumentExpression()?.getText()
|
|
||||||
if (name != null) {
|
|
||||||
"$name = $arg"
|
|
||||||
} else {
|
|
||||||
"$arg"
|
|
||||||
}
|
|
||||||
}.makeString(", ", "(", ")")
|
|
||||||
|
|
||||||
val newCall =
|
val newCall =
|
||||||
if (params == "()") {
|
if (params == "()") {
|
||||||
"$calleeText $literal"
|
"$calleeText $functionLiteral"
|
||||||
} else {
|
} else {
|
||||||
"$calleeText$params $literal"
|
"$calleeText$params $functionLiteral"
|
||||||
}
|
}
|
||||||
element.replace(JetPsiFactory.createExpression(element.getProject(), newCall))
|
element.replace(JetPsiFactory.createExpression(element.getProject(), newCall))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IS_APPLICABLE: true
|
||||||
|
fun foo() {
|
||||||
|
bar<caret>(2, @l{ it })
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(a: Int, b: (Int) -> Int) {
|
||||||
|
b(a)
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IS_APPLICABLE: true
|
||||||
|
fun foo() {
|
||||||
|
bar(2) @l{ it }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(a: Int, b: (Int) -> Int) {
|
||||||
|
b(a)
|
||||||
|
}
|
||||||
@@ -2537,6 +2537,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
|||||||
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/inapplicable3.kt");
|
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/inapplicable3.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("labeledLambda.kt")
|
||||||
|
public void testLabeledLambda() throws Exception {
|
||||||
|
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/labeledLambda.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("lambdaWithCommas.kt")
|
@TestMetadata("lambdaWithCommas.kt")
|
||||||
public void testLambdaWithCommas() throws Exception {
|
public void testLambdaWithCommas() throws Exception {
|
||||||
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt");
|
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user