toUElement() made work with KtAnnotationEntry (#1240)
* `KotlinUastLanguagePlugin#convertElement` made work with `KtAnnotationEntry` * `KotlinUastLanguagePlugin#convertElementWithParent` searches for parent `KotlinUNamedExpression`
This commit is contained in:
@@ -70,8 +70,15 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
|
|||||||
if (element is KtLightClassForFacade) return convertDeclaration(element, null, requiredType)
|
if (element is KtLightClassForFacade) return convertDeclaration(element, null, requiredType)
|
||||||
|
|
||||||
val parentCallback = fun(): UElement? {
|
val parentCallback = fun(): UElement? {
|
||||||
val parent = KotlinConverter.unwrapElements(element.parent) ?: return null
|
val parent = element.parent
|
||||||
return convertElementWithParent(parent, null)
|
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)
|
return convertDeclaration(element, parentCallback, requiredType)
|
||||||
?: KotlinConverter.convertPsiElement(element, parentCallback, requiredType)
|
?: KotlinConverter.convertPsiElement(element, parentCallback, requiredType)
|
||||||
@@ -168,7 +175,7 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
|
|||||||
|
|
||||||
is KtFile -> el<UFile> { KotlinUFile(original, this@KotlinUastLanguagePlugin) }
|
is KtFile -> el<UFile> { KotlinUFile(original, this@KotlinUastLanguagePlugin) }
|
||||||
is FakeFileForLightClass -> el<UFile> { KotlinUFile(original.navigationElement, this@KotlinUastLanguagePlugin) }
|
is FakeFileForLightClass -> el<UFile> { KotlinUFile(original.navigationElement, this@KotlinUastLanguagePlugin) }
|
||||||
|
is KtAnnotationEntry -> el<UAnnotation>(build(::KotlinUAnnotation))
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,6 +207,7 @@ internal object KotlinConverter {
|
|||||||
internal tailrec fun unwrapElements(element: PsiElement?): PsiElement? = when (element) {
|
internal tailrec fun unwrapElements(element: PsiElement?): PsiElement? = when (element) {
|
||||||
is KtValueArgumentList -> unwrapElements(element.parent)
|
is KtValueArgumentList -> unwrapElements(element.parent)
|
||||||
is KtValueArgument -> unwrapElements(element.parent)
|
is KtValueArgument -> unwrapElements(element.parent)
|
||||||
|
is KtDeclarationModifierList -> unwrapElements(element.parent)
|
||||||
else -> element
|
else -> element
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.intellij.psi.PsiModifier
|
|||||||
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
|
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
|
||||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
|
||||||
import org.jetbrains.uast.*
|
import org.jetbrains.uast.*
|
||||||
import org.jetbrains.uast.test.env.findElementByText
|
import org.jetbrains.uast.test.env.findElementByText
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
@@ -18,7 +19,11 @@ class KotlinUastApiTest : AbstractKotlinUastTest() {
|
|||||||
doTest("AnnotationParameters") { _, file ->
|
doTest("AnnotationParameters") { _, file ->
|
||||||
val annotation = file.findElementByText<UAnnotation>("@IntRange(from = 10, to = 0)")
|
val annotation = file.findElementByText<UAnnotation>("@IntRange(from = 10, to = 0)")
|
||||||
assertEquals(annotation.findAttributeValue("from")?.evaluate(), 10)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user