Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple-round: implement incremental providers #1494

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.google.devtools.ksp.analysisapi.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.analysis.providers.KotlinDeclarationProvider
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProviderFactory
import org.jetbrains.kotlin.analysis.providers.impl.KotlinStaticDeclarationProviderFactory
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.KtClassLikeDeclaration
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtScript
import org.jetbrains.kotlin.psi.KtTypeAlias

class IncrementalKotlinDeclarationProvider(var del: KotlinDeclarationProvider) : KotlinDeclarationProvider() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are purely delegating here, it might make more sense to simply use delegation without need to write down all the redundant overrides here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, by delegation doesn't allow swapping the delegate.

override fun computePackageSetWithTopLevelCallableDeclarations(): Set<String> {
return del.computePackageSetWithTopLevelCallableDeclarations()
}

override fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile> {
return del.findFilesForFacade(facadeFqName)
}

override fun findFilesForFacadeByPackage(packageFqName: FqName): Collection<KtFile> {
return del.findFilesForFacadeByPackage(packageFqName)
}

override fun findFilesForScript(scriptFqName: FqName): Collection<KtScript> {
return del.findFilesForScript(scriptFqName)
}

override fun findInternalFilesForFacade(facadeFqName: FqName): Collection<KtFile> {
return del.findInternalFilesForFacade(facadeFqName)
}

override fun getAllClassesByClassId(classId: ClassId): Collection<KtClassOrObject> {
return del.getAllClassesByClassId(classId)
}

override fun getAllTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias> {
return del.getAllTypeAliasesByClassId(classId)
}

override fun getClassLikeDeclarationByClassId(classId: ClassId): KtClassLikeDeclaration? {
return del.getClassLikeDeclarationByClassId(classId)
}

override fun getTopLevelCallableFiles(callableId: CallableId): Collection<KtFile> {
return del.getTopLevelCallableFiles(callableId)
}

override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> {
return del.getTopLevelCallableNamesInPackage(packageFqName)
}

override fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction> {
return del.getTopLevelFunctions(callableId)
}

override fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name> {
return del.getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName)
}

override fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty> {
return del.getTopLevelProperties(callableId)
}
}

class IncrementalKotlinDeclarationProviderFactory(
private val project: Project,
) : KotlinDeclarationProviderFactory() {
private var provider: IncrementalKotlinDeclarationProvider? = null
private lateinit var scope: GlobalSearchScope
private var contextualModule: KtModule? = null
private var files: Collection<KtFile> = emptyList()

override fun createDeclarationProvider(
scope: GlobalSearchScope,
contextualModule: KtModule?
): KotlinDeclarationProvider {
this.scope = scope
this.contextualModule = contextualModule
return IncrementalKotlinDeclarationProvider(createDelegateProvider()).also {
provider = it
}
}

fun update(files: Collection<KtFile>) {
this.files = files
provider?.let {
it.del = createDelegateProvider()
}
}

private fun createDelegateProvider(): KotlinDeclarationProvider {
val staticFactory = KotlinStaticDeclarationProviderFactory(project, files)
return staticFactory.createDeclarationProvider(scope, contextualModule)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.google.devtools.ksp.analysisapi.providers

import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProvider
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProviderFactory
import org.jetbrains.kotlin.analysis.providers.impl.KotlinStaticPackageProviderFactory
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.psi.KtFile

class IncrementalKotlinPackageProvider(var del: KotlinPackageProvider) : KotlinPackageProvider() {
override fun doesKotlinOnlyPackageExist(packageFqName: FqName): Boolean {
return del.doesKotlinOnlyPackageExist(packageFqName)
}

override fun doesPackageExist(packageFqName: FqName, platform: TargetPlatform): Boolean {
return del.doesPackageExist(packageFqName, platform)
}

override fun doesPlatformSpecificPackageExist(packageFqName: FqName, platform: TargetPlatform): Boolean {
return del.doesPlatformSpecificPackageExist(packageFqName, platform)
}

override fun getKotlinOnlySubPackagesFqNames(packageFqName: FqName, nameFilter: (Name) -> Boolean): Set<Name> {
return del.getKotlinOnlySubPackagesFqNames(packageFqName, nameFilter)
}

override fun getPlatformSpecificSubPackagesFqNames(
packageFqName: FqName,
platform: TargetPlatform,
nameFilter: (Name) -> Boolean
): Set<Name> {
return del.getPlatformSpecificSubPackagesFqNames(packageFqName, platform, nameFilter)
}

override fun getSubPackageFqNames(
packageFqName: FqName,
platform: TargetPlatform,
nameFilter: (Name) -> Boolean
): Set<Name> {
return del.getSubPackageFqNames(packageFqName, platform, nameFilter)
}
}

class IncrementalKotlinPackageProviderFactory(
private val project: Project,
) : KotlinPackageProviderFactory() {
private var provider: IncrementalKotlinPackageProvider? = null
private lateinit var scope: GlobalSearchScope
private var files: Collection<KtFile> = emptyList()

override fun createPackageProvider(searchScope: GlobalSearchScope): KotlinPackageProvider {
this.scope = searchScope
return IncrementalKotlinPackageProvider(createDelegateProvider()).also {
provider = it
}
}

fun update(files: Collection<KtFile>) {
this.files = files
provider?.let {
it.del = createDelegateProvider()
}
}

private fun createDelegateProvider(): KotlinPackageProvider {
val staticFactory = KotlinStaticPackageProviderFactory(project, files)
return staticFactory.createPackageProvider(scope)
}
}
Loading
Loading