diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/MoveLambdaOutsideParenthesesIntention.kt b/idea/src/org/jetbrains/jet/plugin/intentions/MoveLambdaOutsideParenthesesIntention.kt index fa68c9d4351..dc5660429fb 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/MoveLambdaOutsideParenthesesIntention.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/MoveLambdaOutsideParenthesesIntention.kt @@ -31,15 +31,27 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention 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)) } } \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt new file mode 100644 index 00000000000..2b6af09dc8a --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt @@ -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) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt.after b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt.after new file mode 100644 index 00000000000..b0c5817f991 --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas.kt.after @@ -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) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt new file mode 100644 index 00000000000..7f19a6a4484 --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt @@ -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) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt.after b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt.after new file mode 100644 index 00000000000..032e39c1d6b --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas2.kt.after @@ -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) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt new file mode 100644 index 00000000000..025166b9029 --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt @@ -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) + }) +} \ No newline at end of file diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt.after b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt.after new file mode 100644 index 00000000000..6587e445215 --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/lambdaWithCommas3.kt.after @@ -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) + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 318b3cbc91f..950aab2c417 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -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");