Work in progress on consistent UAST structure

This commit is contained in:
Dmitry Jemerov
2017-09-13 17:21:27 +02:00
parent a5a5b37e1e
commit c98c3a7033
19 changed files with 292 additions and 150 deletions
@@ -1,12 +1,14 @@
package org.jetbrains.uast.test.kotlin
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiRecursiveElementVisitor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.utils.addToStdlib.assertedCast
import org.jetbrains.uast.UDeclaration
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UFile
import org.jetbrains.uast.kotlin.KOTLIN_CACHED_UELEMENT_KEY
import org.jetbrains.uast.kotlin.KotlinUastLanguagePlugin
import org.jetbrains.uast.test.common.RenderLogTestBase
import org.jetbrains.uast.visitor.UastVisitor
@@ -21,12 +23,7 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
override fun check(testName: String, file: UFile) {
super.check(testName, file)
file.psi.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
KotlinUastLanguagePlugin().convertElementWithParent(element, null)
super.visitElement(element)
}
})
val parentMap = mutableMapOf<PsiElement, String>()
file.accept(object : UastVisitor {
private val parentStack = Stack<UElement>()
@@ -39,6 +36,11 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
else {
Assert.assertEquals("Wrong parent of $node", parentStack.peek(), parent)
}
node.psi?.let {
if (it !in parentMap) {
parentMap[it] = parentStack.reversed().joinToString { it.asLogString() }
}
}
parentStack.push(node)
return false
}
@@ -48,6 +50,22 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
parentStack.pop()
}
})
file.psi.clearUastCaches()
file.psi.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
val uElement = KotlinUastLanguagePlugin().convertElementWithParent(element, null)
val expectedParents = parentMap[element]
if (expectedParents != null) {
assertNotNull("Expected to be able to convert PSI element $element", uElement)
val parents = generateSequence(uElement!!.uastParent) { it.uastParent }.joinToString { it.asLogString() }
assertEquals("Inconsistent parents for $uElement", expectedParents, parents)
}
super.visitElement(element)
}
})
file.checkContainingFileForAllElements()
}
@@ -68,3 +86,12 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
})
}
}
private fun PsiFile.clearUastCaches() {
accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
super.visitElement(element)
element.putUserData(KOTLIN_CACHED_UELEMENT_KEY, null)
}
})
}