Lambda to anonymous function: use callable builder (KT-7710)
This commit is contained in:
+25
-15
@@ -42,22 +42,32 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
|
||||
if (it.getTargetFunctionDescriptor(context) == descriptor) it.labeledExpression?.delete()
|
||||
}
|
||||
|
||||
val extension = descriptor.extensionReceiverParameter?.type?.let { "$it." } ?: ""
|
||||
val params = descriptor.valueParameters.joinToString { "${it.name}: ${it.type}" }
|
||||
val returnType = descriptor.returnType?.let { if (it.isUnit()) "" else ": $it" } ?: ""
|
||||
if (returnType.isNotEmpty()) {
|
||||
val lastStatement = bodyExpression.statements.lastOrNull()
|
||||
if (lastStatement != null && lastStatement !is KtReturnExpression) {
|
||||
val foldableReturns = BranchedFoldingUtils.getFoldableReturns(lastStatement)
|
||||
if (foldableReturns == null || foldableReturns.isEmpty()) {
|
||||
lastStatement.replace(psiFactory.createExpressionByPattern("return $0", lastStatement))
|
||||
// TODO: check type rendering (!!!)
|
||||
val anonymousFunction = psiFactory.createFunction(
|
||||
KtPsiFactory.CallableBuilder(KtPsiFactory.CallableBuilder.Target.FUNCTION).apply {
|
||||
typeParams()
|
||||
descriptor.extensionReceiverParameter?.type?.let {
|
||||
receiver(it.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
val anonymousFunction = element.replaced(
|
||||
psiFactory.createExpressionByPattern("fun $0($1)$2 { $3 }", extension, params, returnType, bodyExpression, reformat = false)
|
||||
) as KtNamedFunction
|
||||
name("")
|
||||
for (parameter in descriptor.valueParameters) {
|
||||
param(parameter.name.asString(), parameter.type.toString())
|
||||
}
|
||||
descriptor.returnType?.takeIf { !it.isUnit() }?.let {
|
||||
val lastStatement = bodyExpression.statements.lastOrNull()
|
||||
if (lastStatement != null && lastStatement !is KtReturnExpression) {
|
||||
val foldableReturns = BranchedFoldingUtils.getFoldableReturns(lastStatement)
|
||||
if (foldableReturns == null || foldableReturns.isEmpty()) {
|
||||
lastStatement.replace(psiFactory.createExpressionByPattern("return $0", lastStatement))
|
||||
}
|
||||
}
|
||||
returnType(it.toString())
|
||||
} ?: noReturnType()
|
||||
blockBody(" " + bodyExpression.text)
|
||||
}.asString()
|
||||
)
|
||||
|
||||
(anonymousFunction.parent as? KtLambdaArgument)?.also { it.moveInsideParentheses(it.analyze(BodyResolveMode.PARTIAL)) }
|
||||
val resultingFunction = element.replaced(anonymousFunction)
|
||||
(resultingFunction.parent as? KtLambdaArgument)?.also { it.moveInsideParentheses(it.analyze(BodyResolveMode.PARTIAL)) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
fun foo(f: (Int) -> String) {}
|
||||
|
||||
fun test() {
|
||||
foo(fun(it: Int): String { return "" })
|
||||
foo(fun(it: Int): String {
|
||||
return ""
|
||||
})
|
||||
}
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
fun bar(f: (Int, Int) -> String) {}
|
||||
|
||||
fun test() {
|
||||
bar(fun(i: Int, j: Int): String { return "$i$j" })
|
||||
bar(fun(i: Int, j: Int): String {
|
||||
return "$i$j"
|
||||
})
|
||||
}
|
||||
@@ -2,5 +2,7 @@ class Foo
|
||||
fun bar(f: Foo.() -> Unit) {}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
bar(fun Foo.() {})
|
||||
bar(fun Foo.() {
|
||||
|
||||
})
|
||||
}
|
||||
@@ -2,5 +2,7 @@ class Foo
|
||||
fun baz(f: Foo.(i: Int, j: Int) -> Int) {}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
baz(fun Foo.(i: Int, j: Int): Int { return i + j })
|
||||
baz(fun Foo.(i: Int, j: Int): Int {
|
||||
return i + j
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
fun foo(f: (Int) -> String) {}
|
||||
|
||||
fun test() {
|
||||
foo(fun(it: Int): String { return "$it" })
|
||||
foo(fun(it: Int): String {
|
||||
return "$it"
|
||||
})
|
||||
}
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
fun foo(f: (Int) -> String) {}
|
||||
|
||||
fun test() {
|
||||
foo(fun(it: Int): String { return "$it" })
|
||||
foo(fun(it: Int): String {
|
||||
return "$it"
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
fun baz(name: String, f: (Int) -> String) {}
|
||||
|
||||
fun test() {
|
||||
baz(name = "", f = fun(it: Int): String { return "$it" })
|
||||
baz(name = "", f = fun(it: Int): String {
|
||||
return "$it"
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
fun foo(f: () -> String) {}
|
||||
|
||||
fun test() {
|
||||
foo(fun(): String { return "" })
|
||||
foo(fun(): String {
|
||||
return ""
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user