-
Notifications
You must be signed in to change notification settings - Fork 275
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...lin/com/google/devtools/ksp/analysisapi/providers/IncrementalKotlinDeclarationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../kotlin/com/google/devtools/ksp/analysisapi/providers/IncrementalKotlinPackageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.