New J2K: Clean-up parenthesizing and block rendering in code builder

This commit is contained in:
Simon Ogorodnik
2018-07-20 13:53:51 +03:00
committed by Ilya Kirillov
parent 07a33ceebd
commit 7e2be15fcc
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.j2k package org.jetbrains.kotlin.j2k
import org.jetbrains.kotlin.j2k.NewCodeBuilder.ParenthesisKind.*
import org.jetbrains.kotlin.j2k.ast.Nullability import org.jetbrains.kotlin.j2k.ast.Nullability
import org.jetbrains.kotlin.j2k.tree.* import org.jetbrains.kotlin.j2k.tree.*
import org.jetbrains.kotlin.j2k.tree.impl.* import org.jetbrains.kotlin.j2k.tree.impl.*
@@ -107,12 +108,9 @@ class NewCodeBuilder {
} }
if (klass.declarationList.isNotEmpty()) { if (klass.declarationList.isNotEmpty()) {
printer.println(" {") printer.block(multiline = true) {
printer.pushIndent() klass.declarationList.forEach { it.accept(this) }
klass.declarationList.forEach { it.accept(this) } }
printer.popIndent()
printer.println("}")
} else { } else {
printer.println() printer.println()
} }
@@ -154,11 +152,42 @@ class NewCodeBuilder {
printer.printWithNoIndent(")", ": ") printer.printWithNoIndent(")", ": ")
ktFunction.returnType.accept(this) ktFunction.returnType.accept(this)
if (ktFunction.block !== JKBodyStub) { if (ktFunction.block !== JKBodyStub) {
printer.printlnWithNoIndent(" {") printer.block(multiline = true) {
printer.pushIndent() ktFunction.block.accept(this)
ktFunction.block.accept(this) }
printer.popIndent() }
printer.printWithNoIndent("}") }
override fun visitIfElseExpression(ifElseExpression: JKIfElseExpression) {
printer.printWithNoIndent("if (")
ifElseExpression.condition.accept(this)
printer.printWithNoIndent(")")
ifElseExpression.thenBranch.accept(this)
printer.printWithNoIndent(" else ")
ifElseExpression.elseBranch.accept(this)
}
override fun visitIfStatement(ifStatement: JKIfStatement) {
printer.printWithNoIndent("if (")
ifStatement.condition.accept(this)
printer.printWithNoIndent(")")
renderStatementOrBlock(ifStatement.thenBranch)
}
override fun visitIfElseStatement(ifElseStatement: JKIfElseStatement) {
visitIfStatement(ifElseStatement)
printer.printWithNoIndent(" else ")
renderStatementOrBlock(ifElseStatement.elseBranch)
}
private fun renderStatementOrBlock(statement: JKStatement, multiline: Boolean = false) {
if (statement is JKBlockStatement) {
printer.block(multiline) {
statement.block.statements.forEach { it.accept(this) }
}
} else {
statement.accept(this)
} }
} }
@@ -206,15 +235,15 @@ class NewCodeBuilder {
override fun visitMethodCallExpression(methodCallExpression: JKMethodCallExpression) { override fun visitMethodCallExpression(methodCallExpression: JKMethodCallExpression) {
printer.printWithNoIndent(FqName(methodCallExpression.identifier.fqName).shortName().asString()) printer.printWithNoIndent(FqName(methodCallExpression.identifier.fqName).shortName().asString())
printer.printWithNoIndent("(") printer.par {
methodCallExpression.arguments.accept(this) methodCallExpression.arguments.accept(this)
printer.printWithNoIndent(")") }
} }
override fun visitParenthesizedExpression(parenthesizedExpression: JKParenthesizedExpression) { override fun visitParenthesizedExpression(parenthesizedExpression: JKParenthesizedExpression) {
printer.printWithNoIndent("(") printer.par {
parenthesizedExpression.expression.accept(this) parenthesizedExpression.expression.accept(this)
printer.printWithNoIndent(")") }
} }
override fun visitDeclarationStatement(declarationStatement: JKDeclarationStatement) { override fun visitDeclarationStatement(declarationStatement: JKDeclarationStatement) {
@@ -232,17 +261,8 @@ class NewCodeBuilder {
override fun visitWhileStatement(whileStatement: JKWhileStatement) { override fun visitWhileStatement(whileStatement: JKWhileStatement) {
printer.print("while(") printer.print("while(")
whileStatement.condition.accept(this) whileStatement.condition.accept(this)
printer.printlnWithNoIndent(")", "{") printer.printWithNoIndent(")")
printer.pushIndent() renderStatementOrBlock(whileStatement.body, multiline = true)
val body = whileStatement.body
if (body is JKBlockStatement) {
body.block.statements.forEach { it.accept(this) }
} else {
body.accept(this)
}
printer.popIndent()
printer.println("}")
} }
override fun visitLocalVariable(localVariable: JKLocalVariable) { override fun visitLocalVariable(localVariable: JKLocalVariable) {
@@ -299,11 +319,31 @@ class NewCodeBuilder {
override fun visitArrayAccessExpression(arrayAccessExpression: JKArrayAccessExpression) { override fun visitArrayAccessExpression(arrayAccessExpression: JKArrayAccessExpression) {
arrayAccessExpression.expression.accept(this) arrayAccessExpression.expression.accept(this)
printer.printWithNoIndent("[") printer.par(SQUARE) { arrayAccessExpression.indexExpression.accept(this) }
arrayAccessExpression.indexExpression.accept(this)
printer.printWithNoIndent("]")
} }
private inline fun Printer.indented(block: () -> Unit) {
this.pushIndent()
block()
this.popIndent()
}
private inline fun Printer.block(multiline: Boolean = false, crossinline body: () -> Unit) {
par(if (multiline) CURVED_MULTILINE else CURVED) {
indented(body)
}
}
private inline fun Printer.par(kind: ParenthesisKind = ParenthesisKind.ROUND, body: () -> Unit) {
this.printWithNoIndent(kind.open)
body()
this.printWithNoIndent(kind.close)
}
}
private enum class ParenthesisKind(val open: String, val close: String) {
ROUND("(", ")"), SQUARE("[", "]"), CURVED("{", "}"), CURVED_MULTILINE("{\n", "}\n"), INLINE_COMMENT("/*", "*/")
override fun visitLambdaExpression(lambdaExpression: JKLambdaExpression) { override fun visitLambdaExpression(lambdaExpression: JKLambdaExpression) {
printer.printWithNoIndent("{") printer.printWithNoIndent("{")
lambdaExpression.parameters.firstOrNull()?.accept(this) lambdaExpression.parameters.firstOrNull()?.accept(this)