From 96ac913a6d47d63a12333b3136b2df5937122cf7 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Wed, 17 Jan 2018 17:46:08 +0300 Subject: [PATCH] 181: Uast: parents for `UIdentifier`s from `toUElement` --- .../declarations/UastLightIdentifier.kt.181 | 7 ++- .../ClassAnnotation.identifiers.txt.181 | 12 +++++ .../testData/Constructors.identifiers.txt.181 | 52 +++++++++++++++++++ .../LocalDeclarations.identifiers.txt.181 | 13 +++++ .../AbstractKotlinIdentifiersTest.kt.181 | 15 ++++++ .../tests/KotlinUastIdentifiersTest.kt.181 | 22 ++++++++ .../test/common/IdentifiersTestBase.kt.181 | 49 +++++++++++++++++ 7 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 plugins/uast-kotlin/testData/ClassAnnotation.identifiers.txt.181 create mode 100644 plugins/uast-kotlin/testData/Constructors.identifiers.txt.181 create mode 100644 plugins/uast-kotlin/testData/LocalDeclarations.identifiers.txt.181 create mode 100644 plugins/uast-kotlin/tests/AbstractKotlinIdentifiersTest.kt.181 create mode 100644 plugins/uast-kotlin/tests/KotlinUastIdentifiersTest.kt.181 create mode 100644 plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/IdentifiersTestBase.kt.181 diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/UastLightIdentifier.kt.181 b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/UastLightIdentifier.kt.181 index 110419ffc51..b8e1f391445 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/UastLightIdentifier.kt.181 +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/UastLightIdentifier.kt.181 @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration import org.jetbrains.uast.UElement import org.jetbrains.uast.UIdentifier import org.jetbrains.uast.kotlin.unwrapFakeFileForLightClass +import org.jetbrains.uast.toUElement class UastLightIdentifier(lightOwner: PsiNameIdentifierOwner, ktDeclaration: KtNamedDeclaration?) : KtLightIdentifier(lightOwner, ktDeclaration) { @@ -37,13 +38,15 @@ class KotlinUIdentifier private constructor( override val javaPsi: PsiIdentifier?, override val sourcePsi: PsiElement?, override val psi: PsiElement?, - override val uastParent: UElement? -) : UIdentifier(psi, uastParent) { + givenParent: UElement? +) : UIdentifier(psi, givenParent) { init { assert(sourcePsi == null || sourcePsi is LeafPsiElement || sourcePsi is KtElement, { "sourcePsi should be physical" }) } + override val uastParent: UElement? by lazy { givenParent ?: sourcePsi?.parent?.toUElement() } + constructor(javaPsi: PsiIdentifier?, sourcePsi: PsiElement?, uastParent: UElement?) : this(javaPsi, sourcePsi, javaPsi, uastParent) constructor(sourcePsi: PsiElement?, uastParent: UElement?) : this(null, sourcePsi, sourcePsi, uastParent) } \ No newline at end of file diff --git a/plugins/uast-kotlin/testData/ClassAnnotation.identifiers.txt.181 b/plugins/uast-kotlin/testData/ClassAnnotation.identifiers.txt.181 new file mode 100644 index 00000000000..5ce47e5e597 --- /dev/null +++ b/plugins/uast-kotlin/testData/ClassAnnotation.identifiers.txt.181 @@ -0,0 +1,12 @@ +Test -> USimpleNameReferenceExpression (identifier = Test) +A -> UClass (name = A) +MyAnnotation -> UClass (name = MyAnnotation) +text -> [!] UnknownKotlinExpression (VALUE_PARAMETER) +String -> USimpleNameReferenceExpression (identifier = String) +MyAnnotation -> USimpleNameReferenceExpression (identifier = MyAnnotation) +B -> UClass (name = B) +MyAnnotation -> USimpleNameReferenceExpression (identifier = MyAnnotation) +InB -> UClass (name = InB) +MyAnnotation -> USimpleNameReferenceExpression (identifier = MyAnnotation) +MyAnnotation -> USimpleNameReferenceExpression (identifier = MyAnnotation) +Obj -> UClass (name = Obj) diff --git a/plugins/uast-kotlin/testData/Constructors.identifiers.txt.181 b/plugins/uast-kotlin/testData/Constructors.identifiers.txt.181 new file mode 100644 index 00000000000..eb7c73b64f5 --- /dev/null +++ b/plugins/uast-kotlin/testData/Constructors.identifiers.txt.181 @@ -0,0 +1,52 @@ +A -> UClass (name = A) +str -> UParameter (name = str) +String -> USimpleNameReferenceExpression (identifier = String) +i -> UParameter (name = i) +Int -> USimpleNameReferenceExpression (identifier = Int) +i -> USimpleNameReferenceExpression (identifier = i) +toString -> USimpleNameReferenceExpression (identifier = toString) +AWithInit -> UClass (name = AWithInit) +str -> UParameter (name = str) +String -> USimpleNameReferenceExpression (identifier = String) + println -> USimpleNameReferenceExpression (identifier = println) +i -> UParameter (name = i) +Int -> USimpleNameReferenceExpression (identifier = Int) +i -> USimpleNameReferenceExpression (identifier = i) +toString -> USimpleNameReferenceExpression (identifier = toString) +AWith2Init -> UClass (name = AWith2Init) +str -> UParameter (name = str) +String -> USimpleNameReferenceExpression (identifier = String) + println -> USimpleNameReferenceExpression (identifier = println) + println -> USimpleNameReferenceExpression (identifier = println) +i -> UParameter (name = i) +Int -> USimpleNameReferenceExpression (identifier = Int) +i -> USimpleNameReferenceExpression (identifier = i) +toString -> USimpleNameReferenceExpression (identifier = toString) +AOnlyInit -> UClass (name = AOnlyInit) + println -> USimpleNameReferenceExpression (identifier = println) + println -> USimpleNameReferenceExpression (identifier = println) +AWithSecondary -> UClass (name = AWithSecondary) +a -> UField (name = a) +String -> USimpleNameReferenceExpression (identifier = String) +i -> UParameter (name = i) +Int -> USimpleNameReferenceExpression (identifier = Int) + a -> USimpleNameReferenceExpression (identifier = a) + i -> USimpleNameReferenceExpression (identifier = i) + toString -> USimpleNameReferenceExpression (identifier = toString) +s -> UParameter (name = s) +String -> USimpleNameReferenceExpression (identifier = String) + a -> USimpleNameReferenceExpression (identifier = a) + s -> USimpleNameReferenceExpression (identifier = s) +AWithSecondaryInit -> UClass (name = AWithSecondaryInit) +a -> UField (name = a) +String -> USimpleNameReferenceExpression (identifier = String) + println -> USimpleNameReferenceExpression (identifier = println) +i -> UParameter (name = i) +Int -> USimpleNameReferenceExpression (identifier = Int) + a -> USimpleNameReferenceExpression (identifier = a) + i -> USimpleNameReferenceExpression (identifier = i) + toString -> USimpleNameReferenceExpression (identifier = toString) +s -> UParameter (name = s) +String -> USimpleNameReferenceExpression (identifier = String) + a -> USimpleNameReferenceExpression (identifier = a) + s -> USimpleNameReferenceExpression (identifier = s) diff --git a/plugins/uast-kotlin/testData/LocalDeclarations.identifiers.txt.181 b/plugins/uast-kotlin/testData/LocalDeclarations.identifiers.txt.181 new file mode 100644 index 00000000000..6545b4eb469 --- /dev/null +++ b/plugins/uast-kotlin/testData/LocalDeclarations.identifiers.txt.181 @@ -0,0 +1,13 @@ +foo -> UAnnotationMethod (name = foo) +Boolean -> USimpleNameReferenceExpression (identifier = Boolean) + Local -> UClass (name = Local) + bar -> ULambdaExpression + Local -> USimpleNameReferenceExpression (identifier = Local) + baz -> ULocalVariable (name = baz) + Local -> USimpleNameReferenceExpression (identifier = Local) + Int -> USimpleNameReferenceExpression (identifier = Int) + someLocalFun -> ULambdaExpression + text -> [!] UnknownKotlinExpression (VALUE_PARAMETER) + String -> USimpleNameReferenceExpression (identifier = String) + bar -> USimpleNameReferenceExpression (identifier = bar) + Local -> USimpleNameReferenceExpression (identifier = Local) diff --git a/plugins/uast-kotlin/tests/AbstractKotlinIdentifiersTest.kt.181 b/plugins/uast-kotlin/tests/AbstractKotlinIdentifiersTest.kt.181 new file mode 100644 index 00000000000..3809ff99f28 --- /dev/null +++ b/plugins/uast-kotlin/tests/AbstractKotlinIdentifiersTest.kt.181 @@ -0,0 +1,15 @@ +package org.jetbrains.uast.test.kotlin + +import org.jetbrains.uast.test.common.IdentifiersTestBase +import java.io.File + + +abstract class AbstractKotlinIdentifiersTest : AbstractKotlinUastTest(), IdentifiersTestBase { + + private fun getTestFile(testName: String, ext: String) = + File(File(AbstractKotlinUastTest.TEST_KOTLIN_MODEL_DIR, testName).canonicalPath + '.' + ext) + + override fun getIdentifiersFile(testName: String): File = getTestFile(testName, "identifiers.txt") + + +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/KotlinUastIdentifiersTest.kt.181 b/plugins/uast-kotlin/tests/KotlinUastIdentifiersTest.kt.181 new file mode 100644 index 00000000000..3330242a36b --- /dev/null +++ b/plugins/uast-kotlin/tests/KotlinUastIdentifiersTest.kt.181 @@ -0,0 +1,22 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.uast.test.kotlin + +import org.junit.Test + +class KotlinUastIdentifiersTest : AbstractKotlinIdentifiersTest() { + + @Test + fun testClassAnnotation() = doTest("ClassAnnotation") + + @Test + fun testLocalDeclarations() = doTest("LocalDeclarations") + + @Test + fun testConstructors() = doTest("Constructors") + + +} \ No newline at end of file diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/IdentifiersTestBase.kt.181 b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/IdentifiersTestBase.kt.181 new file mode 100644 index 00000000000..8349f1a5f55 --- /dev/null +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/common/IdentifiersTestBase.kt.181 @@ -0,0 +1,49 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.uast.test.common + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiElementVisitor +import org.jetbrains.kotlin.psi.KtBlockExpression +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.uast.UFile +import org.jetbrains.uast.UIdentifier +import org.jetbrains.uast.sourcePsiElement +import org.jetbrains.uast.test.env.assertEqualsToFile +import org.jetbrains.uast.toUElementOfType +import java.io.File + +interface IdentifiersTestBase { + fun getIdentifiersFile(testName: String): File + + private fun UFile.asIdentifiersWithParents(): String { + val builder = StringBuilder() + var level = 0 + (this.psi as KtFile).accept(object : PsiElementVisitor() { + override fun visitElement(element: PsiElement) { + val uIdentifier = element.toUElementOfType() + if (uIdentifier != null) { + builder.append(" ".repeat(level)) + builder.append(uIdentifier.sourcePsiElement!!.text) + builder.append(" -> ") + builder.append(uIdentifier.uastParent?.asLogString()) + builder.appendln() + } + if (element is KtBlockExpression) level++ + element.acceptChildren(this) + if (element is KtBlockExpression) level-- + } + }) + return builder.toString() + } + + fun check(testName: String, file: UFile) { + val valuesFile = getIdentifiersFile(testName) + + assertEqualsToFile("Identifiers", valuesFile, file.asIdentifiersWithParents()) + } + +}