New J2K: Added list child into JKBranchElement. Added attach and detach method to JKElement
This commit is contained in:
@@ -16,44 +16,59 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.j2k.tree.impl
|
package org.jetbrains.kotlin.j2k.tree.impl
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
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.visitors.JKVisitor
|
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import kotlin.properties.ReadWriteProperty
|
import kotlin.properties.ReadWriteProperty
|
||||||
import kotlin.reflect.KProperty
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
private class JKChild<T : JKTreeElement>(val value: Int) : ReadWriteProperty<JKMutableBranchElement, T> {
|
class JKChild<T : JKElement>(val value: Int) : ReadWriteProperty<JKBranchElementBase, T> {
|
||||||
override operator fun getValue(thisRef: JKMutableBranchElement, property: KProperty<*>): T {
|
override operator fun getValue(thisRef: JKBranchElementBase, property: KProperty<*>): T {
|
||||||
return thisRef.children[value] as T
|
return thisRef.children[value] as T
|
||||||
}
|
}
|
||||||
|
|
||||||
override operator fun setValue(thisRef: JKMutableBranchElement, property: KProperty<*>, value: T) {
|
override operator fun setValue(thisRef: JKBranchElementBase, property: KProperty<*>, value: T) {
|
||||||
thisRef.children[this.value].parent = null
|
(thisRef.children[this.value] as T).detach()
|
||||||
value.parent = thisRef
|
value.attach(thisRef)
|
||||||
thisRef.children[this.value] = value
|
thisRef.children[this.value] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKListChild<T : JKTreeElement>() : ReadWriteProperty<JKMutableBranchElement, List<T>> {
|
class JKListChild<T : JKElement>(val value: Int) : ReadWriteProperty<JKBranchElementBase, List<T>> {
|
||||||
override operator fun getValue(thisRef: JKMutableBranchElement, property: KProperty<*>): List<T> {
|
override operator fun getValue(thisRef: JKBranchElementBase, property: KProperty<*>): List<T> {
|
||||||
return thisRef.children.toList() as List<T>
|
return thisRef.children.toList() as List<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
override operator fun setValue(thisRef: JKMutableBranchElement, property: KProperty<*>, value: List<T>) {
|
override operator fun setValue(thisRef: JKBranchElementBase, property: KProperty<*>, value: List<T>) {
|
||||||
thisRef.children.forEach { it.parent = null }
|
(thisRef.children[this.value] as List<T>).forEach { it.detach() }
|
||||||
value.forEach { it.parent = thisRef }
|
value.forEach { it.attach(thisRef) }
|
||||||
thisRef.children.clear()
|
thisRef.children[this.value] = value
|
||||||
thisRef.children.addAll(value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class JKElementBase : JKTreeElement {
|
abstract class JKElementBase : JKTreeElement {
|
||||||
override var parent: JKElement? = null
|
override var parent: JKElement? = null
|
||||||
|
|
||||||
fun <D : JKTreeElement> D.setParent(p: JKTreeElement): D {
|
final override fun detach() {
|
||||||
parent = p
|
val prevParent = parent
|
||||||
return this
|
assert(prevParent != null)
|
||||||
|
parent = null
|
||||||
|
onDetach(prevParent!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun onDetach(from: JKElement) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
final override fun attach(to: JKElement) {
|
||||||
|
assert(parent == null)
|
||||||
|
parent = to
|
||||||
|
onAttach()
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun onAttach() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitElement(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitElement(this, data)
|
||||||
@@ -61,46 +76,35 @@ abstract class JKElementBase : JKTreeElement {
|
|||||||
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {}
|
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKMutableBranchElement : JKBranchElement {
|
abstract class JKBranchElementBase : JKElementBase(), JKBranchElement {
|
||||||
override val children: MutableList<JKTreeElement>
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class JKBranchElementBase : JKElementBase(), JKMutableBranchElement {
|
|
||||||
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
|
|
||||||
children.forEach { it.accept(visitor, data) }
|
|
||||||
}
|
|
||||||
|
|
||||||
protected var childNum = 0
|
protected var childNum = 0
|
||||||
protected fun <T : JKTreeElement, U : T> child(v: U): ReadWriteProperty<JKMutableBranchElement, T> {
|
protected fun <T : JKTreeElement, U : T> child(v: U): ReadWriteProperty<JKBranchElementBase, T> {
|
||||||
children.add(childNum, v)
|
children.add(childNum, v)
|
||||||
return JKChild(childNum++)
|
return JKChild(childNum++)
|
||||||
}
|
}
|
||||||
|
|
||||||
override val children: MutableList<JKTreeElement> = mutableListOf()
|
protected inline fun <reified T : JKTreeElement> child(): ReadWriteProperty<JKBranchElementBase, List<T>> {
|
||||||
}
|
children[childNum] = listOf<T>()
|
||||||
|
return JKListChild(childNum++)
|
||||||
abstract class JKElementListBase: JKElementBase(), JKMutableBranchElement {
|
|
||||||
override val children: MutableList<JKTreeElement> = mutableListOf()
|
|
||||||
|
|
||||||
protected inline fun <reified T : JKTreeElement> children(): JKListChild<T> {
|
|
||||||
return JKListChild()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected inline fun <reified T : JKTreeElement> children(v: List<T>): JKListChild<T> {
|
protected inline fun <reified T : JKTreeElement> child(v: List<T>): ReadWriteProperty<JKBranchElementBase, List<T>> {
|
||||||
children.addAll(v)
|
children[childNum] = v
|
||||||
return JKListChild()
|
return JKListChild(childNum++)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
|
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
|
||||||
children.forEach { it.accept(visitor, data) }
|
children.forEach {
|
||||||
|
if (it is JKTreeElement)
|
||||||
|
it.accept(visitor, data)
|
||||||
|
else
|
||||||
|
(it as? List<JKTreeElement>)?.forEach { it.accept(visitor, data) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class JKUDeclarationListImpl : JKElementListBase(), JKUDeclarationList {
|
override val children: MutableList<Any> = mutableListOf()
|
||||||
override var declarations: List<JKUniverseDeclaration> by children()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override var classKind: JKClass.ClassKind) :
|
class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override var classKind: JKClass.ClassKind) :
|
||||||
JKUniverseClass,
|
JKUniverseClass,
|
||||||
JKBranchElementBase() {
|
JKBranchElementBase() {
|
||||||
@@ -109,7 +113,7 @@ class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override
|
|||||||
override var modifierList by child(modifierList)
|
override var modifierList by child(modifierList)
|
||||||
override val valid: Boolean = true
|
override val valid: Boolean = true
|
||||||
|
|
||||||
override val declarationList by child(JKUDeclarationListImpl())
|
override var declarationList by child<JKUniverseDeclaration>()
|
||||||
|
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitUniverseClass(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitUniverseClass(this, data)
|
||||||
}
|
}
|
||||||
@@ -117,8 +121,8 @@ class JKClassImpl(modifierList: JKModifierList, name: JKNameIdentifier, override
|
|||||||
|
|
||||||
class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKElementBase() {}
|
class JKNameIdentifierImpl(override val value: String) : JKNameIdentifier, JKElementBase() {}
|
||||||
|
|
||||||
class JKModifierListImpl(modifiers: List<JKModifier> = emptyList()) : JKModifierList, JKElementListBase() {
|
class JKModifierListImpl(modifiers: List<JKModifier> = emptyList()) : JKModifierList, JKBranchElementBase() {
|
||||||
override var modifiers: List<JKModifier> by children(modifiers)
|
override var modifiers: List<JKModifier> by child(modifiers)
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitModifierList(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitModifierList(this, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,8 +131,8 @@ class JKValueArgumentImpl(type: JKType, override val name: String) : JKValueArgu
|
|||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitValueArgument(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitValueArgument(this, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKBlockImpl(statements: List<JKStatement> = emptyList()) : JKBlock, JKElementListBase() {
|
class JKBlockImpl(statements: List<JKStatement> = emptyList()) : JKBlock, JKBranchElementBase() {
|
||||||
override var statements by children(statements)
|
override var statements by child(statements)
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitBlock(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitBlock(this, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,8 +154,8 @@ class JKPostfixExpressionImpl(expression: JKExpression, override var operator: J
|
|||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitPostfixExpression(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitPostfixExpression(this, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKExpressionListImpl(expressions: List<JKExpression> = emptyList()) : JKExpressionList, JKElementListBase() {
|
class JKExpressionListImpl(expressions: List<JKExpression> = emptyList()) : JKExpressionList, JKBranchElementBase() {
|
||||||
override var expressions by children(expressions)
|
override var expressions by child(expressions)
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitExpressionList(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitExpressionList(this, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +191,8 @@ class JKParenthesizedExpressionImpl(override var expression: JKExpression) : JKP
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKTypeCastExpressionImpl(override var expression: JKExpression, override var type: JKType) : JKTypeCastExpression, JKBranchElementBase() {
|
class JKTypeCastExpressionImpl(override var expression: JKExpression, override var type: JKType) : JKTypeCastExpression,
|
||||||
|
JKBranchElementBase() {
|
||||||
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitTypeCastExpression(this, data)
|
override fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R = visitor.visitTypeCastExpression(this, data)
|
||||||
|
|
||||||
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
|
override fun <D> acceptChildren(visitor: JKVisitor<Unit, D>, data: D) {
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin.j2k.tree
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.j2k.ast.Nullability
|
import org.jetbrains.kotlin.j2k.ast.Nullability
|
||||||
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
|
import org.jetbrains.kotlin.j2k.tree.visitors.JKVisitor
|
||||||
|
import kotlin.properties.ReadWriteProperty
|
||||||
|
import kotlin.reflect.KProperty
|
||||||
|
|
||||||
interface JKTreeElement : JKElement {
|
interface JKTreeElement : JKElement {
|
||||||
fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R
|
fun <R, D> accept(visitor: JKVisitor<R, D>, data: D): R
|
||||||
@@ -36,7 +38,7 @@ interface JKUDeclarationList : JKDeclarationList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface JKUniverseClass : JKUniverseDeclaration, JKClass {
|
interface JKUniverseClass : JKUniverseDeclaration, JKClass {
|
||||||
override val declarationList: JKUDeclarationList
|
override var declarationList: List<JKUniverseDeclaration>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKModifier : JKTreeElement
|
interface JKModifier : JKTreeElement
|
||||||
|
|||||||
@@ -16,16 +16,20 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.j2k.tree
|
package org.jetbrains.kotlin.j2k.tree
|
||||||
|
|
||||||
interface JKOperator: JKTreeElement
|
interface JKOperator : JKTreeElement
|
||||||
|
|
||||||
interface JKQualifier
|
interface JKQualifier
|
||||||
|
|
||||||
interface JKElement {
|
interface JKElement {
|
||||||
var parent: JKElement?
|
val parent: JKElement?
|
||||||
|
|
||||||
|
fun detach()
|
||||||
|
|
||||||
|
fun attach(to: JKElement)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKBranchElement : JKElement {
|
interface JKBranchElement : JKElement {
|
||||||
val children: List<JKTreeElement>
|
val children: List<Any>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface JKModifierListOwner {
|
interface JKModifierListOwner {
|
||||||
@@ -40,7 +44,7 @@ interface JKDeclaration : JKElement, JKReferenceTarget
|
|||||||
|
|
||||||
interface JKClass : JKDeclaration, JKModifierListOwner {
|
interface JKClass : JKDeclaration, JKModifierListOwner {
|
||||||
val name: JKNameIdentifier
|
val name: JKNameIdentifier
|
||||||
val declarationList: JKDeclarationList
|
val declarationList: List<JKDeclaration>
|
||||||
var classKind: ClassKind
|
var classKind: ClassKind
|
||||||
|
|
||||||
enum class ClassKind {
|
enum class ClassKind {
|
||||||
|
|||||||
@@ -15,3 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.j2k.tree.multiverse
|
package org.jetbrains.kotlin.j2k.tree.multiverse
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.j2k.tree.JKDeclaration
|
||||||
|
|
||||||
|
interface JKMultiverseDeclaration : JKDeclaration
|
||||||
@@ -29,14 +29,14 @@ abstract class JKMultiverseElementBase : JKElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKMultiverseField(override var name: JKNameIdentifier) : JKField, JKMultiverseElementBase() {
|
class JKMultiverseField(override var name: JKNameIdentifier) : JKField, JKMultiverseDeclaration, JKMultiverseElementBase() {
|
||||||
override var modifierList: JKModifierList = JKModifierListImpl()
|
override var modifierList: JKModifierList = JKModifierListImpl()
|
||||||
override var type: JKType = JKJavaPrimitiveTypeImpl.BOOLEAN //TODO
|
override var type: JKType = JKJavaPrimitiveTypeImpl.BOOLEAN //TODO
|
||||||
override val valid: Boolean
|
override val valid: Boolean
|
||||||
get() = true
|
get() = true
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMultiverseElementBase() {
|
class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMultiverseDeclaration, JKMultiverseElementBase() {
|
||||||
override val returnType: JKType
|
override val returnType: JKType
|
||||||
get() = TODO("not implemented")
|
get() = TODO("not implemented")
|
||||||
override var modifierList: JKModifierList = JKModifierListImpl()
|
override var modifierList: JKModifierList = JKModifierListImpl()
|
||||||
@@ -46,11 +46,9 @@ class JKMultiverseMethod(override var name: JKNameIdentifier) : JKMethod, JKMult
|
|||||||
}
|
}
|
||||||
|
|
||||||
class JKMultiverseClass(
|
class JKMultiverseClass(
|
||||||
override var name: JKNameIdentifier, override val declarationList: JKMDeclarationList,
|
override var name: JKNameIdentifier, override val declarationList: List<JKMultiverseDeclaration>,
|
||||||
override var classKind: JKClass.ClassKind, override var modifierList: JKModifierList
|
override var classKind: JKClass.ClassKind, override var modifierList: JKModifierList
|
||||||
) : JKClass, JKMultiverseElementBase() {
|
) : JKClass, JKMultiverseElementBase() {
|
||||||
override val valid: Boolean
|
override val valid: Boolean
|
||||||
get() = true
|
get() = true
|
||||||
}
|
}
|
||||||
|
|
||||||
class JKMDeclarationList(override val declarations: List<JKDeclaration>) : JKMultiverseElementBase(), JKDeclarationList
|
|
||||||
Reference in New Issue
Block a user