Converter:

Fix inadequate formatting
This commit is contained in:
Pavel V. Talanov
2013-11-26 18:28:52 +04:00
parent 741e031ff1
commit 04edb3d742
8 changed files with 39 additions and 32 deletions
+2 -2
View File
@@ -18,12 +18,12 @@ package org.jetbrains.jet.j2k.ast
public open class Block(val statements: List<Element>, val notEmpty: Boolean = false) : Statement() { public open class Block(val statements: List<Element>, val notEmpty: Boolean = false) : Statement() {
public override fun isEmpty(): Boolean { public override fun isEmpty(): Boolean {
return !notEmpty && (statements.size() == 0 || statements.all { it == Statement.EMPTY_STATEMENT }) return !notEmpty && statements.all { it.isEmpty() }
} }
public override fun toKotlin(): String { public override fun toKotlin(): String {
if (!isEmpty()) { if (!isEmpty()) {
return "{\n" + statements.toKotlin("\n") + "\n}" return "{\n" + statements .filter { !it.isEmpty() }.toKotlin("\n", "", "\n") + "}"
} }
return "" return ""
+4 -4
View File
@@ -44,9 +44,9 @@ public open class Class(val converter: Converter,
} }
open fun primaryConstructorBodyToKotlin(): String? { open fun primaryConstructorBodyToKotlin(): String? {
val maybeConstructor: Constructor? = getPrimaryConstructor() val maybeConstructor = getPrimaryConstructor()
if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty() ?: true)) { if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty() ?: true)) {
return maybeConstructor.primaryBodyToKotlin() return maybeConstructor.primaryBodyToKotlin() + "\n"
} }
return "" return ""
@@ -57,7 +57,7 @@ public open class Class(val converter: Converter,
open fun typeParameterWhereToKotlin(): String { open fun typeParameterWhereToKotlin(): String {
if (hasWhere()) { if (hasWhere()) {
val wheres = typeParameters.filter { it is TypeParameter }.map { (it as TypeParameter).getWhereToKotlin() } val wheres = typeParameters.filter { it is TypeParameter }.map { (it as TypeParameter).getWhereToKotlin() }
return " where " + wheres.makeString(", ") + " " return " where " + wheres.makeString(", ")
} }
return "" return ""
} }
@@ -122,7 +122,7 @@ public open class Class(val converter: Converter,
open fun needsOpenModifier() = !isDefinitelyFinal() && converter.settings.openByDefault open fun needsOpenModifier() = !isDefinitelyFinal() && converter.settings.openByDefault
fun bodyToKotlin(): String { fun bodyToKotlin(): String {
return " {\n" + getNonStatic(membersExceptConstructors()).toKotlin("\n") + "\n" + primaryConstructorBodyToKotlin() + "\n" + classObjectToKotlin() + "\n}" return " {\n" + getNonStatic(membersExceptConstructors()).toKotlin("\n", "", "\n") + primaryConstructorBodyToKotlin() + classObjectToKotlin() + "}"
} }
private fun classObjectToKotlin(): String { private fun classObjectToKotlin(): String {
+2 -2
View File
@@ -44,8 +44,8 @@ public class Enum(converter: Converter,
primaryConstructorSignatureToKotlin() + primaryConstructorSignatureToKotlin() +
typeParametersToKotlin() + typeParametersToKotlin() +
implementTypesToKotlin() + implementTypesToKotlin() +
" {\n" + membersExceptConstructors().toKotlin("\n") + "\n" + " {\n" + membersExceptConstructors().toKotlin("\n", "", "\n") +
(if (primaryConstructorBody.isEmpty()) "" else primaryConstructorBody + "\n") + (if (primaryConstructorBody.isEmpty()) "" else primaryConstructorBody) +
"}" "}"
} }
} }
+5 -5
View File
@@ -16,12 +16,12 @@
package org.jetbrains.jet.j2k.ast package org.jetbrains.jet.j2k.ast
public open class File(val packageName: String, public class File(val packageName: String,
val body: MutableList<Node>, val body: List<Node>,
val mainFunction: String) : Node { val mainFunction: String) : Node {
public override fun toKotlin(): String { override fun toKotlin(): String {
val common = body.toKotlin("\n") + "\n" + mainFunction val common = body.filterNot { it is Element && it.isEmpty() }.toKotlin("\n") + mainFunction
if (packageName.isEmpty()) { if (packageName.isEmpty()) {
return common return common
} }
@@ -42,7 +42,7 @@ public open class Function(val converter: Converter,
if (hasWhere()) if (hasWhere())
{ {
val wheres = typeParameters.filter { it is TypeParameter }.map { ((it as TypeParameter).getWhereToKotlin() ) } val wheres = typeParameters.filter { it is TypeParameter }.map { ((it as TypeParameter).getWhereToKotlin() ) }
return " where " + wheres.makeString(", ") + " " return "where " + wheres.makeString(", ") + " "
} }
return "" return ""
+9 -3
View File
@@ -28,8 +28,8 @@ public class Import(val name: String) : Node {
public override fun toKotlin() = "import " + name public override fun toKotlin() = "import " + name
} }
public class ImportList(val imports: List<Import>) : Node { public class ImportList(val imports: List<Import>) : Element {
override fun toKotlin() = imports.filter { val filteredImports = imports.filter {
!it.name.isEmpty() && it.name !in NOT_NULL_ANNOTATIONS !it.name.isEmpty() && it.name !in NOT_NULL_ANNOTATIONS
}.filter { }.filter {
// If name is invalid, like with star imports, don't try to filter // If name is invalid, like with star imports, don't try to filter
@@ -40,7 +40,13 @@ public class ImportList(val imports: List<Import>) : Node {
val kotlinAnalogsForClass = JavaToKotlinClassMap.getInstance().mapPlatformClass(FqName(it.name)) val kotlinAnalogsForClass = JavaToKotlinClassMap.getInstance().mapPlatformClass(FqName(it.name))
kotlinAnalogsForClass.isEmpty() kotlinAnalogsForClass.isEmpty()
} }
}.toKotlin("\n") }
override fun isEmpty(): Boolean {
return filteredImports.isEmpty()
}
override fun toKotlin() = filteredImports.toKotlin("\n")
} }
public fun Converter.importsToImportList(importList: PsiImportList): ImportList = public fun Converter.importsToImportList(importList: PsiImportList): ImportList =
@@ -20,7 +20,8 @@ package org.jetbrains.jet.j2k.ast
public abstract class Statement() : Element { public abstract class Statement() : Element {
class object { class object {
public val EMPTY_STATEMENT: Statement = object : Statement() { public val EMPTY_STATEMENT: Statement = object : Statement() {
public override fun toKotlin() = "" override fun toKotlin() = ""
override fun isEmpty() = true
} }
} }
} }
@@ -55,9 +56,9 @@ public open class IfStatement(val condition: Expression,
val thenStatement: Element, val thenStatement: Element,
val elseStatement: Element) : Expression() { val elseStatement: Element) : Expression() {
public override fun toKotlin(): String { public override fun toKotlin(): String {
val result: String = "if (" + condition.toKotlin() + ")\n" + thenStatement.toKotlin() + "\n" val result: String = "if (" + condition.toKotlin() + ")\n" + thenStatement.toKotlin()
if (elseStatement != Statement.EMPTY_STATEMENT) { if (elseStatement != Statement.EMPTY_STATEMENT) {
return result + "else\n" + elseStatement.toKotlin() return result + "\nelse\n" + elseStatement.toKotlin()
} }
return result return result
@@ -77,19 +77,19 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
} }
public override fun visitForStatement(statement: PsiForStatement?) { public override fun visitForStatement(statement: PsiForStatement?) {
val initialization: PsiStatement? = statement?.getInitialization() val initialization = statement?.getInitialization()
val update: PsiStatement? = statement?.getUpdate() val update = statement?.getUpdate()
val condition: PsiExpression? = statement?.getCondition() val condition = statement?.getCondition()
val body: PsiStatement? = statement?.getBody() val body = statement?.getBody()
val firstChild: PsiLocalVariable? = (if (initialization != null && (initialization.getFirstChild() is PsiLocalVariable)) val firstChild = (if (initialization != null && (initialization.getFirstChild() is PsiLocalVariable))
(initialization.getFirstChild() as PsiLocalVariable) (initialization.getFirstChild() as PsiLocalVariable)
else else
null) null)
var bodyWriteCount: Int = countWritingAccesses(firstChild, body) var bodyWriteCount = countWritingAccesses(firstChild, body)
var conditionWriteCount: Int = countWritingAccesses(firstChild, condition) var conditionWriteCount = countWritingAccesses(firstChild, condition)
var updateWriteCount: Int = countWritingAccesses(firstChild, update) var updateWriteCount = countWritingAccesses(firstChild, update)
val onceWritableIterator: Boolean = updateWriteCount == 1 && bodyWriteCount + conditionWriteCount == 0 val onceWritableIterator = updateWriteCount == 1 && bodyWriteCount + conditionWriteCount == 0
val operationTokenType: IElementType? = (if (condition is PsiBinaryExpression) val operationTokenType = (if (condition is PsiBinaryExpression)
condition.getOperationTokenType() condition.getOperationTokenType()
else else
null) null)
@@ -98,8 +98,8 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
(isPlusPlusExpression(update.getChildren()[0])) && (operationTokenType == JavaTokenType.LT || operationTokenType == JavaTokenType.LE) && (isPlusPlusExpression(update.getChildren()[0])) && (operationTokenType == JavaTokenType.LT || operationTokenType == JavaTokenType.LE) &&
initialization.getFirstChild() != null && (initialization.getFirstChild() is PsiLocalVariable) && initialization.getFirstChild() != null && (initialization.getFirstChild() is PsiLocalVariable) &&
firstChild != null && firstChild.getNameIdentifier() != null && onceWritableIterator) { firstChild != null && firstChild.getNameIdentifier() != null && onceWritableIterator) {
val end: Expression = getConverter().expressionToExpression((condition as PsiBinaryExpression).getROperand()) val end = getConverter().expressionToExpression((condition as PsiBinaryExpression).getROperand())
val endExpression: Expression = (if (operationTokenType == JavaTokenType.LT) val endExpression = (if (operationTokenType == JavaTokenType.LT)
BinaryExpression(end, Identifier("1"), "-") BinaryExpression(end, Identifier("1"), "-")
else else
end) end)