From be4b86e3d9c0c2b988bfa1d05d2d572f9360a53d Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Wed, 30 Aug 2017 17:46:30 +0300 Subject: [PATCH] `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) --- .../kotlin/asJava/elements/lightAnnotations.kt | 6 +++++- .../jetbrains/kotlin/asJava/KtLightAnnotationTest.kt | 10 ++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt index 812dfb9c72d..8844bd063f5 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt @@ -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 diff --git a/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt b/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt index 9726b924f87..70b553136c4 100644 --- a/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt +++ b/idea/tests/org/jetbrains/kotlin/asJava/KtLightAnnotationTest.kt @@ -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 =