[Analysis API FIR] preserve type annotations on asPsiType conversion

^KT-66603 Fixed
This commit is contained in:
Dmitrii Gridin
2024-03-14 18:31:58 +01:00
committed by Space Team
parent 3c8a95e623
commit 96575a0bdb
18 changed files with 136 additions and 48 deletions
@@ -22,6 +22,16 @@ public abstract class KtPsiTypeProvider : KtAnalysisSessionComponent() {
allowErrorTypes: Boolean,
): PsiTypeElement?
public abstract fun asPsiType(
type: KtType,
useSitePosition: PsiElement,
allowErrorTypes: Boolean,
mode: KtTypeMappingMode,
isAnnotationMethod: Boolean,
suppressWildcards: Boolean?,
preserveAnnotations: Boolean,
): PsiType?
public abstract fun asKtType(
psiType: PsiType,
useSitePosition: PsiElement
@@ -54,6 +64,10 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
* `null` is no-op by default, i.e., their suppression/appearance is determined by type annotations.
*
* Note: [PsiTypeElement] is JVM conception, so this method will return `null` for non-JVM platforms.
*
* @return [PsiTypeElement] without type annotations if mapping is successful
*
* @see asPsiType
*/
public fun KtType.asPsiTypeElement(
useSitePosition: PsiElement,
@@ -63,23 +77,30 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
suppressWildcards: Boolean? = null,
): PsiTypeElement? = withValidityAssertion {
analysisSession.psiTypeProvider.asPsiTypeElement(
this,
useSitePosition,
mode,
isAnnotationMethod,
suppressWildcards,
allowErrorTypes,
type = this,
useSitePosition = useSitePosition,
mode = mode,
isAnnotationMethod = isAnnotationMethod,
suppressWildcards = suppressWildcards,
allowErrorTypes = allowErrorTypes,
)
}
/**
* Converts the given [KtType] to [PsiType] under [useSitePosition] context.
*
* This simply unwraps [PsiTypeElement] returned from [asPsiTypeElement].
* Use this version if type annotation is not required. Otherwise, use [asPsiTypeElement] to get [PsiTypeElement] as an owner of
* annotations on [PsiType] and annotate the resulting [PsiType] with proper [PsiAnnotation][com.intellij.psi.PsiAnnotation].
*
* Note: [PsiType] is JVM conception, so this method will return `null` for non-JVM platforms.
*
* @receiver type to convert
*
* @param useSitePosition is used to determine if the given [KtType] needs to be approximated.
* For instance, if the given type is local yet available in the same scope of use site,
* we can still use such a local type.
* Otherwise, e.g., exposed to public as a return type, the resulting type will be approximated accordingly.
*
* @param allowErrorTypes if **false** the result will be null in the case of an error type inside the [type][this]
*
* @param preserveAnnotations if **true** the result [PsiType] will have converted annotations from the original [type][this]
*/
public fun KtType.asPsiType(
useSitePosition: PsiElement,
@@ -87,14 +108,18 @@ public interface KtPsiTypeProviderMixIn : KtAnalysisSessionMixIn {
mode: KtTypeMappingMode = KtTypeMappingMode.DEFAULT,
isAnnotationMethod: Boolean = false,
suppressWildcards: Boolean? = null,
): PsiType? =
asPsiTypeElement(
useSitePosition,
allowErrorTypes,
mode,
isAnnotationMethod,
suppressWildcards,
)?.type
preserveAnnotations: Boolean = true,
): PsiType? = withValidityAssertion {
analysisSession.psiTypeProvider.asPsiType(
type = this,
useSitePosition = useSitePosition,
allowErrorTypes = allowErrorTypes,
mode = mode,
isAnnotationMethod = isAnnotationMethod,
suppressWildcards = suppressWildcards,
preserveAnnotations = preserveAnnotations,
)
}
/**
* Converts given [PsiType] to [KtType].
@@ -0,0 +1,2 @@
KtType: @foo.MyAnno(s = "outer") kotlin.collections.List<@foo.MyAnno(s = "middle") kotlin.collections.List<@foo.AnotherAnnotation(k = foo.Nested::class) kotlin.String>>
PsiType: java.util.List<? extends java.util.List<? extends java.lang.String>>
@@ -1,2 +1,2 @@
KtType: @foo.MyAnno(s = "outer") kotlin.collections.List<@foo.MyAnno(s = "middle") kotlin.collections.List<@foo.AnotherAnnotation(k = foo.Nested::class) kotlin.String>>
PsiType: java.util.List<? extends java.util.List<? extends java.lang.String>>
PsiType: java.util.@foo.MyAnno("outer") List<@foo.MyAnno("middle") ? extends java.util.List<? extends java.lang.String>>
@@ -0,0 +1,2 @@
KtType: @foo.MyAnno(s = "str1") kotlin.String
PsiType: java.lang.String
@@ -1,2 +1,2 @@
KtType: @foo.MyAnno(s = "str1") kotlin.String
PsiType: java.lang.String
PsiType: java.lang.@foo.MyAnno("str" + "1") String
@@ -1,2 +1,2 @@
KtType: @kotlin.jvm.JvmSuppressWildcards(suppress = false) (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
PsiType: kotlin.jvm.functions.Function1<? super AnimatedContentTransitionScope<NavBackStackEntry>,? extends EnterTransition>
PsiType: kotlin.jvm.functions.@kotlin.jvm.JvmSuppressWildcards(suppress = false) Function1<? super AnimatedContentTransitionScope<NavBackStackEntry>,? extends EnterTransition>
@@ -1,2 +1,2 @@
KtType: @kotlin.jvm.JvmSuppressWildcards(suppress = true) (AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition?)?
PsiType: kotlin.jvm.functions.Function1<AnimatedContentTransitionScope<NavBackStackEntry>,EnterTransition>
PsiType: kotlin.jvm.functions.@kotlin.jvm.JvmSuppressWildcards(suppress = true) Function1<AnimatedContentTransitionScope<NavBackStackEntry>,EnterTransition>