Merge pull request #472 from wutalman/move_lambda

KT-4889: (Bug fix) Intention to move lambda outside parentheses now handles commas
This commit is contained in:
Svetlana Isakova
2014-05-29 17:11:41 +04:00
8 changed files with 90 additions and 9 deletions
@@ -31,15 +31,27 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<J
override fun applyTo(element: JetCallExpression, editor: Editor) {
val args = element.getValueArguments()
val literal = args.last!!.getArgumentExpression()?.getText() // we know args.last is non null
val callText = element.getText()
if (callText == null || literal == null) return
val endIndex = callText.lastIndexOf(",")
val newCall = if (endIndex > 0) {
"${callText.substring(0, endIndex)}) $literal"
} else {
"${callText.substring(0, callText.indexOf("("))} $literal"
}
val literal = args.last!!.getArgumentExpression()?.getText()
val calleeText = element.getCalleeExpression()?.getText()
if (calleeText == null || literal == null) return
val params =
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 =
if (params == "()") {
"$calleeText $literal"
} else {
"$calleeText$params $literal"
}
element.replace(JetPsiFactory.createExpression(element.getProject(), newCall))
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar({
<caret>val a = foo(1, 2)
})
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar {
val a = foo(1, 2)
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(x: Int, f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar(1, {
<caret>val a = foo(1, 2)
})
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(x: Int, f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar(1) {
val a = foo(1, 2)
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(a: Int, b: Int, f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar(1, 2, /* , , */ {
<caret>val a = foo(1, 2)
})
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: true
fun bar(a: Int, b: Int, f: () -> Unit) {}
fun foo(a: Int, b: Int) = 2
fun test() {
bar(1, 2) {
val a = foo(1, 2)
}
}
@@ -2537,6 +2537,21 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/inapplicable3.kt");
}
@TestMetadata("lambdaWithCommas.kt")
public void testLambdaWithCommas() throws Exception {
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt");
}
@TestMetadata("lambdaWithCommas2.kt")
public void testLambdaWithCommas2() throws Exception {
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt");
}
@TestMetadata("lambdaWithCommas3.kt")
public void testLambdaWithCommas3() throws Exception {
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt");
}
@TestMetadata("moveLambda1.kt")
public void testMoveLambda1() throws Exception {
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda1.kt");