[Analysis API] simplify KotlinDeclarationProvider interface
This commit is contained in:
+3
-2
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.analysis.api.fir
|
package org.jetbrains.kotlin.analysis.api.fir
|
||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
|
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
|
||||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||||
import org.jetbrains.kotlin.analysis.api.components.*
|
import org.jetbrains.kotlin.analysis.api.components.*
|
||||||
@@ -140,8 +141,8 @@ private constructor(
|
|||||||
internal val firSymbolProvider: FirSymbolProvider get() = useSiteSession.symbolProvider
|
internal val firSymbolProvider: FirSymbolProvider get() = useSiteSession.symbolProvider
|
||||||
internal val targetPlatform: TargetPlatform get() = useSiteSession.moduleData.platform
|
internal val targetPlatform: TargetPlatform get() = useSiteSession.moduleData.platform
|
||||||
|
|
||||||
internal val useSiteScopeDeclarationProvider: KotlinDeclarationProvider =
|
val useSiteAnalisisScope: GlobalSearchScope = analysisScopeProviderImpl.getAnalysisScope()
|
||||||
project.createDeclarationProvider(analysisScopeProviderImpl.getAnalysisScope())
|
val useSiteScopeDeclarationProvider: KotlinDeclarationProvider = project.createDeclarationProvider(useSiteAnalisisScope)
|
||||||
|
|
||||||
fun getScopeSessionFor(session: FirSession): ScopeSession = withValidityAssertion { firResolveSession.getScopeSessionFor(session) }
|
fun getScopeSessionFor(session: FirSession): ScopeSession = withValidityAssertion { firResolveSession.getScopeSessionFor(session) }
|
||||||
|
|
||||||
|
|||||||
+15
-16
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.*
|
|||||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||||
import org.jetbrains.kotlin.fir.symbols.ensureResolved
|
import org.jetbrains.kotlin.fir.symbols.ensureResolved
|
||||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
@@ -159,14 +160,7 @@ internal class KtFirScopeProvider(
|
|||||||
|
|
||||||
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope {
|
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope {
|
||||||
return packageMemberScopeCache.getOrPut(packageSymbol) {
|
return packageMemberScopeCache.getOrPut(packageSymbol) {
|
||||||
KtFirPackageScope(
|
createPackageScope(packageSymbol.fqName)
|
||||||
packageSymbol.fqName,
|
|
||||||
project,
|
|
||||||
builder,
|
|
||||||
token,
|
|
||||||
GlobalSearchScope.allScope(project), // TODO
|
|
||||||
analysisSession.targetPlatform,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,19 +226,24 @@ internal class KtFirScopeProvider(
|
|||||||
return when (firScope) {
|
return when (firScope) {
|
||||||
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder, token)
|
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder, token)
|
||||||
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, analysisSession.useSiteScopeDeclarationProvider, token)
|
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, analysisSession.useSiteScopeDeclarationProvider, token)
|
||||||
is FirPackageMemberScope -> KtFirPackageScope(
|
is FirPackageMemberScope -> createPackageScope(firScope.fqName)
|
||||||
firScope.fqName,
|
|
||||||
project,
|
|
||||||
builder,
|
|
||||||
token,
|
|
||||||
GlobalSearchScope.allScope(project), // todo
|
|
||||||
analysisSession.targetPlatform
|
|
||||||
)
|
|
||||||
is FirContainingNamesAwareScope -> KtFirDelegatingScope(firScope, builder, token)
|
is FirContainingNamesAwareScope -> KtFirDelegatingScope(firScope, builder, token)
|
||||||
else -> TODO(firScope::class.toString())
|
else -> TODO(firScope::class.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createPackageScope(fqName: FqName): KtFirPackageScope {
|
||||||
|
return KtFirPackageScope(
|
||||||
|
fqName,
|
||||||
|
project,
|
||||||
|
builder,
|
||||||
|
token,
|
||||||
|
analysisSession.useSiteAnalisisScope,
|
||||||
|
analysisSession.useSiteScopeDeclarationProvider,
|
||||||
|
analysisSession.targetPlatform
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun convertToKtTypeScope(firScope: FirScope): KtTypeScope {
|
private fun convertToKtTypeScope(firScope: FirScope): KtTypeScope {
|
||||||
return when (firScope) {
|
return when (firScope) {
|
||||||
is FirContainingNamesAwareScope -> KtFirDelegatingTypeScope(firScope, builder, token)
|
is FirContainingNamesAwareScope -> KtFirDelegatingTypeScope(firScope, builder, token)
|
||||||
|
|||||||
+4
-7
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
|
|||||||
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
|
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
|
||||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||||
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||||
import org.jetbrains.kotlin.analysis.providers.createDeclarationProvider
|
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||||
import org.jetbrains.kotlin.analysis.providers.createPackageProvider
|
import org.jetbrains.kotlin.analysis.providers.createPackageProvider
|
||||||
import org.jetbrains.kotlin.fir.extensions.FirExtensionService
|
import org.jetbrains.kotlin.fir.extensions.FirExtensionService
|
||||||
import org.jetbrains.kotlin.fir.extensions.declarationGenerators
|
import org.jetbrains.kotlin.fir.extensions.declarationGenerators
|
||||||
@@ -34,9 +34,9 @@ internal class KtFirPackageScope(
|
|||||||
private val builder: KtSymbolByFirBuilder,
|
private val builder: KtSymbolByFirBuilder,
|
||||||
override val token: KtLifetimeToken,
|
override val token: KtLifetimeToken,
|
||||||
private val searchScope: GlobalSearchScope,
|
private val searchScope: GlobalSearchScope,
|
||||||
|
private val declarationProvider: KotlinDeclarationProvider,
|
||||||
private val targetPlatform: TargetPlatform,
|
private val targetPlatform: TargetPlatform,
|
||||||
) : KtScope {
|
) : KtScope {
|
||||||
private val declarationsProvider = project.createDeclarationProvider(searchScope)
|
|
||||||
private val packageProvider = project.createPackageProvider(searchScope)
|
private val packageProvider = project.createPackageProvider(searchScope)
|
||||||
|
|
||||||
private val firScope: FirPackageMemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
private val firScope: FirPackageMemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||||
@@ -48,17 +48,14 @@ internal class KtFirPackageScope(
|
|||||||
|
|
||||||
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
||||||
hashSetOf<Name>().apply {
|
hashSetOf<Name>().apply {
|
||||||
addAll(declarationsProvider.getFunctionsNamesInPackage(fqName))
|
addAll(declarationProvider.getTopLevelCallableNamesInPackage(fqName))
|
||||||
addAll(declarationsProvider.getPropertyNamesInPackage(fqName))
|
|
||||||
|
|
||||||
addAll(collectGeneratedTopLevelCallables())
|
addAll(collectGeneratedTopLevelCallables())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
||||||
hashSetOf<Name>().apply {
|
hashSetOf<Name>().apply {
|
||||||
addAll(declarationsProvider.getClassNamesInPackage(fqName))
|
addAll(declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(fqName))
|
||||||
addAll(declarationsProvider.getTypeAliasNamesInPackage(fqName))
|
|
||||||
|
|
||||||
JavaPsiFacade.getInstance(project)
|
JavaPsiFacade.getInstance(project)
|
||||||
.findPackage(fqName.asString())
|
.findPackage(fqName.asString())
|
||||||
|
|||||||
+2
-4
@@ -51,8 +51,7 @@ internal class KtFirStarImportingScope(
|
|||||||
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
||||||
imports.flatMapTo(hashSetOf()) { import: Import ->
|
imports.flatMapTo(hashSetOf()) { import: Import ->
|
||||||
if (import.relativeClassName == null) { // top level callable
|
if (import.relativeClassName == null) { // top level callable
|
||||||
declarationProvider.getFunctionsNamesInPackage(import.packageFqName) +
|
declarationProvider.getTopLevelCallableNamesInPackage(import.packageFqName)
|
||||||
declarationProvider.getPropertyNamesInPackage(import.packageFqName)
|
|
||||||
} else { //member
|
} else { //member
|
||||||
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
||||||
firScope.getStaticsScope(classId)?.getCallableNames().orEmpty()
|
firScope.getStaticsScope(classId)?.getCallableNames().orEmpty()
|
||||||
@@ -69,8 +68,7 @@ internal class KtFirStarImportingScope(
|
|||||||
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
||||||
imports.flatMapTo(hashSetOf()) { import ->
|
imports.flatMapTo(hashSetOf()) { import ->
|
||||||
if (import.relativeClassName == null) {
|
if (import.relativeClassName == null) {
|
||||||
declarationProvider.getClassNamesInPackage(import.packageFqName) +
|
declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(import.packageFqName)
|
||||||
declarationProvider.getTypeAliasNamesInPackage(import.packageFqName)
|
|
||||||
} else {
|
} else {
|
||||||
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
||||||
firScope.getStaticsScope(classId)?.getClassifierNames().orEmpty()
|
firScope.getStaticsScope(classId)?.getClassifierNames().orEmpty()
|
||||||
|
|||||||
+3
-4
@@ -24,14 +24,13 @@ public abstract class KotlinDeclarationProvider {
|
|||||||
public abstract fun getAllClassesByClassId(classId: ClassId): Collection<KtClassOrObject>
|
public abstract fun getAllClassesByClassId(classId: ClassId): Collection<KtClassOrObject>
|
||||||
public abstract fun getAllTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias>
|
public abstract fun getAllTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias>
|
||||||
|
|
||||||
public abstract fun getClassNamesInPackage(packageFqName: FqName): Set<Name>
|
public abstract fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name>
|
||||||
public abstract fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name>
|
|
||||||
|
|
||||||
public abstract fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty>
|
public abstract fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty>
|
||||||
public abstract fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction>
|
public abstract fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction>
|
||||||
|
|
||||||
public abstract fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name>
|
public abstract fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name>
|
||||||
public abstract fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name>
|
|
||||||
public abstract fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile>
|
public abstract fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile>
|
||||||
public abstract fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile>
|
public abstract fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile>
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-28
@@ -51,37 +51,21 @@ public class KotlinStaticDeclarationProvider internal constructor(
|
|||||||
}
|
}
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
|
|
||||||
override fun getClassNamesInPackage(packageFqName: FqName): Set<Name> =
|
|
||||||
index.classMap[packageFqName]
|
|
||||||
?.filter { ktClassOrObject ->
|
|
||||||
ktClassOrObject.inScope
|
|
||||||
}
|
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name> =
|
override fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name> {
|
||||||
index.typeAliasMap[packageFqName]
|
val classifiers = index.classMap[packageFqName].orEmpty() + index.typeAliasMap[packageFqName].orEmpty()
|
||||||
?.filter { ktTypeAlias ->
|
return classifiers.filter { it.inScope }
|
||||||
ktTypeAlias.inScope
|
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||||
}
|
}
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name> =
|
|
||||||
index.topLevelPropertyMap[packageFqName]
|
|
||||||
?.filter { ktProperty ->
|
|
||||||
ktProperty.inScope
|
|
||||||
}
|
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name> =
|
override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> {
|
||||||
index.topLevelFunctionMap[packageFqName]
|
val callables = index.topLevelPropertyMap[packageFqName].orEmpty() + index.topLevelFunctionMap[packageFqName].orEmpty()
|
||||||
?.filter { ktNamedFunction ->
|
return callables
|
||||||
ktNamedFunction.inScope
|
.filter { it.inScope }
|
||||||
}
|
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
}
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
||||||
index.facadeFileMap[packageFqName]
|
index.facadeFileMap[packageFqName]
|
||||||
|
|||||||
+1
-2
@@ -6,7 +6,6 @@
|
|||||||
package org.jetbrains.kotlin.analysis.low.level.api.fir.providers
|
package org.jetbrains.kotlin.analysis.low.level.api.fir.providers
|
||||||
|
|
||||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirModuleResolveComponents
|
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirModuleResolveComponents
|
||||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.file.builder.ModuleFileCache
|
|
||||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||||
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProvider
|
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProvider
|
||||||
import org.jetbrains.kotlin.fir.FirSession
|
import org.jetbrains.kotlin.fir.FirSession
|
||||||
@@ -80,7 +79,7 @@ internal class LLFirProvider(
|
|||||||
|
|
||||||
|
|
||||||
override fun getClassNamesInPackage(fqName: FqName): Set<Name> =
|
override fun getClassNamesInPackage(fqName: FqName): Set<Name> =
|
||||||
declarationProvider.getClassNamesInPackage(fqName)
|
declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(fqName)
|
||||||
|
|
||||||
@NoMutableState
|
@NoMutableState
|
||||||
private inner class SymbolProvider : FirSymbolProvider(session) {
|
private inner class SymbolProvider : FirSymbolProvider(session) {
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ class KotlinAsJavaFirSupport(private val project: Project) : KotlinAsJavaSupport
|
|||||||
packageFqName: FqName,
|
packageFqName: FqName,
|
||||||
searchScope: GlobalSearchScope
|
searchScope: GlobalSearchScope
|
||||||
): Collection<KtClassOrObject> = project.createDeclarationProvider(searchScope).run {
|
): Collection<KtClassOrObject> = project.createDeclarationProvider(searchScope).run {
|
||||||
getClassNamesInPackage(packageFqName).flatMap {
|
getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName).flatMap {
|
||||||
getAllClassesByClassId(ClassId.topLevel(packageFqName.child(it)))
|
getAllClassesByClassId(ClassId.topLevel(packageFqName.child(it)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user