All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.analysis.providers

import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*

/**
 * A declaration provider for a given scope. Can be created via [KotlinDeclarationProviderFactory].
 * May be called frequently, so for implementations it is better to cache results.
 */
public abstract class KotlinDeclarationProvider {
    public abstract fun getClassLikeDeclarationByClassId(classId: ClassId): KtClassLikeDeclaration?

    public abstract fun getAllClassesByClassId(classId: ClassId): Collection
    public abstract fun getAllTypeAliasesByClassId(classId: ClassId): Collection

    public abstract fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set

    public abstract fun getTopLevelProperties(callableId: CallableId): Collection
    public abstract fun getTopLevelFunctions(callableId: CallableId): Collection

    public abstract fun getTopLevelCallableFiles(callableId: CallableId): Collection

    public abstract fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set

    public abstract fun findFilesForFacadeByPackage(packageFqName: FqName): Collection

    public abstract fun findFilesForFacade(facadeFqName: FqName): Collection

    /**
     * Currently we want only classes from libraries ([org.jetbrains.kotlin.analysis.decompiler.psi.file.KtClsFile])
     */
    public abstract fun findInternalFilesForFacade(facadeFqName: FqName): Collection

    public abstract fun findFilesForScript(scriptFqName: FqName): Collection

    public abstract fun computePackageSetWithTopLevelCallableDeclarations(): Set
}

public abstract class KotlinDeclarationProviderFactory {
    public abstract fun createDeclarationProvider(scope: GlobalSearchScope, contextualModule: KtModule?): KotlinDeclarationProvider

    public companion object {
        public fun getInstance(project: Project): KotlinDeclarationProviderFactory =
            project.getService(KotlinDeclarationProviderFactory::class.java)
    }
}

/**
 * While the main declaration provider created via [createDeclarationProvider] is scope-based, there are other declaration providers which
 * are not, such as file-based declaration providers. Not all declarations provided by such declaration providers can be provided by the
 * main declaration provider, even if the correct scope is provided (such as a file-based scope). For example, the main declaration provider
 * may be based on an index which doesn't contain the declarations provided by file-based declaration providers.
 *
 * Hence, [KotlinDeclarationProvider]s cannot just be combined by combining the scopes of all declaration providers and calling
 * [createDeclarationProvider]. [KotlinDeclarationProviderMerger] should implement proper merging logic that takes these concerns into
 * account.
 */
public abstract class KotlinDeclarationProviderMerger {
    /**
     * Merges [declarationProviders] if possible, creating a combined declaration provider that should be more efficient compared to calling
     * separate declaration providers. Not all given declaration providers might be mergeable, or there might be multiple separate sets of
     * declaration providers which can be merged individually, so the resulting declaration provider may be a composite Kotlin declaration
     * provider.
     */
    public abstract fun mergeDeclarationProviders(declarationProviders: List): KotlinDeclarationProvider

    public companion object {
        public fun getInstance(project: Project): KotlinDeclarationProviderMerger =
            project.getService(KotlinDeclarationProviderMerger::class.java)
    }
}

public fun Project.createDeclarationProvider(scope: GlobalSearchScope, module: KtModule?): KotlinDeclarationProvider =
    KotlinDeclarationProviderFactory.getInstance(this).createDeclarationProvider(scope, module)

public fun Project.mergeDeclarationProviders(declarationProviders: List): KotlinDeclarationProvider =
    KotlinDeclarationProviderMerger.getInstance(this).mergeDeclarationProviders(declarationProviders)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy