toUElement() made work with KtAnnotationEntry (#1240)

* `KotlinUastLanguagePlugin#convertElement` made work with `KtAnnotationEntry`

* `KotlinUastLanguagePlugin#convertElementWithParent` searches for parent `KotlinUNamedExpression`
This commit is contained in:
xiexed
2017-08-16 13:01:37 +03:00
committed by Dmitry Jemerov
parent a12877e51c
commit 6a4ea8b669
2 changed files with 17 additions and 4 deletions
@@ -70,8 +70,15 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
if (element is KtLightClassForFacade) return convertDeclaration(element, null, requiredType)
val parentCallback = fun(): UElement? {
val parent = KotlinConverter.unwrapElements(element.parent) ?: return null
return convertElementWithParent(parent, null)
val parent = element.parent
val parentUnwrapped = KotlinConverter.unwrapElements(parent) ?: return null
if (parent is KtValueArgument && parentUnwrapped is KtAnnotationEntry) {
val argumentName = parent.getArgumentName()?.asName?.asString() ?: ""
return (convertElementWithParent(parentUnwrapped, null) as UAnnotation)
.attributeValues.find { it.name == argumentName }
}
else
return convertElementWithParent(parentUnwrapped, null)
}
return convertDeclaration(element, parentCallback, requiredType)
?: KotlinConverter.convertPsiElement(element, parentCallback, requiredType)
@@ -168,7 +175,7 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
is KtFile -> el<UFile> { KotlinUFile(original, this@KotlinUastLanguagePlugin) }
is FakeFileForLightClass -> el<UFile> { KotlinUFile(original.navigationElement, this@KotlinUastLanguagePlugin) }
is KtAnnotationEntry -> el<UAnnotation>(build(::KotlinUAnnotation))
else -> null
}
}
@@ -200,6 +207,7 @@ internal object KotlinConverter {
internal tailrec fun unwrapElements(element: PsiElement?): PsiElement? = when (element) {
is KtValueArgumentList -> unwrapElements(element.parent)
is KtValueArgument -> unwrapElements(element.parent)
is KtDeclarationModifierList -> unwrapElements(element.parent)
else -> element
}
@@ -4,6 +4,7 @@ import com.intellij.psi.PsiModifier
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import org.jetbrains.uast.*
import org.jetbrains.uast.test.env.findElementByText
import org.junit.Assert
@@ -18,7 +19,11 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
doTest("AnnotationParameters") { _, file ->
val annotation = file.findElementByText<UAnnotation>("@IntRange(from = 10, to = 0)")
assertEquals(annotation.findAttributeValue("from")?.evaluate(), 10)
assertEquals(annotation.findAttributeValue("to")?.evaluate(), 0)
val toAttribute = annotation.findAttributeValue("to")!!
assertEquals(toAttribute.evaluate(), 0)
KtUsefulTestCase.assertInstanceOf(annotation.psi.toUElement(), UAnnotation::class.java)
KtUsefulTestCase.assertInstanceOf(toAttribute.uastParent, UNamedExpression::class.java)
KtUsefulTestCase.assertInstanceOf(toAttribute.psi.toUElement()?.uastParent, UNamedExpression::class.java)
}
}