KtLightAnnotationForSourceEntry fix for smart-pointers creating

`LightElementValue` made PsiCompiledElement to make it properly anchorable when creating smartpointers, when still use `kotlinOrigin.containingFile` for it to be able to `registerProblem` on such elements. (refs KT-18054)
This commit is contained in:
Nicolay Mitropolsky
2017-08-30 17:46:30 +03:00
committed by Pavel Talanov
parent b0fb3370e3
commit be4b86e3d9
2 changed files with 11 additions and 5 deletions
@@ -67,7 +67,9 @@ class KtLightAnnotationForSourceEntry(
val delegate: D,
private val parent: PsiElement,
valueOrigin: AnnotationValueOrigin
) : PsiAnnotationMemberValue, PsiElement by delegate {
) : PsiAnnotationMemberValue, PsiCompiledElement, PsiElement by delegate {
override fun getMirror(): PsiElement = delegate
val originalExpression: PsiElement? by lazyPub(valueOrigin)
fun getConstantValue(): Any? {
@@ -81,9 +83,11 @@ class KtLightAnnotationForSourceEntry(
override fun getReferences() = originalExpression?.references.orEmpty()
override fun getLanguage() = KotlinLanguage.INSTANCE
override fun getNavigationElement() = originalExpression
override fun isPhysical(): Boolean = originalExpression?.containingFile == kotlinOrigin.containingFile
override fun getTextRange() = originalExpression?.textRange ?: TextRange.EMPTY_RANGE
override fun getParent() = parent
override fun getText() = originalExpression?.text.orEmpty()
override fun getContainingFile(): PsiFile? = if (isPhysical) kotlinOrigin.containingFile else delegate.containingFile
override fun replace(newElement: PsiElement): PsiElement {
val value = (newElement as? PsiLiteral)?.value as? String ?: return this
@@ -21,13 +21,12 @@ import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.uast.java.annotations
class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
fun testBooleanAnnotation() {
fun testBooleanAnnotationDefaultValue() {
myFixture.addClass("""
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@@ -70,9 +69,10 @@ class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() {
""".trimIndent())
myFixture.testHighlighting("Qualifier.java", "AnnotatedClass.kt")
val annotations = myFixture.findClass("AnnotatedClass").methods.first { it.name == "bar" }.parameterList.parameters.single()
val annotation = myFixture.findClass("AnnotatedClass").methods.first { it.name == "bar" }.parameterList.parameters.single()
.expectAnnotations(2).single { it.qualifiedName == "Qualifier" }
val annotationAttributeVal = annotations.findAttributeValue("value") as PsiElement
val annotationAttributeVal = annotation.findAttributeValue("value") as PsiElement
TestCase.assertTrue(annotationAttributeVal.isPhysical)
assertTextRangeAndValue("\"foo\"", "foo", annotationAttributeVal)
}
@@ -372,6 +372,8 @@ class KtLightAnnotationTest : KotlinLightCodeInsightFixtureTestCase() {
assertTextAndRange(expected, psiElement)
val result = JavaPsiFacade.getInstance(project).constantEvaluationHelper.computeConstantExpression(psiElement)
TestCase.assertEquals(value, result)
val smartPointer = SmartPointerManager.getInstance(psiElement.project).createSmartPsiElementPointer(psiElement)
assertTextAndRange(expected, smartPointer.element!!)
}
private fun PsiModifierListOwner.expectAnnotations(number: Int): Array<PsiAnnotation> =