From f1e5c85ba35bfb328488a224f81639c880fc3c1a Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 10 Mar 2016 18:14:39 +0300 Subject: [PATCH] Uast: Support Kotlin anonymous initializers --- .../jetbrains/uast/declarations/UFunction.kt | 2 +- .../jetbrains/uast/kinds/UastFunctionKind.kt | 7 ++-- .../uast/java/kinds/JavaFunctionKinds.kt | 23 +++++++++++ .../kotlin/uast/KotlinUastLanguagePlugin.kt | 1 + .../kotlin/uast/declarations/KotlinUClass.kt | 7 +++- .../uast/declarations/kotlinUFunctions.kt | 41 +++++++++++++++++++ .../kotlin/uast/kinds/KotlinFunctionKinds.kt | 23 +++++++++++ 7 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 plugins/uast-java/src/org/jetbrains/uast/java/kinds/JavaFunctionKinds.kt create mode 100644 plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/kinds/KotlinFunctionKinds.kt diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt index 41be03ca3dc..3ba6f805dd0 100644 --- a/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt @@ -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 } diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt index b18285fa62c..b589e576892 100644 --- a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt @@ -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") } } \ No newline at end of file diff --git a/plugins/uast-java/src/org/jetbrains/uast/java/kinds/JavaFunctionKinds.kt b/plugins/uast-java/src/org/jetbrains/uast/java/kinds/JavaFunctionKinds.kt new file mode 100644 index 00000000000..0ac3da8526a --- /dev/null +++ b/plugins/uast-java/src/org/jetbrains/uast/java/kinds/JavaFunctionKinds.kt @@ -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("") +} \ No newline at end of file diff --git a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/KotlinUastLanguagePlugin.kt b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/KotlinUastLanguagePlugin.kt index 60d9d784ef6..a26b5b99736 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/KotlinUastLanguagePlugin.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/KotlinUastLanguagePlugin.kt @@ -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) diff --git a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/KotlinUClass.kt b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/KotlinUClass.kt index 1dc7f9fb40e..0bc956f96b3 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/KotlinUClass.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/KotlinUClass.kt @@ -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 { diff --git a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/kotlinUFunctions.kt b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/kotlinUFunctions.kt index 5463d58bdff..476eed687ef 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/kotlinUFunctions.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/declarations/kotlinUFunctions.kt @@ -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 + get() = emptyList() + + override val valueParameterCount: Int + get() = 0 + + override val typeParameters: List + 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() + + override val nameElement by lz { KotlinPsiElementStub(psi.node.findChildByType(KtTokens.INIT_KEYWORD)?.psi ?: psi, this) } + + override val name: String + get() = "" + + override fun hasModifier(modifier: UastModifier) = false + + override val annotations: List + get() = emptyList() } \ No newline at end of file diff --git a/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/kinds/KotlinFunctionKinds.kt b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/kinds/KotlinFunctionKinds.kt new file mode 100644 index 00000000000..8898440f709 --- /dev/null +++ b/plugins/uast-kotlin/src/org/jetbrains/kotlin/uast/kinds/KotlinFunctionKinds.kt @@ -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("") +} \ No newline at end of file