Converter:
Fix inadequate formatting
This commit is contained in:
@@ -18,12 +18,12 @@ package org.jetbrains.jet.j2k.ast
|
||||
|
||||
public open class Block(val statements: List<Element>, val notEmpty: Boolean = false) : Statement() {
|
||||
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 {
|
||||
if (!isEmpty()) {
|
||||
return "{\n" + statements.toKotlin("\n") + "\n}"
|
||||
return "{\n" + statements .filter { !it.isEmpty() }.toKotlin("\n", "", "\n") + "}"
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -44,9 +44,9 @@ public open class Class(val converter: Converter,
|
||||
}
|
||||
|
||||
open fun primaryConstructorBodyToKotlin(): String? {
|
||||
val maybeConstructor: Constructor? = getPrimaryConstructor()
|
||||
val maybeConstructor = getPrimaryConstructor()
|
||||
if (maybeConstructor != null && !(maybeConstructor.block?.isEmpty() ?: true)) {
|
||||
return maybeConstructor.primaryBodyToKotlin()
|
||||
return maybeConstructor.primaryBodyToKotlin() + "\n"
|
||||
}
|
||||
|
||||
return ""
|
||||
@@ -57,7 +57,7 @@ public open class Class(val converter: Converter,
|
||||
open fun typeParameterWhereToKotlin(): String {
|
||||
if (hasWhere()) {
|
||||
val wheres = typeParameters.filter { it is TypeParameter }.map { (it as TypeParameter).getWhereToKotlin() }
|
||||
return " where " + wheres.makeString(", ") + " "
|
||||
return " where " + wheres.makeString(", ")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -122,7 +122,7 @@ public open class Class(val converter: Converter,
|
||||
open fun needsOpenModifier() = !isDefinitelyFinal() && converter.settings.openByDefault
|
||||
|
||||
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 {
|
||||
|
||||
@@ -44,8 +44,8 @@ public class Enum(converter: Converter,
|
||||
primaryConstructorSignatureToKotlin() +
|
||||
typeParametersToKotlin() +
|
||||
implementTypesToKotlin() +
|
||||
" {\n" + membersExceptConstructors().toKotlin("\n") + "\n" +
|
||||
(if (primaryConstructorBody.isEmpty()) "" else primaryConstructorBody + "\n") +
|
||||
" {\n" + membersExceptConstructors().toKotlin("\n", "", "\n") +
|
||||
(if (primaryConstructorBody.isEmpty()) "" else primaryConstructorBody) +
|
||||
"}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
|
||||
package org.jetbrains.jet.j2k.ast
|
||||
|
||||
public open class File(val packageName: String,
|
||||
val body: MutableList<Node>,
|
||||
val mainFunction: String) : Node {
|
||||
public class File(val packageName: String,
|
||||
val body: List<Node>,
|
||||
val mainFunction: String) : Node {
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
val common = body.toKotlin("\n") + "\n" + mainFunction
|
||||
override fun toKotlin(): String {
|
||||
val common = body.filterNot { it is Element && it.isEmpty() }.toKotlin("\n") + mainFunction
|
||||
if (packageName.isEmpty()) {
|
||||
return common
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public open class Function(val converter: Converter,
|
||||
if (hasWhere())
|
||||
{
|
||||
val wheres = typeParameters.filter { it is TypeParameter }.map { ((it as TypeParameter).getWhereToKotlin() ) }
|
||||
return " where " + wheres.makeString(", ") + " "
|
||||
return "where " + wheres.makeString(", ") + " "
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
@@ -28,8 +28,8 @@ public class Import(val name: String) : Node {
|
||||
public override fun toKotlin() = "import " + name
|
||||
}
|
||||
|
||||
public class ImportList(val imports: List<Import>) : Node {
|
||||
override fun toKotlin() = imports.filter {
|
||||
public class ImportList(val imports: List<Import>) : Element {
|
||||
val filteredImports = imports.filter {
|
||||
!it.name.isEmpty() && it.name !in NOT_NULL_ANNOTATIONS
|
||||
}.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))
|
||||
kotlinAnalogsForClass.isEmpty()
|
||||
}
|
||||
}.toKotlin("\n")
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
return filteredImports.isEmpty()
|
||||
}
|
||||
|
||||
override fun toKotlin() = filteredImports.toKotlin("\n")
|
||||
}
|
||||
|
||||
public fun Converter.importsToImportList(importList: PsiImportList): ImportList =
|
||||
|
||||
@@ -20,7 +20,8 @@ package org.jetbrains.jet.j2k.ast
|
||||
public abstract class Statement() : Element {
|
||||
class object {
|
||||
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 elseStatement: Element) : Expression() {
|
||||
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) {
|
||||
return result + "else\n" + elseStatement.toKotlin()
|
||||
return result + "\nelse\n" + elseStatement.toKotlin()
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -77,19 +77,19 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
}
|
||||
|
||||
public override fun visitForStatement(statement: PsiForStatement?) {
|
||||
val initialization: PsiStatement? = statement?.getInitialization()
|
||||
val update: PsiStatement? = statement?.getUpdate()
|
||||
val condition: PsiExpression? = statement?.getCondition()
|
||||
val body: PsiStatement? = statement?.getBody()
|
||||
val firstChild: PsiLocalVariable? = (if (initialization != null && (initialization.getFirstChild() is PsiLocalVariable))
|
||||
val initialization = statement?.getInitialization()
|
||||
val update = statement?.getUpdate()
|
||||
val condition = statement?.getCondition()
|
||||
val body = statement?.getBody()
|
||||
val firstChild = (if (initialization != null && (initialization.getFirstChild() is PsiLocalVariable))
|
||||
(initialization.getFirstChild() as PsiLocalVariable)
|
||||
else
|
||||
null)
|
||||
var bodyWriteCount: Int = countWritingAccesses(firstChild, body)
|
||||
var conditionWriteCount: Int = countWritingAccesses(firstChild, condition)
|
||||
var updateWriteCount: Int = countWritingAccesses(firstChild, update)
|
||||
val onceWritableIterator: Boolean = updateWriteCount == 1 && bodyWriteCount + conditionWriteCount == 0
|
||||
val operationTokenType: IElementType? = (if (condition is PsiBinaryExpression)
|
||||
var bodyWriteCount = countWritingAccesses(firstChild, body)
|
||||
var conditionWriteCount = countWritingAccesses(firstChild, condition)
|
||||
var updateWriteCount = countWritingAccesses(firstChild, update)
|
||||
val onceWritableIterator = updateWriteCount == 1 && bodyWriteCount + conditionWriteCount == 0
|
||||
val operationTokenType = (if (condition is PsiBinaryExpression)
|
||||
condition.getOperationTokenType()
|
||||
else
|
||||
null)
|
||||
@@ -98,8 +98,8 @@ public open class StatementVisitor(converter: Converter) : ElementVisitor(conver
|
||||
(isPlusPlusExpression(update.getChildren()[0])) && (operationTokenType == JavaTokenType.LT || operationTokenType == JavaTokenType.LE) &&
|
||||
initialization.getFirstChild() != null && (initialization.getFirstChild() is PsiLocalVariable) &&
|
||||
firstChild != null && firstChild.getNameIdentifier() != null && onceWritableIterator) {
|
||||
val end: Expression = getConverter().expressionToExpression((condition as PsiBinaryExpression).getROperand())
|
||||
val endExpression: Expression = (if (operationTokenType == JavaTokenType.LT)
|
||||
val end = getConverter().expressionToExpression((condition as PsiBinaryExpression).getROperand())
|
||||
val endExpression = (if (operationTokenType == JavaTokenType.LT)
|
||||
BinaryExpression(end, Identifier("1"), "-")
|
||||
else
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user