UastKotlinPsiVariable psiParent made lazy to eliminate recursion when building UAST parents

This commit is contained in:
nickl
2017-10-20 16:48:09 +03:00
committed by xiexed
parent b2e53644a5
commit 6a659f33b5
6 changed files with 138 additions and 48 deletions
+25 -7
View File
@@ -131,23 +131,19 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
fun testWhenStringLiteral() {
doTest("WhenStringLiteral") { _, file ->
fun UFile.findULiteralExpressionFromPsi(refText: String): ULiteralExpression =
this.psi.findElementAt(file.psi.text.indexOf(refText))!!
.parentsWithSelf.mapNotNull { it.toUElementOfType<ULiteralExpression>() }.first()
file.findULiteralExpressionFromPsi("abc").let { literalExpression ->
file.findFromPsi<ULiteralExpression>("abc").let { literalExpression ->
val psi = literalExpression.psi!!
Assert.assertTrue(psi is KtLiteralStringTemplateEntry)
UsefulTestCase.assertInstanceOf(literalExpression.uastParent, USwitchClauseExpressionWithBody::class.java)
}
file.findULiteralExpressionFromPsi("def").let { literalExpression ->
file.findFromPsi<ULiteralExpression>("def").let { literalExpression ->
val psi = literalExpression.psi!!
Assert.assertTrue(psi is KtLiteralStringTemplateEntry)
UsefulTestCase.assertInstanceOf(literalExpression.uastParent, USwitchClauseExpressionWithBody::class.java)
}
file.findULiteralExpressionFromPsi("def1").let { literalExpression ->
file.findFromPsi<ULiteralExpression>("def1").let { literalExpression ->
val psi = literalExpression.psi!!
Assert.assertTrue(psi is KtLiteralStringTemplateEntry)
UsefulTestCase.assertInstanceOf(literalExpression.uastParent, UBlockExpression::class.java)
@@ -156,4 +152,26 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
}
}
@Test
fun testWhenAndDestructing() {
doTest("WhenAndDestructing") { _, file ->
file.findFromPsi<UExpression>("val (bindingContext, statementFilter) = arr").let { e ->
val uBlockExpression = e.getParentOfType<UBlockExpression>()
Assert.assertNotNull(uBlockExpression)
val uMethod = uBlockExpression!!.getParentOfType<UMethod>()
Assert.assertNotNull(uMethod)
}
}
}
}
inline fun <reified T : UElement> UFile.findFromPsi(refText: String): T =
(this.psi.findElementAt(this.psi.text.indexOf(refText)) ?: throw AssertionError("requested text '$refText' was not found in ${this.psi.name}"))
.parentsWithSelf.dropWhile { !it.text.contains(refText) }.mapNotNull { it.toUElementOfType<T>() }.first().also {
if (it.psi != null && it.psi?.text != refText) throw AssertionError("requested text '$refText' found as '${it.psi?.text}' in $it")
}
@@ -48,4 +48,7 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() {
@Test fun testUnexpectedContainer() = doTest("UnexpectedContainerException") { testName, file -> check(testName, file, false) }
@Test fun testWhenStringLiteral() = doTest("WhenStringLiteral") { testName, file -> check(testName, file, false) }
@Test
fun testWhenAndDestructing() = doTest("WhenAndDestructing") { testName, file -> check(testName, file, false) }
}