J2K: If last expression in Method Call is Lambda add it outside the parenthesis
This commit is contained in:
@@ -59,4 +59,8 @@ public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<J
|
||||
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
||||
element.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
|
||||
fun applyTo(element: JetCallExpression) {
|
||||
element.moveFunctionLiteralOutsideParentheses()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,10 +26,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||
import org.jetbrains.kotlin.idea.intentions.ConvertToStringTemplateIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.IfNullToElvisIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgumentsIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.SimplifyNegatedBinaryExpressionIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.*
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix
|
||||
@@ -172,6 +169,18 @@ public class J2kPostProcessor(private val formatCode: Boolean) : PostProcessor {
|
||||
super.visitBinaryExpression(expression)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitCallExpression(expression: JetCallExpression) {
|
||||
super.visitCallExpression(expression)
|
||||
|
||||
val literalArgument = expression.valueArguments.lastOrNull()?.getArgumentExpression()?.unpackFunctionLiteral()
|
||||
if (literalArgument != null) {
|
||||
val intention = MoveLambdaOutsideParenthesesIntention()
|
||||
if (intention.isApplicableTo(expression, literalArgument.textOffset)) {
|
||||
intention.applyTo(expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
for (typeArgs in redundantTypeArgs) {
|
||||
|
||||
@@ -138,7 +138,7 @@ class ForConverter(
|
||||
block.generateCode(builder)
|
||||
}
|
||||
else {
|
||||
val call = MethodCallExpression.build(null, "run", listOf(), listOf(), false, LambdaExpression(null, block))
|
||||
val call = MethodCallExpression.build(null, "run", listOf(LambdaExpression(null, block)), listOf(), false)
|
||||
call.generateCode(builder)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,14 +64,14 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
||||
else {
|
||||
val block = Block(listOf(description), LBrace().assignNoPrototype(), RBrace().assignNoPrototype())
|
||||
val lambda = LambdaExpression(null, block.assignNoPrototype())
|
||||
result = MethodCallExpression.build(null, "assert", listOf(condition), listOf(), false, lambda)
|
||||
result = MethodCallExpression.build(null, "assert", listOf(condition, lambda), listOf(), false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitBlockStatement(statement: PsiBlockStatement) {
|
||||
val block = codeConverter.convertBlock(statement.getCodeBlock())
|
||||
result = MethodCallExpression.build(null, "run", listOf(), listOf(), false, LambdaExpression(null, block).assignNoPrototype())
|
||||
result = MethodCallExpression.build(null, "run", listOf(LambdaExpression(null, block).assignNoPrototype()), listOf(), false)
|
||||
}
|
||||
|
||||
override fun visitBreakStatement(statement: PsiBreakStatement) {
|
||||
@@ -225,7 +225,7 @@ class DefaultStatementConverter : JavaElementVisitor(), StatementConverter {
|
||||
val parameter = LambdaParameter(Identifier(variable.name!!).assignNoPrototype(), null).assignNoPrototype()
|
||||
val parameterList = ParameterList(listOf(parameter)).assignNoPrototype()
|
||||
val lambda = LambdaExpression(parameterList, block)
|
||||
expression = MethodCallExpression.build(codeConverter.convertExpression(variable.getInitializer()), "use", listOf(), listOf(), false, lambda)
|
||||
expression = MethodCallExpression.build(codeConverter.convertExpression(variable.getInitializer()), "use", listOf(lambda), listOf(), false)
|
||||
expression.assignNoPrototype()
|
||||
block = Block(listOf(expression), LBrace().assignNoPrototype(), RBrace().assignNoPrototype()).assignNoPrototype()
|
||||
}
|
||||
|
||||
@@ -22,47 +22,37 @@ class MethodCallExpression(
|
||||
val methodExpression: Expression,
|
||||
val arguments: List<Expression>,
|
||||
val typeArguments: List<Type>,
|
||||
override val isNullable: Boolean,
|
||||
val lambdaArgument: LambdaExpression? = null
|
||||
override val isNullable: Boolean
|
||||
) : Expression() {
|
||||
|
||||
override fun generateCode(builder: CodeBuilder) {
|
||||
builder.appendOperand(this, methodExpression).append(typeArguments, ", ", "<", ">")
|
||||
if (arguments.isNotEmpty() || lambdaArgument == null) {
|
||||
builder.append("(").append(arguments, ", ").append(")")
|
||||
}
|
||||
if (lambdaArgument != null) {
|
||||
builder.append(lambdaArgument)
|
||||
}
|
||||
builder.append("(").append(arguments, ", ").append(")")
|
||||
}
|
||||
|
||||
companion object {
|
||||
public fun buildNotNull(receiver: Expression?,
|
||||
methodName: String,
|
||||
arguments: List<Expression> = listOf(),
|
||||
typeArguments: List<Type> = listOf(),
|
||||
lambdaArgument: LambdaExpression? = null): MethodCallExpression
|
||||
= build(receiver, methodName, arguments, typeArguments, false, lambdaArgument)
|
||||
typeArguments: List<Type> = listOf()): MethodCallExpression
|
||||
= build(receiver, methodName, arguments, typeArguments, false)
|
||||
|
||||
public fun buildNullable(receiver: Expression?,
|
||||
methodName: String,
|
||||
arguments: List<Expression> = listOf(),
|
||||
typeArguments: List<Type> = listOf(),
|
||||
lambdaArgument: LambdaExpression? = null): MethodCallExpression
|
||||
= build(receiver, methodName, arguments, typeArguments, true, lambdaArgument)
|
||||
typeArguments: List<Type> = listOf()): MethodCallExpression
|
||||
= build(receiver, methodName, arguments, typeArguments, true)
|
||||
|
||||
public fun build(receiver: Expression?,
|
||||
methodName: String,
|
||||
arguments: List<Expression>,
|
||||
typeArguments: List<Type>,
|
||||
isNullable: Boolean,
|
||||
lambdaArgument: LambdaExpression? = null): MethodCallExpression {
|
||||
isNullable: Boolean): MethodCallExpression {
|
||||
val identifier = Identifier(methodName, false).assignNoPrototype()
|
||||
return MethodCallExpression(if (receiver != null) QualifiedExpression(receiver, identifier).assignNoPrototype() else identifier,
|
||||
arguments,
|
||||
typeArguments,
|
||||
isNullable,
|
||||
lambdaArgument)
|
||||
isNullable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
val d3 = Array<Array<IntArray>>(5, { arrayOfNulls<IntArray>(5) })
|
||||
val d3 = Array<Array<IntArray>>(5) { arrayOfNulls<IntArray>(5) }
|
||||
+1
-1
@@ -1 +1 @@
|
||||
val d2 = Array(5, { IntArray(5) })
|
||||
val d2 = Array(5) { IntArray(5) }
|
||||
@@ -1 +1 @@
|
||||
val ss = Array<Array<String>>(5, { arrayOfNulls<String>(5) })
|
||||
val ss = Array<Array<String>>(5) { arrayOfNulls<String>(5) }
|
||||
@@ -1 +1 @@
|
||||
val sss = Array(5, { Array<Array<String>>(5, { arrayOfNulls<String>(5) }) })
|
||||
val sss = Array(5) { Array<Array<String>>(5) { arrayOfNulls<String>(5) } }
|
||||
+15
-15
@@ -18,29 +18,29 @@ public class Java8Class {
|
||||
}
|
||||
|
||||
public fun foo() {
|
||||
foo0({ "42" })
|
||||
foo0({ "42" })
|
||||
foo0({
|
||||
foo0 { "42" }
|
||||
foo0 { "42" }
|
||||
foo0 {
|
||||
helper()
|
||||
"42"
|
||||
})
|
||||
}
|
||||
|
||||
foo1({ i -> "42" })
|
||||
foo1({ i -> "42" })
|
||||
foo1({ i: Int ->
|
||||
foo1 { i -> "42" }
|
||||
foo1 { i -> "42" }
|
||||
foo1 { i: Int ->
|
||||
helper()
|
||||
if (i > 1) {
|
||||
return@foo1 "42"
|
||||
}
|
||||
|
||||
"43"
|
||||
})
|
||||
}
|
||||
|
||||
foo2({ i, j -> "42" })
|
||||
foo2({ i: Int, j: Int ->
|
||||
foo2 { i, j -> "42" }
|
||||
foo2 { i: Int, j: Int ->
|
||||
helper()
|
||||
"42"
|
||||
})
|
||||
}
|
||||
|
||||
val f = { i: Int, k: Int ->
|
||||
helper()
|
||||
@@ -68,19 +68,19 @@ public class Java8Class {
|
||||
|
||||
val runnable = { }
|
||||
|
||||
foo1({ i: Int ->
|
||||
foo1 { i: Int ->
|
||||
if (i > 1) {
|
||||
return@foo1 "42"
|
||||
}
|
||||
|
||||
foo0({
|
||||
foo0 {
|
||||
if (true) {
|
||||
return@foo0 "42"
|
||||
}
|
||||
"43"
|
||||
})
|
||||
}
|
||||
|
||||
"43"
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user