Bug fix in intention to move lambda outside parentheses, handles commas better
This commit is contained in:
+21
-9
@@ -31,15 +31,27 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<J
|
|||||||
|
|
||||||
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() // we know args.last is non null
|
val literal = args.last!!.getArgumentExpression()?.getText()
|
||||||
val callText = element.getText()
|
val calleeText = element.getCalleeExpression()?.getText()
|
||||||
if (callText == null || literal == null) return
|
if (calleeText == null || literal == null) return
|
||||||
val endIndex = callText.lastIndexOf(",")
|
|
||||||
val newCall = if (endIndex > 0) {
|
val params =
|
||||||
"${callText.substring(0, endIndex)}) $literal"
|
args.subList(0, args.size - 1).map {
|
||||||
} else {
|
val name = it?.getArgumentName()?.getText()
|
||||||
"${callText.substring(0, callText.indexOf("("))} $literal"
|
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))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2256,6 +2256,21 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
|||||||
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/inapplicable3.kt");
|
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")
|
@TestMetadata("moveLambda1.kt")
|
||||||
public void testMoveLambda1() throws Exception {
|
public void testMoveLambda1() throws Exception {
|
||||||
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda1.kt");
|
doTestMoveLambdaOutsideParentheses("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user