Uast: KotlinUNestedAnnotation for processing nested annotations (IDEA-185890)

This commit is contained in:
Nicolay Mitropolsky
2018-02-01 13:13:05 +03:00
committed by xiexed
parent 1f81c0cdfe
commit 48ea52def1
8 changed files with 165 additions and 21 deletions
@@ -275,6 +275,23 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
}
}
@Test
fun testNestedAnnotation() = doTest("AnnotationComplex") { _, file ->
file.findElementByTextFromPsi<UElement>("@AnnotationArray(value = Annotation())")
.findElementByTextFromPsi<UElement>("Annotation()")
.sourcePsiElement
.let { referenceExpression ->
val convertedUAnnotation = referenceExpression
.cast<KtReferenceExpression>()
.toUElementOfType<UAnnotation>()
?: throw AssertionError("haven't got annotation from $referenceExpression(${referenceExpression?.javaClass})")
assertEquals("Annotation", convertedUAnnotation.qualifiedName)
}
}
}
fun <T, R> Iterable<T>.assertedFind(value: R, transform: (T) -> R): T = find { transform(it) == value } ?: throw AssertionError("'$value' not found, only ${this.joinToString { transform(it).toString() }}")
@@ -72,4 +72,7 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
@Test
fun testAnonymous() = doTest("Anonymous")
@Test
fun testAnnotationComplex() = doTest("AnnotationComplex")
}
@@ -73,7 +73,8 @@ inline fun <reified T : UElement> PsiElement.findUElementByTextFromPsi(refText:
?: throw AssertionError("requested text '$refText' was not found in $this")
val uElementContainingText = elementAtStart.parentsWithSelf.let {
if (strict) it.dropWhile { !it.text.contains(refText) } else it
}.mapNotNull { it.toUElementOfType<T>() }.first()
}.mapNotNull { it.toUElementOfType<T>() }.firstOrNull()
?: throw AssertionError("requested text '$refText' not found as '${T::class.java.canonicalName}' in $this")
if (strict && uElementContainingText.psi != null && uElementContainingText.psi?.text != refText) {
throw AssertionError("requested text '$refText' found as '${uElementContainingText.psi?.text}' in $uElementContainingText")
}