New J2K: Use JKDeclaration instead of ValIdentifier in ForLoop
This commit is contained in:
committed by
Ilya Kirillov
parent
d228d6110c
commit
e10a81a942
@@ -47,7 +47,7 @@ class NewCodeBuilder {
|
||||
|
||||
override fun visitKtForInStatement(ktForInStatement: JKKtForInStatement) {
|
||||
printer.printWithNoIndent("for (")
|
||||
printer.printWithNoIndent(ktForInStatement.variableIdentifier.value)
|
||||
ktForInStatement.declaration.accept(this)
|
||||
printer.printWithNoIndent(" in ")
|
||||
ktForInStatement.iterationExpression.accept(this)
|
||||
printer.printWithNoIndent(") ")
|
||||
@@ -190,7 +190,6 @@ class NewCodeBuilder {
|
||||
printer.printWithNoIndent(" = ")
|
||||
ktProperty.initializer.accept(this)
|
||||
}
|
||||
printer.printlnWithNoIndent()
|
||||
}
|
||||
|
||||
override fun visitKtInitDeclaration(ktInitDeclaration: JKKtInitDeclaration) {
|
||||
@@ -369,6 +368,7 @@ class NewCodeBuilder {
|
||||
override fun visitDeclarationStatement(declarationStatement: JKDeclarationStatement) {
|
||||
declarationStatement.declaredStatements.forEach {
|
||||
it.accept(this)
|
||||
printer.printlnWithNoIndent()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -386,14 +386,16 @@ class NewCodeBuilder {
|
||||
}
|
||||
|
||||
override fun visitLocalVariable(localVariable: JKLocalVariable) {
|
||||
if (localVariable.modifierList.modality == JKModalityModifier.Modality.FINAL) {
|
||||
printer.print("val")
|
||||
} else {
|
||||
printer.print("var")
|
||||
if (localVariable.parent !is JKKtForInStatement) {
|
||||
if (localVariable.modifierList.modality == JKModalityModifier.Modality.FINAL) {
|
||||
printer.print("val")
|
||||
} else {
|
||||
printer.print("var")
|
||||
}
|
||||
}
|
||||
|
||||
printer.printWithNoIndent(" ", localVariable.name.value)
|
||||
if (localVariable.type.type != JKContextType) {
|
||||
if (localVariable.type.type != JKContextType && localVariable.type.type !is JKNoTypeImpl) {
|
||||
printer.printWithNoIndent(": ")
|
||||
localVariable.type.accept(this)
|
||||
}
|
||||
@@ -401,7 +403,6 @@ class NewCodeBuilder {
|
||||
printer.printWithNoIndent(" = ")
|
||||
localVariable.initializer.accept(this)
|
||||
}
|
||||
printer.printlnWithNoIndent()
|
||||
}
|
||||
|
||||
override fun visitKtConvertedFromForLoopSyntheticWhileStatement(
|
||||
@@ -413,6 +414,7 @@ class NewCodeBuilder {
|
||||
}
|
||||
|
||||
private fun renderType(type: JKType) {
|
||||
if (type is JKNoTypeImpl) return
|
||||
when (type) {
|
||||
is JKClassType -> type.classReference.fqName?.let { printer.printWithNoIndent(FqName(it).shortName().asString()) }
|
||||
is JKUnresolvedClassType -> printer.printWithNoIndent(type.name)
|
||||
|
||||
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.j2k.conversions
|
||||
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.j2k.*
|
||||
import org.jetbrains.kotlin.j2k.ast.Mutability
|
||||
import org.jetbrains.kotlin.j2k.tree.*
|
||||
import org.jetbrains.kotlin.j2k.tree.impl.*
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
@@ -118,8 +119,15 @@ class ForConversion(private val context: ConversionContext) : RecursiveApplicabl
|
||||
// val explicitType = if (context.converter.settings.specifyLocalVariableTypeByDefault)
|
||||
// JKJavaPrimitiveTypeImpl.INT
|
||||
// else null
|
||||
val loopVarDeclaration =
|
||||
JKLocalVariableImpl(
|
||||
JKModifierListImpl(JKModalityModifierImpl(JKModalityModifier.Modality.FINAL)),
|
||||
JKTypeElementImpl(JKNoTypeImpl),
|
||||
loopVar::name.detached(),
|
||||
JKStubExpressionImpl()
|
||||
)
|
||||
return JKKtForInStatementImpl(
|
||||
loopVar::name.detached(),
|
||||
loopVarDeclaration,
|
||||
range,
|
||||
loopStatement::body.detached()
|
||||
)
|
||||
|
||||
@@ -196,6 +196,10 @@ class JKClassTypeImpl(
|
||||
override val nullability: Nullability = Nullability.Default
|
||||
) : JKClassType
|
||||
|
||||
object JKNoTypeImpl: JKNoType {
|
||||
override val nullability: Nullability = Nullability.NotNull
|
||||
}
|
||||
|
||||
class JKStarProjectionTypeImpl : JKStarProjectionType
|
||||
|
||||
class JKUnresolvedClassType(
|
||||
|
||||
@@ -234,10 +234,9 @@ fun JKClass.getOrCreateInitDeclaration(): JKKtInitDeclaration {
|
||||
return newDeclaration
|
||||
}
|
||||
|
||||
class JKKtForInStatementImpl(variableIdentifier: JKNameIdentifier, iterationExpression: JKExpression, body: JKStatement) :
|
||||
JKKtForInStatement,
|
||||
JKBranchElementBase() {
|
||||
override var variableIdentifier: JKNameIdentifier by child(variableIdentifier)
|
||||
class JKKtForInStatementImpl(declaration: JKDeclaration, iterationExpression: JKExpression, body: JKStatement) :
|
||||
JKKtForInStatement, JKBranchElementBase() {
|
||||
override var declaration: JKDeclaration by child(declaration)
|
||||
override var iterationExpression: JKExpression by child(iterationExpression)
|
||||
override var body: JKStatement by child(body)
|
||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitKtForInStatement(this, data)
|
||||
|
||||
@@ -79,7 +79,7 @@ interface JKKtInitDeclaration : JKDeclaration {
|
||||
}
|
||||
|
||||
interface JKKtForInStatement : JKStatement {
|
||||
var variableIdentifier: JKNameIdentifier
|
||||
var declaration: JKDeclaration
|
||||
var iterationExpression: JKExpression
|
||||
var body: JKStatement
|
||||
}
|
||||
|
||||
@@ -60,6 +60,8 @@ interface JKType {
|
||||
val nullability: Nullability
|
||||
}
|
||||
|
||||
interface JKNoType : JKType
|
||||
|
||||
interface JKParametrizedType : JKType {
|
||||
val parameters: List<JKType>
|
||||
}
|
||||
@@ -108,3 +110,5 @@ fun <T : JKElement> KProperty0<T>.detached(): T =
|
||||
fun <T : JKElement> KProperty0<List<T>>.detached(): List<T> =
|
||||
get().also { list -> list.forEach { detach(it) } }
|
||||
|
||||
fun <T : JKElement> T.detached(from: JKElement): T =
|
||||
also { it.detach(from) }
|
||||
Reference in New Issue
Block a user