Fix OperatorToFunctionIntention: insert function literal expressions to arguments correctly

This commit is contained in:
Natalia Ukhorskaya
2015-12-16 16:37:36 +03:00
parent 6bfbec8c88
commit 7f0065c806
3 changed files with 11 additions and 4 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -199,9 +200,15 @@ public class OperatorToFunctionIntention : SelfTargetingIntention<KtExpression>(
val funcLitArgs = element.getLambdaArguments()
val calleeText = callee.getText()
val transformation = "$calleeText.${OperatorNameConventions.INVOKE.asString()}" +
(if (argumentString == null) "" else "($argumentString)")
(if (argumentString == null) "()" else "($argumentString)")
val transformed = KtPsiFactory(element).createExpression(transformation)
funcLitArgs.forEach { transformed.add(it) }
val callExpression = transformed.getCalleeExpressionIfAny()?.parent as? KtCallExpression
if (callExpression != null) {
funcLitArgs.forEach { callExpression.add(it) }
if (argumentString == null) {
callExpression.valueArgumentList?.delete()
}
}
return callee.getParent()!!.replace(transformed) as KtExpression
}
@@ -4,5 +4,5 @@ class Mocha() {
}
fun main() {
val mocha = Mocha()
val testing = mocha.invoke(1, "fire"){ x: Int -> "hello world" }
val testing = mocha.invoke(1, "fire") { x: Int -> "hello world" }
}
@@ -3,5 +3,5 @@ class Mocha() {
}
fun main() {
val mocha = Mocha()
val testing = mocha.invoke{ x: Int -> "hello world" }
val testing = mocha.invoke { x: Int -> "hello world" }
}