Skip to content

KotlinDeclarationProvider

A KotlinDeclarationProvider provides Kotlin PSI declarations for the project as well as additional information about them. The declarations may come from sources, libraries in case of stubbing, or declaration generation.

Declaration providers usually don't cover declarations of the whole project, but rather of a restricted context. This usually means being limited to a specific scope, but the details depend on the kind of declaration provider.

The main kind of declaration providers is created via KotlinDeclarationProviderFactory and it is scope-based, but there are other kinds of declaration providers, such as those created by KotlinForwardDeclarationProviderFactory.

Declaration providers are critical for performance, so implementations should cache results.

Lifetime

If a KotlinDeclarationProvider has been created for a specific KaModule, its lifetime does not exceed the lifetime of the underlying LL FIR session. For other declaration providers, the Analysis API guarantees that their lifetime is constrained by the next out-of-block or module state modification (see KotlinModificationEvent).

This allows a declaration provider to cache values without worrying about invalidation as it can assume an immutable module/project state.

Inheritors

Properties

hasSpecificCallablePackageNamesComputation

Whether the declaration provider has a specific implementation of computePackageNamesWithTopLevelCallables. This allows the Analysis API backend to determine whether callable package sets are computed and cached separately or with computePackageNames.

hasSpecificClassifierPackageNamesComputation

Whether the declaration provider has a specific implementation of computePackageNamesWithTopLevelClassifiers. This allows the Analysis API backend to determine whether classifier package sets are computed and cached separately or with computePackageNames.

Functions

computePackageNames

Calculates the set of package names which can be provided by this declaration provider.

The set may contain false positives. null may be returned if the package set is too expensive or impossible to compute.

computePackageNames is used as the default implementation for computePackageNamesWithTopLevelClassifiers and computePackageNamesWithTopLevelCallables if either returns null. It depends on the declaration provider whether it's worth computing separate package sets for classifiers and callables, or just one set containing all package names.

computePackageNamesWithTopLevelCallables

Calculates the set of package names which contain callables and can be provided by this declaration provider.

The set may contain false positives. null may be returned if the package set is too expensive or impossible to compute.

computePackageNamesWithTopLevelClassifiers

Calculates the set of package names which contain classifiers and can be provided by this declaration provider.

The set may contain false positives. null may be returned if the package set is too expensive or impossible to compute.

findFilesForFacade

abstract fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile>

findFilesForFacadeByPackage

abstract fun findFilesForFacadeByPackage(packageFqName: FqName): Collection<KtFile>

findFilesForScript

abstract fun findFilesForScript(scriptFqName: FqName): Collection<KtScript>

findInternalFilesForFacade

abstract fun findInternalFilesForFacade(facadeFqName: FqName): Collection<KtFile>

Currently we want only classes from libraries.

getAllClassesByClassId

abstract fun getAllClassesByClassId(classId: ClassId): Collection<KtClassOrObject>

getAllTypeAliasesByClassId

abstract fun getAllTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias>

getClassLikeDeclarationByClassId

abstract fun getClassLikeDeclarationByClassId(classId: ClassId): KtClassLikeDeclaration?

Returns a KtClassLikeDeclaration that has the given classId, or null if no such declaration exists.

getClassLikeDeclarationByClassId does not guarantee a stable result for ambiguous class IDs. If multiple declarations share the same class ID, the declaration provider may return any one of them. To get a list of all possibilities, getAllClassesByClassId and getAllTypeAliasesByClassId should be used instead.

getTopLevelCallableFiles

abstract fun getTopLevelCallableFiles(callableId: CallableId): Collection<KtFile>

getTopLevelCallableNamesInPackage

abstract fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name>

getTopLevelFunctions

abstract fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction>

getTopLevelKotlinClassLikeDeclarationNamesInPackage

abstract fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name>

getTopLevelProperties

abstract fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty>