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