[SLC] drop KtAnalysisSession from SymbolLightParameterForReceiver

^KT-54051
This commit is contained in:
Dmitrii Gridin
2022-11-17 10:13:13 +01:00
committed by Space Team
parent e1cc244be0
commit af1d882c74
2 changed files with 49 additions and 35 deletions
@@ -10,12 +10,14 @@ import com.intellij.psi.PsiIdentifier
import com.intellij.psi.PsiModifierList import com.intellij.psi.PsiModifierList
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.annotations.annotations import org.jetbrains.kotlin.analysis.api.annotations.annotations
import org.jetbrains.kotlin.analysis.api.lifetime.isValid
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtReceiverParameterSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedSymbol import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedSymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.analysis.api.symbols.receiverType
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.asJava.classes.lazyPub import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.light.classes.symbol.annotations.SymbolLightAnnotationForAnnotationCall import org.jetbrains.kotlin.light.classes.symbol.annotations.SymbolLightAnnotationForAnnotationCall
@@ -24,31 +26,39 @@ import org.jetbrains.kotlin.light.classes.symbol.methods.SymbolLightMethodBase
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.SymbolLightClassModifierList import org.jetbrains.kotlin.light.classes.symbol.modifierLists.SymbolLightClassModifierList
import org.jetbrains.kotlin.light.classes.symbol.nonExistentType import org.jetbrains.kotlin.light.classes.symbol.nonExistentType
import org.jetbrains.kotlin.light.classes.symbol.nullabilityType import org.jetbrains.kotlin.light.classes.symbol.nullabilityType
import org.jetbrains.kotlin.light.classes.symbol.restoreSymbolOrThrowIfDisposed
import org.jetbrains.kotlin.psi.KtParameter import org.jetbrains.kotlin.psi.KtParameter
context(KtAnalysisSession)
internal class SymbolLightParameterForReceiver private constructor( internal class SymbolLightParameterForReceiver private constructor(
private val receiver: KtReceiverParameterSymbol, private val ktModule: KtModule,
private val context: KtSymbol, private val callableSymbolWithReceiverPointer: KtSymbolPointer<KtCallableSymbol>,
methodName: String, methodName: String,
method: SymbolLightMethodBase method: SymbolLightMethodBase
) : SymbolLightParameterBase(method) { ) : SymbolLightParameterBase(method) {
private inline fun <T> withReceiverType(crossinline action: context(KtAnalysisSession) (KtType) -> T): T {
return analyze(ktModule) {
val callableSymbol = callableSymbolWithReceiverPointer.restoreSymbolOrThrowIfDisposed()
action(this, requireNotNull(callableSymbol.receiverType))
}
}
companion object { companion object {
context (KtAnalysisSession)
fun tryGet( fun tryGet(
callableSymbol: KtCallableSymbol, ktModule: KtModule,
callableSymbolPointer: KtSymbolPointer<KtCallableSymbol>,
method: SymbolLightMethodBase method: SymbolLightMethodBase
): SymbolLightParameterForReceiver? { ): SymbolLightParameterForReceiver? {
if (callableSymbol !is KtNamedSymbol) return null val methodName = analyze(ktModule) {
val callableSymbol = callableSymbolPointer.restoreSymbolOrThrowIfDisposed()
if (!callableSymbol.isExtension) return null if (callableSymbol !is KtNamedSymbol) return@analyze null
val extensionTypeAndAnnotations = callableSymbol.receiverParameter ?: return null if (!callableSymbol.isExtension || callableSymbol.receiverType == null) return@analyze null
callableSymbol.name.asString()
} ?: return null
return SymbolLightParameterForReceiver( return SymbolLightParameterForReceiver(
receiver = extensionTypeAndAnnotations, ktModule = ktModule,
context = callableSymbol, callableSymbolWithReceiverPointer = callableSymbolPointer,
methodName = callableSymbol.name.asString(), methodName = methodName,
method = method, method = method,
) )
} }
@@ -68,33 +78,42 @@ internal class SymbolLightParameterForReceiver private constructor(
override val kotlinOrigin: KtParameter? = null override val kotlinOrigin: KtParameter? = null
private val _annotations: List<PsiAnnotation> by lazyPub { private val _annotations: List<PsiAnnotation> by lazyPub {
buildList { withReceiverType { receiverType ->
receiver.type.nullabilityType.computeNullabilityAnnotation(this@SymbolLightParameterForReceiver)?.let { add(it) } buildList {
receiver.annotations.mapTo(this) { receiverType.nullabilityType.computeNullabilityAnnotation(this@SymbolLightParameterForReceiver)?.let { add(it) }
SymbolLightAnnotationForAnnotationCall(it, this@SymbolLightParameterForReceiver) receiverType.annotations.mapTo(this) {
SymbolLightAnnotationForAnnotationCall(it, this@SymbolLightParameterForReceiver)
}
} }
} }
} }
override fun getModifierList(): PsiModifierList = _modifierList override fun getModifierList(): PsiModifierList = _modifierList
private val _modifierList: PsiModifierList by lazyPub {
private val _modifierList: PsiModifierList by lazy {
SymbolLightClassModifierList(this, lazyOf(emptySet()), lazyOf(_annotations)) SymbolLightClassModifierList(this, lazyOf(emptySet()), lazyOf(_annotations))
} }
private val _type: PsiType by lazyPub { private val _type: PsiType by lazy {
receiver.type.asPsiType(this@SymbolLightParameterForReceiver) ?: nonExistentType() withReceiverType { receiverType ->
receiverType.asPsiType(this@SymbolLightParameterForReceiver)
} ?: nonExistentType()
} }
override fun getType(): PsiType = _type override fun getType(): PsiType = _type
override fun equals(other: Any?): Boolean = override fun equals(other: Any?): Boolean = this === other ||
this === other || other is SymbolLightParameterForReceiver &&
(other is SymbolLightParameterForReceiver && ktModule == other.ktModule &&
receiver == other.receiver) analyze(ktModule) {
callableSymbolWithReceiverPointer.restoreSymbol() == other.callableSymbolWithReceiverPointer.restoreSymbol()
}
override fun hashCode(): Int = kotlinOrigin.hashCode() override fun hashCode(): Int = _name.hashCode()
override fun isValid(): Boolean = super.isValid() && context.isValid() override fun isValid(): Boolean = super.isValid() && analyze(ktModule) {
callableSymbolWithReceiverPointer.restoreSymbol()?.receiverType != null
}
} }
@@ -11,13 +11,10 @@ import com.intellij.psi.impl.light.LightParameterListBuilder
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.analysis.project.structure.KtModule import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.asJava.elements.KtLightElement import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
import org.jetbrains.kotlin.light.classes.symbol.allowLightClassesOnEdt import org.jetbrains.kotlin.light.classes.symbol.allowLightClassesOnEdt
import org.jetbrains.kotlin.light.classes.symbol.classes.analyzeForLightClasses
import org.jetbrains.kotlin.light.classes.symbol.methods.SymbolLightMethodBase import org.jetbrains.kotlin.light.classes.symbol.methods.SymbolLightMethodBase
import org.jetbrains.kotlin.light.classes.symbol.restoreSymbolOrThrowIfDisposed
import org.jetbrains.kotlin.psi.KtFunction import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtParameterList import org.jetbrains.kotlin.psi.KtParameterList
@@ -39,10 +36,8 @@ internal class SymbolLightParameterList(
val builder = LightParameterListBuilder(manager, language) val builder = LightParameterListBuilder(manager, language)
callableWithReceiverSymbolPointer?.let { callableWithReceiverSymbolPointer?.let {
analyzeForLightClasses(ktModule) { SymbolLightParameterForReceiver.tryGet(ktModule, it, parent)?.let { receiver ->
SymbolLightParameterForReceiver.tryGet(it.restoreSymbolOrThrowIfDisposed(), parent)?.let { receiver -> builder.addParameter(receiver)
builder.addParameter(receiver)
}
} }
} }