|
| 1 | +package com.google.devtools.ksp.standalone |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project |
| 4 | +import com.intellij.psi.search.GlobalSearchScope |
| 5 | +import org.jetbrains.kotlin.analysis.api.fir.utils.isSubClassOf |
| 6 | +import org.jetbrains.kotlin.analysis.api.standalone.base.providers.KotlinStandaloneDirectInheritorsProvider |
| 7 | +import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirInternals |
| 8 | +import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSessionCache |
| 9 | +import org.jetbrains.kotlin.analysis.project.structure.KtDanglingFileModule |
| 10 | +import org.jetbrains.kotlin.analysis.project.structure.KtModule |
| 11 | +import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider |
| 12 | +import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProviderFactory |
| 13 | +import org.jetbrains.kotlin.analysis.providers.KotlinDirectInheritorsProvider |
| 14 | +import org.jetbrains.kotlin.analysis.providers.impl.KotlinStaticDeclarationProviderFactory |
| 15 | +import org.jetbrains.kotlin.fir.declarations.FirClass |
| 16 | +import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider |
| 17 | +import org.jetbrains.kotlin.fir.symbols.SymbolInternals |
| 18 | +import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol |
| 19 | +import org.jetbrains.kotlin.name.ClassId |
| 20 | +import org.jetbrains.kotlin.name.Name |
| 21 | +import org.jetbrains.kotlin.psi.KtClass |
| 22 | +import org.jetbrains.kotlin.psi.KtClassOrObject |
| 23 | +import org.jetbrains.kotlin.psi.psiUtil.contains |
| 24 | + |
| 25 | +// TODO: copied from upstream as a workaround, remove after upstream fixes standalone session builder for KSP. |
| 26 | +@OptIn(LLFirInternals::class, SymbolInternals::class) |
| 27 | +class KspStandaloneDirectInheritorsProvider(private val project: Project) : KotlinDirectInheritorsProvider { |
| 28 | + private val staticDeclarationProviderFactory by lazy { |
| 29 | + ( |
| 30 | + (KotlinDeclarationProviderFactory.getInstance(project) as? IncrementalKotlinDeclarationProviderFactory) |
| 31 | + ?.staticFactory as? KotlinStaticDeclarationProviderFactory |
| 32 | + ) ?: error( |
| 33 | + "`${KotlinStandaloneDirectInheritorsProvider::class.simpleName}" + |
| 34 | + "` expects the following declaration provider factory to be" + |
| 35 | + " registered: `${KotlinStaticDeclarationProviderFactory::class.simpleName}`" |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + override fun getDirectKotlinInheritors( |
| 40 | + ktClass: KtClass, |
| 41 | + scope: GlobalSearchScope, |
| 42 | + includeLocalInheritors: Boolean, |
| 43 | + ): Iterable<KtClassOrObject> { |
| 44 | + val classId = ktClass.getClassId() ?: return emptyList() |
| 45 | + |
| 46 | + val aliases = mutableSetOf(classId.shortClassName) |
| 47 | + calculateAliases(classId.shortClassName, aliases) |
| 48 | + |
| 49 | + val possibleInheritors = aliases.flatMap { staticDeclarationProviderFactory.getDirectInheritorCandidates(it) } |
| 50 | + |
| 51 | + if (possibleInheritors.isEmpty()) { |
| 52 | + return emptyList() |
| 53 | + } |
| 54 | + |
| 55 | + // The index provides candidates from an original module, not dangling files. If we resolve the supertypes of a candidate in the |
| 56 | + // context of its session, we will resolve to FIR classes from non-dangling, original modules. If `ktClass` is inside a dangling |
| 57 | + // file, the FIR class for `ktClass` will come from the dangling module. So we'd compare the original FIR class for the supertype |
| 58 | + // with the dangling FIR class for `ktClass`, resulting in a mismatch. To avoid such incompatible comparisons, we need to resolve |
| 59 | + // `ktClass` to the original FIR class. |
| 60 | + // |
| 61 | + // Note that this means we don't support providing inheritors based on the dangling file yet, for example if an inheritor was added |
| 62 | + // or removed only in the dangling file. |
| 63 | + val baseKtModule = when ( |
| 64 | + val ktModule = ProjectStructureProvider.getModule(project, ktClass, contextualModule = null) |
| 65 | + ) { |
| 66 | + is KtDanglingFileModule -> ktModule.contextModule |
| 67 | + else -> ktModule |
| 68 | + } |
| 69 | + |
| 70 | + val baseFirClass = ktClass.toFirSymbol(classId, baseKtModule)?.fir as? FirClass ?: return emptyList() |
| 71 | + return possibleInheritors.filter { isValidInheritor(it, baseFirClass, scope, includeLocalInheritors) } |
| 72 | + } |
| 73 | + |
| 74 | + private fun calculateAliases(aliasedName: Name, aliases: MutableSet<Name>) { |
| 75 | + staticDeclarationProviderFactory.getInheritableTypeAliases(aliasedName).forEach { alias -> |
| 76 | + val aliasName = alias.nameAsSafeName |
| 77 | + val isNewAliasName = aliases.add(aliasName) |
| 78 | + if (isNewAliasName) { |
| 79 | + calculateAliases(aliasName, aliases) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun isValidInheritor( |
| 85 | + candidate: KtClassOrObject, |
| 86 | + baseFirClass: FirClass, |
| 87 | + scope: GlobalSearchScope, |
| 88 | + includeLocalInheritors: Boolean, |
| 89 | + ): Boolean { |
| 90 | + if (!includeLocalInheritors && candidate.isLocal) { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + if (!scope.contains(candidate)) { |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + val candidateClassId = candidate.getClassId() ?: return false |
| 99 | + val candidateKtModule = ProjectStructureProvider.getModule(project, candidate, contextualModule = null) |
| 100 | + val candidateFirSymbol = candidate.toFirSymbol(candidateClassId, candidateKtModule) ?: return false |
| 101 | + val candidateFirClass = candidateFirSymbol.fir as? FirClass ?: return false |
| 102 | + |
| 103 | + return isSubClassOf( |
| 104 | + candidateFirClass, |
| 105 | + baseFirClass, |
| 106 | + candidateFirClass.moduleData.session, |
| 107 | + allowIndirectSubtyping = false |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + private fun KtClassOrObject.toFirSymbol(classId: ClassId, ktModule: KtModule): FirClassLikeSymbol<*>? { |
| 112 | + val session = LLFirSessionCache.getInstance(project).getSession(ktModule, preferBinary = true) |
| 113 | + return session.symbolProvider.getClassLikeSymbolByClassId(classId) |
| 114 | + } |
| 115 | +} |
0 commit comments