[ULC] fix compatibility with java analyzer for repeatable annotation

* imitate `getReferenceName` to provide interop with existed java analyzer [AnnotationsHighlightUtil.java:238](https://github.com/JetBrains/intellij-community/blob/ad7c664356b2f21af1fa204d5b7b014d8af10fd3/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/AnnotationsHighlightUtil.java#L238)

^KTIJ-19318
This commit is contained in:
Dmitry Gridin
2021-09-07 17:07:05 +07:00
committed by Space
parent 11e71e4e27
commit 8b5d6827c9
4 changed files with 24 additions and 21 deletions
@@ -9,7 +9,6 @@ import com.intellij.psi.*
import com.intellij.psi.impl.PsiImplUtil
import com.intellij.psi.impl.light.LightIdentifier
import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation
import org.jetbrains.kotlin.asJava.elements.KtLightAnnotationForSourceEntry
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
import org.jetbrains.kotlin.asJava.elements.KtLightNullabilityAnnotation
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
@@ -45,9 +44,10 @@ class KtUltraLightSimpleAnnotation(
private val annotationFqName: String?,
private val argumentsList: List<Pair<String, ConstantValue<*>>>,
private val ultraLightSupport: KtUltraLightSupport,
parent: PsiElement
parent: PsiElement,
private val nameReferenceElementProvider: (() -> PsiJavaCodeReferenceElement?)? = null,
) : KtLightAbstractAnnotation(parent, computeDelegate = null) {
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = null
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = nameReferenceElementProvider?.invoke()
private val parameterList = ParameterListImpl()
@@ -14,7 +14,8 @@ import org.jetbrains.kotlin.asJava.classes.lazyPub
class KtLightPsiJavaCodeReferenceElement(
private val ktElement: PsiElement,
reference: () -> PsiReference?,
clsDelegateProvider: () -> PsiJavaCodeReferenceElement?
clsDelegateProvider: () -> PsiJavaCodeReferenceElement?,
private val customReferenceName: String? = null,
) :
PsiElement by ktElement,
PsiReference by LazyPsiReferenceDelegate(ktElement, reference),
@@ -29,7 +30,7 @@ class KtLightPsiJavaCodeReferenceElement(
override fun getTypeParameters(): Array<PsiType> = delegate?.typeParameters ?: emptyArray()
override fun getReferenceName(): String? = delegate?.referenceName
override fun getReferenceName(): String? = customReferenceName ?: delegate?.referenceName
override fun isQualified(): Boolean = delegate?.isQualified ?: false
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.resolve.constants.ArrayValue
@@ -145,10 +146,21 @@ internal fun PsiAnnotation.tryConvertAsRepeatable(
JAVA_LANG_ANNOTATION_REPEATABLE,
listOfNotNull(value),
support,
parent
)
parent,
) {
safeAs<KtLightAnnotationForSourceEntry>()?.kotlinOrigin?.safeAs<KtAnnotationEntry>()?.let {
KtLightPsiJavaCodeReferenceElement(
ktElement = it,
reference = { null },
clsDelegateProvider = { null },
customReferenceName = JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME,
)
}
}
}
internal val JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME: String get() = FqNames.repeatable.shortName().asString()
internal fun PsiAnnotation.tryConvertAsRepeatableContainer(support: KtUltraLightSupport): KtLightAbstractAnnotation? = tryConvertAs(
support,
FqNames.repeatable,
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.asJava.elements
@@ -127,7 +116,8 @@ class KtLightAnnotationForSourceEntry(
(kotlinOrigin as? KtAnnotationEntry)?.typeReference?.reference
?: (kotlinOrigin.calleeExpression?.nameReference)?.references?.firstOrNull()
},
{ lazyClsDelegate?.value?.nameReferenceElement }
{ lazyClsDelegate?.value?.nameReferenceElement },
if (qualifiedName == CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE) JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME else null,
)
private val ktLightAnnotationParameterList by lazyPub { KtLightAnnotationParameterList() }