Repository navigation
ADFA-6857: Search only the base class's package for sealed inheritors #2239
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9fd8c32
ADFA-6857: Stop DirectInheritorsProvider pinning every source file's PSI
itsaky-adfa 28c3214
ADFA-6857: Search only the base class's package for sealed inheritors
itsaky-adfa fa5fceb
ADFA-6857: Use kt-android v2.3.255-b006e8c for same-package inheritors
itsaky-adfa 8317252
fix(zipfs2): synthesize JarListing ancestors iteratively
itsaky-adfa d40ff3f
fix(projects): skip a classpath JAR whose file system is not found
itsaky-adfa f3cb4a0
fix(lsp): bound Kotlin JAR entry buffers before allocating them
itsaky-adfa 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
82 changes: 82 additions & 0 deletions
82
...t/java/com/itsaky/androidide/lsp/kotlin/compiler/services/DirectInheritorsProviderTest.kt
This file contains hidden or 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,82 @@ | ||
| package com.itsaky.androidide.lsp.kotlin.compiler.services | ||
|
|
||
| import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest | ||
| import org.jetbrains.kotlin.analysis.api.platform.declarations.KotlinDirectInheritorsProvider | ||
| import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider | ||
| import org.jetbrains.kotlin.com.intellij.psi.impl.source.PsiFileImpl | ||
| import org.jetbrains.kotlin.com.intellij.psi.search.GlobalSearchScope | ||
| import org.jetbrains.kotlin.psi.KtClass | ||
| import org.jetbrains.kotlin.psi.KtClassOrObject | ||
| import org.jetbrains.kotlin.psi.KtFile | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Test | ||
|
|
||
| class DirectInheritorsProviderTest : KtLspTest() { | ||
| @Test | ||
| fun `same-package search finds inheritors declared in other files of the base class's package`() { | ||
| val base = createSourceFile("p/Base.kt", "package p\nsealed class Base") | ||
| createSourceFile("p/A.kt", "package p\nclass A : Base()") | ||
| createSourceFile("p/Outer.kt", "package p\nclass Outer { class B : Base() }") | ||
| createSourceFile("p/Unrelated.kt", "package p\nclass Unrelated : Any()") | ||
|
|
||
| assertEquals(listOf("p.A", "p.Outer.B"), samePackageInheritorsOf(base, "Base")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `same-package search finds inheritors that extend the base class through a type alias from another package`() { | ||
| val base = createSourceFile("p/Base.kt", "package p\nsealed class Base") | ||
| createSourceFile("q/Alias.kt", "package q\ntypealias Aliased = p.Base") | ||
| createSourceFile("p/ViaAlias.kt", "package p\nimport q.Aliased\nclass ViaAlias : Aliased()") | ||
|
|
||
| assertEquals(listOf("p.ViaAlias"), samePackageInheritorsOf(base, "Base")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `same-package search does not load files outside the base class's package`() { | ||
| val base = createSourceFile("p/Base.kt", "package p\nsealed class Base") | ||
| // Not registered with the file index, so nothing but a whole-module walk can reach it. | ||
| val otherPackage = env.createFile("src", "q/Other.kt", "package q\nclass Other : Any()") as PsiFileImpl | ||
| assertNull(otherPackage.treeElement) | ||
|
|
||
| samePackageInheritorsOf(base, "Base") | ||
|
|
||
| assertNull(otherPackage.treeElement) | ||
| } | ||
|
|
||
| @Test | ||
| fun `full search still finds inheritors in other packages`() { | ||
| val base = createSourceFile("p/Base.kt", "package p\nopen class Base") | ||
| createSourceFile("p/A.kt", "package p\nclass A : Base()") | ||
| createSourceFile("q/B.kt", "package q\nclass B : p.Base()") | ||
|
|
||
| val all = inheritorsOf(base, "Base") { ktClass, scope -> getDirectKotlinInheritors(ktClass, scope, false) } | ||
|
|
||
| assertEquals(listOf("p.A", "q.B"), all) | ||
| assertEquals(listOf("p.A"), samePackageInheritorsOf(base, "Base")) | ||
| } | ||
|
|
||
| private fun samePackageInheritorsOf( | ||
| file: KtFile, | ||
| className: String, | ||
| ): List<String> = | ||
| inheritorsOf(file, className) { ktClass, scope -> | ||
| getSamePackageDirectKotlinInheritors(ktClass, scope, false) | ||
| } | ||
|
|
||
| private fun inheritorsOf( | ||
| file: KtFile, | ||
| className: String, | ||
| search: KotlinDirectInheritorsProvider.(KtClass, GlobalSearchScope) -> Iterable<KtClassOrObject>, | ||
| ): List<String> { | ||
| val ktClass = file.declarations.filterIsInstance<KtClass>().single { it.name == className } | ||
| return analyzeMaybeDanglingForTest(file) { | ||
| val scope = KotlinProjectStructureProvider.getModule(env.project, ktClass, useSiteModule = null).contentScope | ||
| KotlinDirectInheritorsProvider | ||
| .getInstance(env.project) | ||
| .search(ktClass, scope) | ||
| .map { it.fqName!!.asString() } | ||
| .sorted() | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.