Uast: Support Kotlin anonymous initializers

This commit is contained in:
Yan Zhulanow
2016-03-10 18:14:39 +03:00
parent 335712141d
commit f1e5c85ba3
7 changed files with 98 additions and 6 deletions
@@ -47,7 +47,7 @@ interface UFunction : UDeclaration, UModifierOwner, UAnnotated {
return "${visibility.name} fun " + typeParameters + name + "(" + valueParameters + ")" + returnType + body
}
override fun logString() = "UFunction ($name, kind = ${kind.name}, " +
override fun logString() = "UFunction ($name, kind = ${kind.text}, " +
"paramCount = $valueParameterCount)\n" + body.logString().withMargin
}
@@ -15,15 +15,14 @@
*/
package org.jetbrains.uast
class UastFunctionKind(val name: String) {
open class UastFunctionKind(val text: String) {
class UastInitializerKind(val name: String) : UastFunctionKind("INITIALIZER ($name)")
companion object {
@JvmField
val FUNCTION = UastFunctionKind("function")
@JvmField
val CONSTRUCTOR = UastFunctionKind("CONSTRUCTOR")
@JvmField
val INITIALIZER = UastFunctionKind("INITIALIZER")
}
}
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.java
import org.jetbrains.uast.UastFunctionKind
object JavaFunctionKinds {
val STATIC_INITIALIZER = UastFunctionKind.UastInitializerKind("<static>")
}
@@ -71,6 +71,7 @@ internal object KotlinConverter : UastConverter {
internal fun convert(element: KtDeclaration, parent: UElement): UDeclaration? = when (element) {
is KtClassOrObject -> KotlinUClass(element, parent)
is KtAnonymousInitializer -> KotlinAnonymousInitializerUFunction(element, parent)
is KtConstructor<*> -> KotlinConstructorUFunction(element, parent)
is KtFunction -> KotlinUFunction(element, parent)
is KtVariableDeclaration -> KotlinUVariable(element, parent)
@@ -79,8 +79,13 @@ class KotlinUClass(
override val declarations by lz {
val primaryConstructor = psi.getPrimaryConstructor()?.let { KotlinConverter.convert(it, this) }
val anonymousInitializers = psi.getAnonymousInitializers().map { KotlinConverter.convert(it, this) }.filterNotNull()
val declarations = psi.declarations.map { KotlinConverter.convert(it, this) }.filterNotNull()
if (primaryConstructor != null) listOf(primaryConstructor) + declarations else declarations
if (primaryConstructor != null)
listOf(primaryConstructor) + declarations + anonymousInitializers
else
declarations + anonymousInitializers
}
override val superTypes by lz {
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtAnonymousInitializer
import org.jetbrains.kotlin.psi.KtConstructor
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.resolve.BindingContext
@@ -87,4 +89,43 @@ class KotlinUFunction(
get() = psi.typeParameters.size
override val typeParameters by lz { psi.typeParameters.map { KotlinParameterUTypeReference(it, this) } }
}
class KotlinAnonymousInitializerUFunction(
override val psi: KtAnonymousInitializer,
override val parent: UElement
) : UFunction, PsiElementBacked {
override val kind = KotlinFunctionKinds.INIT_BLOCK
override val valueParameters: List<UVariable>
get() = emptyList()
override val valueParameterCount: Int
get() = 0
override val typeParameters: List<UTypeReference>
get() = emptyList()
override val typeParameterCount: Int
get() = 0
override val returnType: UType?
get() = null
override val body by lz { KotlinConverter.convertOrEmpty(psi.body, this) }
override val visibility: UastVisibility
get() = UastVisibility.PRIVATE
override fun getSuperFunctions(context: UastContext) = emptyList<UFunction>()
override val nameElement by lz { KotlinPsiElementStub(psi.node.findChildByType(KtTokens.INIT_KEYWORD)?.psi ?: psi, this) }
override val name: String
get() = "<init>"
override fun hasModifier(modifier: UastModifier) = false
override val annotations: List<UAnnotation>
get() = emptyList()
}
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.uast
import org.jetbrains.uast.UastFunctionKind
object KotlinFunctionKinds {
val INIT_BLOCK = UastFunctionKind.UastInitializerKind("<init>")
}