181: Uast: parents for UIdentifiers from toUElement
This commit is contained in:
committed by
Nikolay Krasko
parent
4e078fe94b
commit
96ac913a6d
+5
-2
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
|||||||
import org.jetbrains.uast.UElement
|
import org.jetbrains.uast.UElement
|
||||||
import org.jetbrains.uast.UIdentifier
|
import org.jetbrains.uast.UIdentifier
|
||||||
import org.jetbrains.uast.kotlin.unwrapFakeFileForLightClass
|
import org.jetbrains.uast.kotlin.unwrapFakeFileForLightClass
|
||||||
|
import org.jetbrains.uast.toUElement
|
||||||
|
|
||||||
class UastLightIdentifier(lightOwner: PsiNameIdentifierOwner, ktDeclaration: KtNamedDeclaration?) :
|
class UastLightIdentifier(lightOwner: PsiNameIdentifierOwner, ktDeclaration: KtNamedDeclaration?) :
|
||||||
KtLightIdentifier(lightOwner, ktDeclaration) {
|
KtLightIdentifier(lightOwner, ktDeclaration) {
|
||||||
@@ -37,13 +38,15 @@ class KotlinUIdentifier private constructor(
|
|||||||
override val javaPsi: PsiIdentifier?,
|
override val javaPsi: PsiIdentifier?,
|
||||||
override val sourcePsi: PsiElement?,
|
override val sourcePsi: PsiElement?,
|
||||||
override val psi: PsiElement?,
|
override val psi: PsiElement?,
|
||||||
override val uastParent: UElement?
|
givenParent: UElement?
|
||||||
) : UIdentifier(psi, uastParent) {
|
) : UIdentifier(psi, givenParent) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
assert(sourcePsi == null || sourcePsi is LeafPsiElement || sourcePsi is KtElement, { "sourcePsi should be physical" })
|
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(javaPsi: PsiIdentifier?, sourcePsi: PsiElement?, uastParent: UElement?) : this(javaPsi, sourcePsi, javaPsi, uastParent)
|
||||||
constructor(sourcePsi: PsiElement?, uastParent: UElement?) : this(null, sourcePsi, sourcePsi, uastParent)
|
constructor(sourcePsi: PsiElement?, uastParent: UElement?) : this(null, sourcePsi, sourcePsi, uastParent)
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
@@ -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)
|
||||||
@@ -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)
|
||||||
@@ -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")
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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")
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<UIdentifier>()
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user