From f27afcf8130fe187986661c76e7bc2818bdbcd68 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 18 Nov 2026 19:25:06 +0530 Subject: [PATCH 1/6] style: spotless reformat, no functional change --- .../kotlin/utils/SymbolVisibilityChecker.kt | 15 +- .../utils/SymbolVisibilityCheckerTest.kt | 331 +++++++++--------- 2 files changed, 176 insertions(+), 170 deletions(-) diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt index 69c4c612c2..46168b24b7 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt @@ -24,8 +24,9 @@ internal class SymbolVisibilityChecker( useSiteModule: KaModule, useSitePackage: String? = null, ): Boolean { - val declaringModule = structureProvider.findModuleForSourceId(symbol.sourceId) - ?: return false + val declaringModule = + structureProvider.findModuleForSourceId(symbol.sourceId) + ?: return false if (!isReachable(useSiteModule, declaringModule)) return false if (!arePlatformCompatible(useSiteModule, declaringModule)) return false @@ -34,7 +35,10 @@ internal class SymbolVisibilityChecker( return true } - fun isReachable(useSiteModule: KaModule, declaringModule: KaModule): Boolean { + fun isReachable( + useSiteModule: KaModule, + declaringModule: KaModule, + ): Boolean { if (useSiteModule == declaringModule) return true if (moduleVisibilityCache[useSiteModule]?.contains(declaringModule) == true) return true @@ -54,7 +58,10 @@ internal class SymbolVisibilityChecker( return false } - fun arePlatformCompatible(useSiteModule: KaModule, declaringModule: KaModule): Boolean { + fun arePlatformCompatible( + useSiteModule: KaModule, + declaringModule: KaModule, + ): Boolean { val usePlatform = useSiteModule.targetPlatform val declPlatform = declaringModule.targetPlatform diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerTest.kt index c6fe6721a6..6b8aa1728e 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerTest.kt @@ -21,170 +21,169 @@ import org.junit.runners.JUnit4 */ @RunWith(JUnit4::class) class SymbolVisibilityCheckerTest { - - // Use relaxed mocks so that no stubs are required for unused methods - private val structureProvider = mockk(relaxed = true) - private val checker = SymbolVisibilityChecker(structureProvider) - - // Two distinct modules - private val moduleA: KaModule = mockk(relaxed = true) - private val moduleB: KaModule = mockk(relaxed = true) - - private fun symbol( - visibility: JvmVisibility, - pkg: String = "com.example", - sourceId: String = "jar-a", - ) = JvmSymbol( - key = "com/example/Foo", - sourceId = sourceId, - name = "com/example/Foo", - shortName = "Foo", - packageName = pkg, - kind = JvmSymbolKind.CLASS, - language = JvmSourceLanguage.KOTLIN, - visibility = visibility, - data = JvmClassInfo(), - ) - - @Test - fun `PUBLIC symbol is always visible regardless of module`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PUBLIC), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = "other.pkg", - ) - ).isTrue() - } - - @Test - fun `PUBLIC symbol is visible even with null useSitePackage`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PUBLIC), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = null, - ) - ).isTrue() - } - - @Test - fun `PRIVATE symbol is never visible`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PRIVATE), - useSiteModule = moduleA, - declaringModule = moduleB, - ) - ).isFalse() - } - - @Test - fun `PRIVATE symbol is not visible even from same module`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PRIVATE), - useSiteModule = moduleA, - declaringModule = moduleA, // same module - ) - ).isFalse() - } - - @Test - fun `INTERNAL symbol is visible from same module`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.INTERNAL), - useSiteModule = moduleA, - declaringModule = moduleA, - ) - ).isTrue() - } - - @Test - fun `INTERNAL symbol is not visible from different module`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.INTERNAL), - useSiteModule = moduleA, - declaringModule = moduleB, - ) - ).isFalse() - } - - @Test - fun `PACKAGE_PRIVATE symbol is visible within same package`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = "com.example", - ) - ).isTrue() - } - - @Test - fun `PACKAGE_PRIVATE symbol is not visible from different package`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = "org.other", - ) - ).isFalse() - } - - @Test - fun `PACKAGE_PRIVATE symbol is not visible when useSitePackage is null`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = null, - ) - ).isFalse() - } - - @Test - fun `PROTECTED symbol is visible within same package`() { - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PROTECTED, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = "com.example", - ) - ).isTrue() - } - - @Test - fun `PROTECTED symbol is visible as assumed descendant across packages`() { - // isDescendant is hardcoded to true in the current implementation - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PROTECTED, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = "different.pkg", - ) - ).isTrue() - } - - @Test - fun `PROTECTED symbol is visible with null useSitePackage due to descendant assumption`() { - // isSamePackage=false but isDescendant=true → visible - assertThat( - checker.isDeclarationVisible( - symbol(JvmVisibility.PROTECTED, pkg = "com.example"), - useSiteModule = moduleA, - declaringModule = moduleB, - useSitePackage = null, - ) - ).isTrue() - } + // Use relaxed mocks so that no stubs are required for unused methods + private val structureProvider = mockk(relaxed = true) + private val checker = SymbolVisibilityChecker(structureProvider) + + // Two distinct modules + private val moduleA: KaModule = mockk(relaxed = true) + private val moduleB: KaModule = mockk(relaxed = true) + + private fun symbol( + visibility: JvmVisibility, + pkg: String = "com.example", + sourceId: String = "jar-a", + ) = JvmSymbol( + key = "com/example/Foo", + sourceId = sourceId, + name = "com/example/Foo", + shortName = "Foo", + packageName = pkg, + kind = JvmSymbolKind.CLASS, + language = JvmSourceLanguage.KOTLIN, + visibility = visibility, + data = JvmClassInfo(), + ) + + @Test + fun `PUBLIC symbol is always visible regardless of module`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PUBLIC), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = "other.pkg", + ), + ).isTrue() + } + + @Test + fun `PUBLIC symbol is visible even with null useSitePackage`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PUBLIC), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = null, + ), + ).isTrue() + } + + @Test + fun `PRIVATE symbol is never visible`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PRIVATE), + useSiteModule = moduleA, + declaringModule = moduleB, + ), + ).isFalse() + } + + @Test + fun `PRIVATE symbol is not visible even from same module`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PRIVATE), + useSiteModule = moduleA, + declaringModule = moduleA, // same module + ), + ).isFalse() + } + + @Test + fun `INTERNAL symbol is visible from same module`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.INTERNAL), + useSiteModule = moduleA, + declaringModule = moduleA, + ), + ).isTrue() + } + + @Test + fun `INTERNAL symbol is not visible from different module`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.INTERNAL), + useSiteModule = moduleA, + declaringModule = moduleB, + ), + ).isFalse() + } + + @Test + fun `PACKAGE_PRIVATE symbol is visible within same package`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = "com.example", + ), + ).isTrue() + } + + @Test + fun `PACKAGE_PRIVATE symbol is not visible from different package`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = "org.other", + ), + ).isFalse() + } + + @Test + fun `PACKAGE_PRIVATE symbol is not visible when useSitePackage is null`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PACKAGE_PRIVATE, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = null, + ), + ).isFalse() + } + + @Test + fun `PROTECTED symbol is visible within same package`() { + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PROTECTED, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = "com.example", + ), + ).isTrue() + } + + @Test + fun `PROTECTED symbol is visible as assumed descendant across packages`() { + // isDescendant is hardcoded to true in the current implementation + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PROTECTED, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = "different.pkg", + ), + ).isTrue() + } + + @Test + fun `PROTECTED symbol is visible with null useSitePackage due to descendant assumption`() { + // isSamePackage=false but isDescendant=true → visible + assertThat( + checker.isDeclarationVisible( + symbol(JvmVisibility.PROTECTED, pkg = "com.example"), + useSiteModule = moduleA, + declaringModule = moduleB, + useSitePackage = null, + ), + ).isTrue() + } } From ab3a7cd226190c53d9175b6f1cc5bb48ac0129ff Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Tue, 17 Nov 2026 21:45:36 +0530 Subject: [PATCH 2/6] perf(lsp-kotlin): memoize module lookups by source id and key the walk by identity --- .../services/ProjectStructureProvider.kt | 38 +++++- .../services/SourceModuleLookupMemo.kt | 46 +++++++ ...rojectStructureProviderModuleLookupTest.kt | 125 ++++++++++++++++++ 3 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/SourceModuleLookupMemo.kt create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProviderModuleLookupTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt index a744793ceb..cafec8e974 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProvider.kt @@ -1,10 +1,10 @@ package com.itsaky.androidide.lsp.kotlin.compiler.services +import androidx.annotation.VisibleForTesting import com.itsaky.androidide.lsp.kotlin.compiler.index.KtSymbolIndex import com.itsaky.androidide.lsp.kotlin.compiler.modules.KtModule import com.itsaky.androidide.lsp.kotlin.compiler.modules.NotUnderContentRootModule import com.itsaky.androidide.lsp.kotlin.compiler.modules.backingFilePath -import org.jetbrains.kotlin.analysis.api.KaExperimentalApi import org.jetbrains.kotlin.analysis.api.KaPlatformInterface import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProvider import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinProjectStructureProviderBase @@ -19,6 +19,8 @@ import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFile import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.KtFile import java.nio.file.Paths +import java.util.Collections +import java.util.IdentityHashMap import java.util.concurrent.ConcurrentHashMap import kotlin.io.path.pathString @@ -28,8 +30,21 @@ internal class ProjectStructureProvider : companion object { fun getInstance(project: Project): ProjectStructureProvider = KotlinProjectStructureProvider.getInstance(project) as ProjectStructureProvider + + /** + * Distinct source ids are the library JAR paths plus the source files looked up. A workspace the + * size of CodeOnTheGo's own (about 180 dependencies of `:app`, about 2,000 source files) fits, and + * an entry holds only two references, so the bound costs well under a megabyte. + */ + const val MAX_MEMOIZED_MODULE_LOOKUPS = 4096 } + @Volatile + private var moduleLookups = SourceModuleLookupMemo(MAX_MEMOIZED_MODULE_LOOKUPS) + + @VisibleForTesting + internal fun memoizedModuleLookupCount(): Int = moduleLookups.size + private lateinit var modules: List private lateinit var project: Project @@ -69,6 +84,9 @@ internal class ProjectStructureProvider : ) { this.modules = modules this.project = project + // A new memo rather than a cleared one: a lookup still walking the previous graph writes into the + // memo it started with, which is discarded here. + moduleLookups = SourceModuleLookupMemo(MAX_MEMOIZED_MODULE_LOOKUPS) } override fun getModule( @@ -135,15 +153,25 @@ internal class ProjectStructureProvider : * The search is recursive: if the top-level modules do not match, their transitive dependencies * are checked as well. * + * Completion calls this once per unimported-symbol candidate, so results are memoized, misses + * included: a miss is the costliest lookup, a walk of the whole graph. Memoizing is sound because + * an environment's module graph is fixed for its lifetime: `AbstractCompilationEnvironment` builds + * `modules` once, during initialization; [KtModule.contentRoots] are immutable; and a workspace + * change builds a new compiler, whose environment calls [setupWith] again, which starts a new memo. + * * @return The declaring [KaModule], or `null` if none is found. */ - @OptIn(KaExperimentalApi::class) - fun findModuleForSourceId(sourceId: String): KaModule? { + fun findModuleForSourceId(sourceId: String): KaModule? = moduleLookups.lookup(sourceId, ::searchModuleForSourceId) + + private fun searchModuleForSourceId(sourceId: String): KaModule? { val path = Paths.get(sourceId) - val visited = mutableSetOf() + // By identity, not by moduleDescription: the description is diagnostic text rebuilt on every read, + // joining every dependency's id, and reading it per visited module per lookup was most of the + // lookup's cost (ADFA-6855 profiling). + val visited = Collections.newSetFromMap(IdentityHashMap()) fun search(module: KaModule): KaModule? { - if (!visited.add(module.moduleDescription)) return null + if (!visited.add(module)) return null if (module is KtModule) { val roots = module.contentRoots if (roots.contains(path) || roots.any { path.startsWith(it) }) return module diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/SourceModuleLookupMemo.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/SourceModuleLookupMemo.kt new file mode 100644 index 0000000000..3b48e81bb6 --- /dev/null +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/SourceModuleLookupMemo.kt @@ -0,0 +1,46 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.services + +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule + +/** + * A bounded memo of source id to declaring module, misses included, evicting the least recently used + * entry once it holds [capacity] of them. + * + * Modules are held strongly. Every module it can hold comes from its provider's module graph, which + * the provider itself holds for as long as the memo is in use, so a weak reference would never be + * cleared; the graph and the memo are dropped together. + * + * The lock guards only the map. [lookup] computes a missing entry outside it, so two threads may + * compute the same entry; both arrive at the same module, and the later write is harmless. + */ +internal class SourceModuleLookupMemo( + private val capacity: Int, +) { + private val lock = Any() + + private val entries = + object : LinkedHashMap(INITIAL_CAPACITY, LOAD_FACTOR, true) { + override fun removeEldestEntry(eldest: MutableMap.MutableEntry): Boolean = size > capacity + } + + val size: Int + get() = synchronized(lock) { entries.size } + + /** The module memoized for [sourceId], computing and memoizing it with [compute] when absent. */ + fun lookup( + sourceId: String, + compute: (String) -> KaModule?, + ): KaModule? { + synchronized(lock) { + if (entries.containsKey(sourceId)) return entries[sourceId] + } + val module = compute(sourceId) + synchronized(lock) { entries[sourceId] = module } + return module + } + + private companion object { + const val INITIAL_CAPACITY = 256 + const val LOAD_FACTOR = 0.75f + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProviderModuleLookupTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProviderModuleLookupTest.kt new file mode 100644 index 0000000000..1e324db255 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/services/ProjectStructureProviderModuleLookupTest.kt @@ -0,0 +1,125 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.services + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.lsp.kotlin.compiler.index.KtSymbolIndex +import com.itsaky.androidide.lsp.kotlin.compiler.modules.KtModule +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.jetbrains.kotlin.analysis.api.KaExperimentalApi +import org.jetbrains.kotlin.com.intellij.mock.MockProject +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import java.nio.file.Path +import java.nio.file.Paths + +/** Requirements for [ProjectStructureProvider.findModuleForSourceId], the per-candidate lookup completion relies on. */ +@RunWith(JUnit4::class) +class ProjectStructureProviderModuleLookupTest { + private val provider = ProjectStructureProvider() + + @OptIn(KaExperimentalApi::class) + private fun module( + name: String, + roots: Set, + deps: List = emptyList(), + descriptionReadable: Boolean = true, + ): KtModule = + mockk(name = name) { + every { contentRoots } returns roots + every { directRegularDependencies } returns deps + if (descriptionReadable) { + every { moduleDescription } returns "module '$name'" + } else { + every { moduleDescription } throws AssertionError("module lookup read the description of $name") + } + } + + private fun setUp(vararg modules: KtModule) { + provider.setupWith( + project = mockk(relaxed = true), + index = mockk(relaxed = true), + modules = modules.toList(), + libraryRoots = emptyList(), + ) + } + + private val jarA = Paths.get("/libs/a.jar") + private val jarB = Paths.get("/libs/b.jar") + private val appSources = Paths.get("/project/app/src/main/java") + + @Test + fun `module lookup does not read module descriptions`() { + val libB = module("libB", setOf(jarB), descriptionReadable = false) + val libA = module("libA", setOf(jarA), deps = listOf(libB), descriptionReadable = false) + setUp(module("app", setOf(appSources), deps = listOf(libA), descriptionReadable = false)) + + assertThat(provider.findModuleForSourceId(jarB.toString())).isSameInstanceAs(libB) + } + + @Test + fun `module lookup returns the first module in dependency order whose roots contain the path`() { + val libA = module("libA", setOf(jarA)) + val shadowingLibA = module("shadowingLibA", setOf(jarA)) + val app = module("app", setOf(appSources), deps = listOf(libA, shadowingLibA)) + setUp(app) + + assertThat(provider.findModuleForSourceId(jarA.toString())).isSameInstanceAs(libA) + assertThat(provider.findModuleForSourceId(appSources.resolve("com/example/Main.kt").toString())) + .isSameInstanceAs(app) + } + + @Test + fun `module lookup returns null for a path no module contains, on every call`() { + setUp(module("app", setOf(appSources), deps = listOf(module("libA", setOf(jarA))))) + + assertThat(provider.findModuleForSourceId("/elsewhere/c.jar")).isNull() + assertThat(provider.findModuleForSourceId("/elsewhere/c.jar")).isNull() + } + + @Test + fun `a repeated module lookup does not walk the module graph again`() { + val libA = module("libA", setOf(jarA)) + setUp(module("app", setOf(appSources), deps = listOf(libA))) + + provider.findModuleForSourceId(jarA.toString()) + provider.findModuleForSourceId(jarA.toString()) + + verify(exactly = 1) { libA.contentRoots } + } + + @Test + fun `a repeated lookup of a path no module contains does not walk the module graph again`() { + val libA = module("libA", setOf(jarA)) + setUp(module("app", setOf(appSources), deps = listOf(libA))) + + provider.findModuleForSourceId("/elsewhere/c.jar") + provider.findModuleForSourceId("/elsewhere/c.jar") + + verify(exactly = 1) { libA.contentRoots } + } + + @Test + fun `setupWith discards module lookups made against the previous module graph`() { + setUp(module("app", setOf(appSources), deps = listOf(module("oldLibA", setOf(jarA))))) + provider.findModuleForSourceId(jarA.toString()) + + val newLibA = module("newLibA", setOf(jarA)) + setUp(module("app", setOf(appSources), deps = listOf(newLibA))) + + assertThat(provider.findModuleForSourceId(jarA.toString())).isSameInstanceAs(newLibA) + } + + @Test + fun `memoized module lookups stay within their bound`() { + setUp(module("app", setOf(appSources))) + + repeat(ProjectStructureProvider.MAX_MEMOIZED_MODULE_LOOKUPS + 100) { i -> + provider.findModuleForSourceId("/elsewhere/$i.jar") + } + + assertThat(provider.memoizedModuleLookupCount()) + .isAtMost(ProjectStructureProvider.MAX_MEMOIZED_MODULE_LOOKUPS) + } +} From 5d7d022a9dea96e4c78f06b0c3fa1e9dddb13d9e Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Tue, 17 Nov 2026 21:47:41 +0530 Subject: [PATCH 3/6] perf(lsp-kotlin): compute a completion's reachable modules once, not per candidate symbol --- .../kotlin/completion/KotlinCompletions.kt | 13 +-- .../kotlin/utils/SymbolVisibilityChecker.kt | 59 +++++++------ ...SymbolVisibilityCheckerReachabilityTest.kt | 82 +++++++++++++++++++ 3 files changed, 120 insertions(+), 34 deletions(-) create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerReachabilityTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt index bf257190c7..c5d79071b3 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt @@ -428,22 +428,13 @@ private fun KaSession.collectUnimportedSymbols(to: MutableList) ctx.ktElement.containingKtFile.packageDirective ?.fqName ?.asString() - val useSiteModule = this.useSiteModule - val visibilityChecker = env.symbolVisibilityChecker + val visibility = env.symbolVisibilityChecker.forUseSite(this.useSiteModule) fun addCompletionItem(symbol: JvmSymbol): Boolean { abortIfCancelled() if (symbol.packageName == currentPackage) return false - - val isVisible = - visibilityChecker.isVisible( - symbol = symbol, - useSiteModule = useSiteModule, - useSitePackage = currentPackage, - ) - - if (!isVisible) return false + if (!visibility.isVisible(symbol, useSitePackage = currentPackage)) return false val item = buildUnimportedSymbolItem(symbol) ?: return false to += item diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt index 46168b24b7..e41f6ec87d 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityChecker.kt @@ -6,7 +6,6 @@ import org.appdevforall.codeonthego.indexing.jvm.JvmVisibility import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule import org.jetbrains.kotlin.analysis.api.projectStructure.allDirectDependencies import org.slf4j.LoggerFactory -import java.util.concurrent.ConcurrentHashMap internal class SymbolVisibilityChecker( private val structureProvider: ProjectStructureProvider, @@ -15,47 +14,61 @@ internal class SymbolVisibilityChecker( private val logger = LoggerFactory.getLogger(SymbolVisibilityChecker::class.java) } - // visibility check cache, for memoization - // useSiteModule -> list of modules visible from useSiteModule - private val moduleVisibilityCache = ConcurrentHashMap>() + /** + * Visibility checks from one use site, with the modules reachable from it computed once. + * + * Held per request rather than cached on the checker: completion's use site is the dangling module + * of its placeholder copy, a new instance per request, so a cache keyed on it would gain an entry + * per completion and never be hit. + */ + inner class UseSiteSymbolVisibility internal constructor( + private val useSiteModule: KaModule, + ) { + private val reachableModules: Set by lazy { modulesReachableFrom(useSiteModule) } + + fun isVisible( + symbol: JvmSymbol, + useSitePackage: String?, + ): Boolean { + val declaringModule = + structureProvider.findModuleForSourceId(symbol.sourceId) + ?: return false + + if (declaringModule !in reachableModules) return false + if (!arePlatformCompatible(useSiteModule, declaringModule)) return false + if (!isDeclarationVisible(symbol, useSiteModule, declaringModule, useSitePackage)) return false + + return true + } + } + + /** Checks for many symbols from [useSiteModule]; walks its dependency graph once for all of them. */ + fun forUseSite(useSiteModule: KaModule) = UseSiteSymbolVisibility(useSiteModule) fun isVisible( symbol: JvmSymbol, useSiteModule: KaModule, useSitePackage: String? = null, - ): Boolean { - val declaringModule = - structureProvider.findModuleForSourceId(symbol.sourceId) - ?: return false - - if (!isReachable(useSiteModule, declaringModule)) return false - if (!arePlatformCompatible(useSiteModule, declaringModule)) return false - if (!isDeclarationVisible(symbol, useSiteModule, declaringModule, useSitePackage)) return false - - return true - } + ): Boolean = forUseSite(useSiteModule).isVisible(symbol, useSitePackage) fun isReachable( useSiteModule: KaModule, declaringModule: KaModule, - ): Boolean { - if (useSiteModule == declaringModule) return true - if (moduleVisibilityCache[useSiteModule]?.contains(declaringModule) == true) return true + ): Boolean = declaringModule in modulesReachableFrom(useSiteModule) - // walk the dependency graph + /** [start] and every module reachable through its dependencies of any kind. */ + private fun modulesReachableFrom(start: KaModule): Set { val visited = mutableSetOf() val queue = ArrayDeque() - queue.add(useSiteModule) + queue.add(start) while (queue.isNotEmpty()) { val current = queue.removeFirst() if (!visited.add(current)) continue - if (current == declaringModule) return true - queue.addAll(current.allDirectDependencies()) } - return false + return visited } fun arePlatformCompatible( diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerReachabilityTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerReachabilityTest.kt new file mode 100644 index 0000000000..3ad97b072c --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/SymbolVisibilityCheckerReachabilityTest.kt @@ -0,0 +1,82 @@ +package com.itsaky.androidide.lsp.kotlin.utils + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.lsp.kotlin.compiler.services.ProjectStructureProvider +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.appdevforall.codeonthego.indexing.jvm.JvmClassInfo +import org.appdevforall.codeonthego.indexing.jvm.JvmSourceLanguage +import org.appdevforall.codeonthego.indexing.jvm.JvmSymbol +import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolKind +import org.appdevforall.codeonthego.indexing.jvm.JvmVisibility +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule +import org.jetbrains.kotlin.platform.jvm.JvmPlatforms +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +/** Requirements for checking many unimported-symbol candidates against one completion's use site. */ +@RunWith(JUnit4::class) +class SymbolVisibilityCheckerReachabilityTest { + private val structureProvider = mockk() + private val checker = SymbolVisibilityChecker(structureProvider) + + private fun module( + name: String, + deps: List = emptyList(), + ): KaModule = + mockk(name = name) { + every { directRegularDependencies } returns deps + every { directFriendDependencies } returns emptyList() + every { directDependsOnDependencies } returns emptyList() + every { targetPlatform } returns JvmPlatforms.defaultJvmPlatform + } + + private fun publicSymbolIn( + sourceId: String, + declaringModule: KaModule, + ): JvmSymbol { + every { structureProvider.findModuleForSourceId(sourceId) } returns declaringModule + return JvmSymbol( + key = "com/example/Foo", + sourceId = sourceId, + name = "com/example/Foo", + shortName = "Foo", + packageName = "com.example", + kind = JvmSymbolKind.CLASS, + language = JvmSourceLanguage.KOTLIN, + visibility = JvmVisibility.PUBLIC, + data = JvmClassInfo(), + ) + } + + @Test + fun `a public symbol in a transitive dependency of the use site is visible`() { + val libB = module("libB") + val app = module("app", deps = listOf(module("libA", deps = listOf(libB)))) + + assertThat(checker.forUseSite(app).isVisible(publicSymbolIn("b.jar", libB), useSitePackage = "app")).isTrue() + } + + @Test + fun `a public symbol in a module the use site does not depend on is not visible`() { + val unrelated = module("unrelated") + val app = module("app", deps = listOf(module("libA"))) + + assertThat(checker.forUseSite(app).isVisible(publicSymbolIn("u.jar", unrelated), useSitePackage = "app")) + .isFalse() + } + + @Test + fun `checking many symbols from one use site walks the dependency graph once`() { + val libB = module("libB") + val libA = module("libA", deps = listOf(libB)) + val app = module("app", deps = listOf(libA)) + val visibility = checker.forUseSite(app) + + repeat(3) { visibility.isVisible(publicSymbolIn("b.jar", libB), useSitePackage = "app") } + + verify(exactly = 1) { libA.directRegularDependencies } + } +} From b374f243584e7a7f9156beb817d8a797ce1a4a90 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Tue, 17 Nov 2026 23:26:30 +0530 Subject: [PATCH 4/6] refactor(lsp-kotlin): find extension completion candidates in a session-only function --- .../completion/ExtensionCallableCandidates.kt | 23 +++++++++++++++++++ .../kotlin/completion/KotlinCompletions.kt | 11 +-------- 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt new file mode 100644 index 0000000000..2085bcd34a --- /dev/null +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt @@ -0,0 +1,23 @@ +package com.itsaky.androidide.lsp.kotlin.completion + +import org.jetbrains.kotlin.analysis.api.KaSession +import org.jetbrains.kotlin.analysis.api.scopes.KaScope +import org.jetbrains.kotlin.analysis.api.symbols.KaCallableSymbol +import org.jetbrains.kotlin.analysis.api.symbols.receiverType +import org.jetbrains.kotlin.analysis.api.types.KaType +import org.jetbrains.kotlin.name.Name + +/** The extensions in [scope] whose names pass [nameFilter] and that can be called on [receiverType]. */ +internal fun KaSession.extensionCallableCandidates( + receiverType: KaType, + scope: KaScope, + nameFilter: (Name) -> Boolean, +): Sequence = + scope + .callables(nameFilter) + .filter { symbol -> + if (!symbol.isExtension) return@filter false + + val extReceiverType = symbol.receiverType ?: return@filter false + receiverType.isSubtypeOf(extReceiverType) + } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt index c5d79071b3..80361f68e9 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt @@ -363,16 +363,7 @@ private fun KaSession.collectExtensionFunctions( receiverType: KaType, to: MutableList, ) { - val extensionSymbols = - ctx.scope - .callables { name -> matchesFilter(name) } - .filter { symbol -> - if (!symbol.isExtension) return@filter false - - val extReceiverType = symbol.receiverType ?: return@filter false - receiverType.isSubtypeOf(extReceiverType) - } - + val extensionSymbols = extensionCallableCandidates(receiverType, ctx.scope) { name -> matchesFilter(name) } to += toCompletionItems(extensionSymbols) } From 071729e14c4dd6bf420f95151ff8aa66e5676517 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Tue, 17 Nov 2026 23:33:14 +0530 Subject: [PATCH 5/6] perf(lsp-kotlin): look up extension completions by indexed name instead of enumerating every imported callable --- .../completion/ExtensionCallableCandidates.kt | 71 ++++++-- .../kotlin/completion/KotlinCompletions.kt | 34 +++- .../ExtensionCallableCandidatesTest.kt | 158 ++++++++++++++++++ 3 files changed, 251 insertions(+), 12 deletions(-) create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidatesTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt index 2085bcd34a..c5b1106a55 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt @@ -1,23 +1,72 @@ package com.itsaky.androidide.lsp.kotlin.completion import org.jetbrains.kotlin.analysis.api.KaSession -import org.jetbrains.kotlin.analysis.api.scopes.KaScope +import org.jetbrains.kotlin.analysis.api.components.KaScopeContext +import org.jetbrains.kotlin.analysis.api.components.KaScopeKind import org.jetbrains.kotlin.analysis.api.symbols.KaCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.receiverType +import org.jetbrains.kotlin.analysis.api.types.KaClassType +import org.jetbrains.kotlin.analysis.api.types.KaFlexibleType import org.jetbrains.kotlin.analysis.api.types.KaType +import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name -/** The extensions in [scope] whose names pass [nameFilter] and that can be called on [receiverType]. */ +/** + * The extensions that can be called on [receiverType] from [scopeContext], whose names pass + * [nameFilter]. + * + * Package and importing scopes are looked up by name, never enumerated: right after `.` every name + * passes, and enumerating them makes the Analysis API materialize every callable the file can see, + * parsing Java sources on the way - most of a member completion's time on device. The names come from + * [indexedExtensionNames], given the JVM symbol index spellings of the receiver type and all its + * supertypes ([extensionIndexReceiverNames]). Scopes the index never sees - local, type, type + * parameter, static member and script scopes - are small and are enumerated. + */ internal fun KaSession.extensionCallableCandidates( receiverType: KaType, - scope: KaScope, + scopeContext: KaScopeContext, + indexedExtensionNames: (receiverTypeNames: Set) -> Set, nameFilter: (Name) -> Boolean, -): Sequence = - scope - .callables(nameFilter) - .filter { symbol -> - if (!symbol.isExtension) return@filter false - - val extReceiverType = symbol.receiverType ?: return@filter false - receiverType.isSubtypeOf(extReceiverType) +): Sequence { + val unindexed = scopeContext.compositeScope { it.isUnindexed() }.callables(nameFilter) + val names = indexedExtensionNames(receiverTypeIndexNames(receiverType)).filter(nameFilter) + val indexed = + if (names.isEmpty()) { + emptySequence() + } else { + scopeContext.compositeScope { !it.isUnindexed() }.callables(names) } + return (unindexed + indexed).filter { symbol -> + if (!symbol.isExtension) return@filter false + + val extReceiverType = symbol.receiverType ?: return@filter false + receiverType.isSubtypeOf(extReceiverType) + } +} + +/** + * The spellings under which the JVM symbol index may store [classId] as an extension receiver. + * + * Library rows come from Kotlin metadata, which separates nested classes with `.`; source rows come + * from `SourceFileIndexer`, which uses `$`. Both are the same for a top-level class. + */ +internal fun extensionIndexReceiverNames(classId: ClassId): Set { + val packagePath = classId.packageFqName.asString().replace('.', '/') + val relativeName = classId.relativeClassName.asString() + + fun qualified(name: String) = if (packagePath.isEmpty()) name else "$packagePath/$name" + + return linkedSetOf(qualified(relativeName.replace('.', '$')), qualified(relativeName)) +} + +private fun KaScopeKind.isUnindexed(): Boolean = + this is KaScopeKind.LocalScope || + this is KaScopeKind.TypeScope || + this is KaScopeKind.TypeParameterScope || + this is KaScopeKind.StaticMemberScope || + this is KaScopeKind.ScriptMemberScope + +private fun KaSession.receiverTypeIndexNames(receiverType: KaType): Set = + (sequenceOf(receiverType) + receiverType.allSupertypes) + .mapNotNull { type -> (if (type is KaFlexibleType) type.lowerBound else type) as? KaClassType } + .flatMapTo(LinkedHashSet()) { extensionIndexReceiverNames(it.classId) } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt index 80361f68e9..f6e5409216 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinCompletions.kt @@ -28,9 +28,11 @@ import com.itsaky.androidide.progress.ICancelChecker import com.itsaky.androidide.progress.ProgressManager import com.itsaky.androidide.projects.FileManager import io.github.rosemoe.sora.lang.completion.CompletionCancelledException +import org.appdevforall.codeonthego.indexing.api.indexQuery import org.appdevforall.codeonthego.indexing.jvm.JvmClassInfo import org.appdevforall.codeonthego.indexing.jvm.JvmFunctionInfo import org.appdevforall.codeonthego.indexing.jvm.JvmSymbol +import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolDescriptor import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolIndex import org.appdevforall.codeonthego.indexing.jvm.JvmSymbolKind import org.appdevforall.codeonthego.indexing.jvm.JvmTypeAliasInfo @@ -363,10 +365,40 @@ private fun KaSession.collectExtensionFunctions( receiverType: KaType, to: MutableList, ) { - val extensionSymbols = extensionCallableCandidates(receiverType, ctx.scope) { name -> matchesFilter(name) } + val extensionSymbols = + extensionCallableCandidates(receiverType, ctx.scopeContext, { names -> indexedExtensionNames(names) }) { name -> + matchesFilter(name) + } to += toCompletionItems(extensionSymbols) } +/** + * Names of the extensions the JVM symbol indexes hold for any of [receiverTypeNames]. + * + * Unfiltered by the typed prefix: [matchesFilter] also accepts camel-case and fuzzy matches, which a + * prefix condition here would drop. Unlimited, since a capped name list silently loses candidates. + */ +context(ctx: AnalysisContext) +private fun indexedExtensionNames(receiverTypeNames: Set): Set { + val indexes = listOfNotNull(ctx.env.sourceIndex, ctx.env.generatedIndex, ctx.env.libraryIndex) + val names = mutableSetOf() + for (index in indexes) { + for (receiverTypeName in receiverTypeNames) { + abortIfCancelled() + val query = + indexQuery { + eq(JvmSymbolDescriptor.KEY_RECEIVER_TYPE, receiverTypeName) + limit = 0 + } + index + .distinctValues(JvmSymbolDescriptor.KEY_NAME, query) + .filter(Name::isValidIdentifier) + .mapTo(names, Name::identifier) + } + } + return names +} + context(env: CompilationEnvironment, ctx: AnalysisContext) private fun KaSession.collectScopeCompletions(to: MutableList) { if (ctx.partial.isBlank()) { diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidatesTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidatesTest.kt new file mode 100644 index 0000000000..72ffe6212f --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidatesTest.kt @@ -0,0 +1,158 @@ +package com.itsaky.androidide.lsp.kotlin.completion + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import org.jetbrains.kotlin.analysis.api.components.KaScopeContext +import org.jetbrains.kotlin.analysis.api.components.KaScopeKind +import org.jetbrains.kotlin.analysis.api.symbols.receiverType +import org.jetbrains.kotlin.analysis.api.types.KaType +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtQualifiedExpression +import org.junit.Test + +/** Requirements for finding the extensions member completion offers after `receiver.`. */ +class ExtensionCallableCandidatesTest : KtLspTest() { + private val declarations = + """ + package test + + open class Base + class Derived : Base() + class Unrelated + class Outer { + class Inner + } + + fun Base.onBase(): Int = 1 + fun Unrelated.onUnrelated(): Int = 2 + fun T.onAnything(): Int = 3 + fun Outer.Inner.onInner(): Int = 4 + """.trimIndent() + + /** + * What the JVM symbol index stores: receiver internal name to extension names. `kotlin/Any` holds the + * stdlib's `Any?.hashCode()` and `Any?.toString()`, which the library index has from metadata. + */ + private val indexedExtensions = + mapOf( + "kotlin/Any" to setOf("hashCode", "toString"), + "test/Base" to setOf("onBase"), + "test/Unrelated" to setOf("onUnrelated"), + "T0" to setOf("onAnything"), + "test/Outer\$Inner" to setOf("onInner"), + ) + + private val completeIndex: (Set) -> Set = { receiverNames -> + receiverNames.flatMap { indexedExtensions[it].orEmpty() }.map(Name::identifier).toSet() + } + + private class Lookup( + val candidates: List, + val receiverNamesAsked: Set, + val enumeratedCandidates: List, + val unindexedScopeCandidates: List, + ) + + /** Extension candidates after the receiver of `[qualified]` in a function whose body is [body]. */ + private fun lookUp( + qualified: String, + body: String, + index: (Set) -> Set = completeIndex, + ): Lookup { + createSourceFile("test/Declarations.kt", declarations) + val file: KtFile = createSourceFile("test/Use.kt", "package test\n\nfun use() {\n$body\n}\n") + val qualifiedExpression = + PsiTreeUtil.findChildrenOfType(file, KtQualifiedExpression::class.java).single { it.text == qualified } + val asked = mutableSetOf() + return analyzeMaybeDanglingForTest(file) { + val receiverType: KaType = qualifiedExpression.receiverExpression.expressionType!! + val scopeContext: KaScopeContext = file.scopeContext(qualifiedExpression.selectorExpression!!) + val found = + extensionCallableCandidates(receiverType, scopeContext, { names -> + asked += names + index(names) + }) { true } + + fun applicableExtensionNames(scopeKinds: (KaScopeKind) -> Boolean): List = + scopeContext + .compositeScope(scopeKinds) + .callables { true } + .filter { it.isExtension && it.receiverType?.let { r -> receiverType.isSubtypeOf(r) } == true } + .mapNotNull { it.callableId?.callableName?.asString() } + .toList() + Lookup( + candidates = found.mapNotNull { it.callableId?.callableName?.asString() }.toList(), + receiverNamesAsked = asked, + enumeratedCandidates = applicableExtensionNames { true }, + unindexedScopeCandidates = + applicableExtensionNames { + it is KaScopeKind.LocalScope || + it is KaScopeKind.TypeScope || + it is KaScopeKind.TypeParameterScope || + it is KaScopeKind.StaticMemberScope || + it is KaScopeKind.ScriptMemberScope + }, + ) + } + } + + @Test + fun `an extension on a supertype of the receiver is offered`() { + assertThat(lookUp("Derived().hashCode()", " Derived().hashCode()").candidates).contains("onBase") + } + + @Test + fun `an extension on an unrelated type is not offered`() { + assertThat(lookUp("Derived().hashCode()", " Derived().hashCode()").candidates).doesNotContain("onUnrelated") + } + + /** Local, type, type-parameter, static member and script scopes hold declarations the index never sees, so they are enumerated. */ + @Test + fun `extensions in scopes the index never sees are found without it`() { + val lookup = + lookUp("Derived().hashCode()", " fun Derived.local(): Int = 5\n Derived().hashCode()", index = { emptySet() }) + + assertThat(lookup.candidates).containsExactlyElementsIn(lookup.unindexedScopeCandidates) + } + + @Test + fun `a top-level extension the index does not name is not looked up`() { + val lookup = lookUp("Derived().hashCode()", " Derived().hashCode()", index = { emptySet() }) + + assertThat(lookup.candidates).doesNotContain("onBase") + } + + @Test + fun `the index is asked about the receiver type and each of its supertypes`() { + val asked = lookUp("Derived().hashCode()", " Derived().hashCode()").receiverNamesAsked + + assertThat(asked).containsAtLeast("test/Derived", "test/Base", "kotlin/Any") + } + + @Test + fun `an extension on a nested class is found under the source index's spelling of its name`() { + val lookup = lookUp("Outer.Inner().hashCode()", " Outer.Inner().hashCode()") + + assertThat(lookup.candidates).contains("onInner") + } + + @Test + fun `with a complete index the candidates are the ones enumerating every scope finds`() { + val lookup = lookUp("Derived().hashCode()", " fun Derived.local(): Int = 5\n Derived().hashCode()") + + assertThat(lookup.candidates).containsExactlyElementsIn(lookup.enumeratedCandidates) + } + + @Test + fun `index receiver names spell a nested class both ways`() { + val nested = ClassId(FqName("test"), FqName("Outer.Inner"), isLocal = false) + val topLevel = ClassId(FqName("test"), FqName("Base"), isLocal = false) + + assertThat(extensionIndexReceiverNames(nested)).containsExactly("test/Outer\$Inner", "test/Outer.Inner") + assertThat(extensionIndexReceiverNames(topLevel)).containsExactly("test/Base") + } +} From c8272371b8424aaff2cf3fa735c526d10365937a Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Wed, 18 Nov 2026 00:43:16 +0530 Subject: [PATCH 6/6] perf(lsp-kotlin): look up member extensions of implicit receivers by name, read from Kotlin classes only --- .../completion/ExtensionCallableCandidates.kt | 59 +++++++--- .../MemberExtensionCandidatesTest.kt | 109 ++++++++++++++++++ 2 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/MemberExtensionCandidatesTest.kt diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt index c5b1106a55..6b416f47df 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/ExtensionCallableCandidates.kt @@ -4,6 +4,8 @@ import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.components.KaScopeContext import org.jetbrains.kotlin.analysis.api.components.KaScopeKind import org.jetbrains.kotlin.analysis.api.symbols.KaCallableSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol +import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin import org.jetbrains.kotlin.analysis.api.symbols.receiverType import org.jetbrains.kotlin.analysis.api.types.KaClassType import org.jetbrains.kotlin.analysis.api.types.KaFlexibleType @@ -19,8 +21,12 @@ import org.jetbrains.kotlin.name.Name * passes, and enumerating them makes the Analysis API materialize every callable the file can see, * parsing Java sources on the way - most of a member completion's time on device. The names come from * [indexedExtensionNames], given the JVM symbol index spellings of the receiver type and all its - * supertypes ([extensionIndexReceiverNames]). Scopes the index never sees - local, type, type - * parameter, static member and script scopes - are small and are enumerated. + * supertypes ([extensionIndexReceiverNames]). + * + * The implicit receivers' type scopes are looked up by name too, with [memberExtensionNames]: inside + * an Android class they hold every member of its whole framework hierarchy, all materialized just to + * keep the few member extensions. The remaining scopes - local, type parameter, static member and + * script - are small and the index never sees them, so they are enumerated. */ internal fun KaSession.extensionCallableCandidates( receiverType: KaType, @@ -28,15 +34,11 @@ internal fun KaSession.extensionCallableCandidates( indexedExtensionNames: (receiverTypeNames: Set) -> Set, nameFilter: (Name) -> Boolean, ): Sequence { - val unindexed = scopeContext.compositeScope { it.isUnindexed() }.callables(nameFilter) - val names = indexedExtensionNames(receiverTypeIndexNames(receiverType)).filter(nameFilter) - val indexed = - if (names.isEmpty()) { - emptySequence() - } else { - scopeContext.compositeScope { !it.isUnindexed() }.callables(names) - } - return (unindexed + indexed).filter { symbol -> + val enumerated = scopeContext.compositeScope { it.isEnumerated() }.callables(nameFilter) + val members = callablesNamed(scopeContext, memberExtensionNames(scopeContext).filter(nameFilter)) { it is KaScopeKind.TypeScope } + val indexedNames = indexedExtensionNames(receiverTypeIndexNames(receiverType)).filter(nameFilter) + val indexed = callablesNamed(scopeContext, indexedNames) { !it.isEnumerated() && it !is KaScopeKind.TypeScope } + return (enumerated + members + indexed).filter { symbol -> if (!symbol.isExtension) return@filter false val extReceiverType = symbol.receiverType ?: return@filter false @@ -59,14 +61,43 @@ internal fun extensionIndexReceiverNames(classId: ClassId): Set { return linkedSetOf(qualified(relativeName.replace('.', '$')), qualified(relativeName)) } -private fun KaScopeKind.isUnindexed(): Boolean = +/** + * Names of the extensions declared as members of the implicit receivers' classes or their + * supertypes, read from declared members only. Java classes are skipped: they cannot declare member + * extensions, and in an Android class they are nearly the whole hierarchy. + */ +internal fun KaSession.memberExtensionNames(scopeContext: KaScopeContext): Set { + val names = mutableSetOf() + val visited = mutableSetOf() + for (receiver in scopeContext.implicitReceivers) { + for (type in sequenceOf(receiver.type) + receiver.type.allSupertypes) { + val classSymbol = (type.lowerBoundIfFlexible() as? KaClassType)?.symbol as? KaClassSymbol ?: continue + if (classSymbol.origin == KaSymbolOrigin.JAVA_SOURCE || classSymbol.origin == KaSymbolOrigin.JAVA_LIBRARY) continue + if (!visited.add(classSymbol)) continue + classSymbol.declaredMemberScope + .callables { true } + .filter { it.isExtension } + .mapNotNullTo(names) { it.callableId?.callableName } + } + } + return names +} + +private fun KaSession.callablesNamed( + scopeContext: KaScopeContext, + names: List, + scopeKinds: (KaScopeKind) -> Boolean, +): Sequence = if (names.isEmpty()) emptySequence() else scopeContext.compositeScope(scopeKinds).callables(names) + +private fun KaScopeKind.isEnumerated(): Boolean = this is KaScopeKind.LocalScope || - this is KaScopeKind.TypeScope || this is KaScopeKind.TypeParameterScope || this is KaScopeKind.StaticMemberScope || this is KaScopeKind.ScriptMemberScope +private fun KaType.lowerBoundIfFlexible(): KaType = if (this is KaFlexibleType) lowerBound else this + private fun KaSession.receiverTypeIndexNames(receiverType: KaType): Set = (sequenceOf(receiverType) + receiverType.allSupertypes) - .mapNotNull { type -> (if (type is KaFlexibleType) type.lowerBound else type) as? KaClassType } + .mapNotNull { type -> type.lowerBoundIfFlexible() as? KaClassType } .flatMapTo(LinkedHashSet()) { extensionIndexReceiverNames(it.classId) } diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/MemberExtensionCandidatesTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/MemberExtensionCandidatesTest.kt new file mode 100644 index 0000000000..bec49075f1 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/completion/MemberExtensionCandidatesTest.kt @@ -0,0 +1,109 @@ +package com.itsaky.androidide.lsp.kotlin.completion + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import org.jetbrains.kotlin.analysis.api.symbols.receiverType +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtQualifiedExpression +import org.junit.Test + +/** Requirements for offering extensions declared as members of the implicit receivers after `receiver.`. */ +class MemberExtensionCandidatesTest : KtLspTest() { + private val declarations = + """ + package test + + open class BaseBuilder { + fun String.inherited(): Int = 1 + } + + class Builder : BaseBuilder() { + fun String.shout(): Int = 2 + } + + class Scope { + fun T.wrap(): Int = 3 + } + + class JavaBacked : java.util.ArrayList() { + fun String.own(): Int = 4 + fun plain(): Int = 5 + } + """.trimIndent() + + @Test + fun `member extension names come from the Kotlin classes of the implicit receivers' hierarchies`() { + createSourceFile("test/Declarations.kt", declarations) + val file: KtFile = createSourceFile("test/Use.kt", "package test\n\nfun JavaBacked.inJava() {\n \"x\".length\n}\n") + val selector = + PsiTreeUtil.findChildrenOfType(file, KtQualifiedExpression::class.java).single().selectorExpression!! + + val names = analyzeMaybeDanglingForTest(file) { memberExtensionNames(file.scopeContext(selector)) } + + assertThat(names.map { it.asString() }).containsExactly("own") + } + + private class Lookup( + val candidates: List, + val enumeratedCandidates: List, + ) + + /** Candidates after the receiver of `[qualified]` inside [function], with an index that knows nothing. */ + private fun lookUp( + function: String, + qualified: String, + ): Lookup { + createSourceFile("test/Declarations.kt", declarations) + val file: KtFile = createSourceFile("test/Use.kt", "package test\n\n$function {\n $qualified\n}\n") + val qualifiedExpression = + PsiTreeUtil.findChildrenOfType(file, KtQualifiedExpression::class.java).single { it.text == qualified } + return analyzeMaybeDanglingForTest(file) { + val receiverType = qualifiedExpression.receiverExpression.expressionType!! + val scopeContext = file.scopeContext(qualifiedExpression.selectorExpression!!) + val found = extensionCallableCandidates(receiverType, scopeContext, { emptySet() }) { true } + val enumerated = + scopeContext + .compositeScope() + .callables { true } + .filter { it.isExtension && it.receiverType?.let { r -> receiverType.isSubtypeOf(r) } == true } + Lookup( + candidates = found.mapNotNull { it.callableId?.callableName?.asString() }.toList(), + enumeratedCandidates = + enumerated + .mapNotNull { it.callableId?.callableName?.asString() } + .filter { it in setOf("inherited", "shout", "wrap") } + .toList(), + ) + } + } + + @Test + fun `a member extension declared in a Kotlin implicit receiver class is offered`() { + assertThat(lookUp("fun Builder.build()", "\"x\".length").candidates).contains("shout") + } + + @Test + fun `a member extension inherited from a Kotlin superclass of the implicit receiver is offered`() { + assertThat(lookUp("fun Builder.build()", "\"x\".length").candidates).contains("inherited") + } + + /** + * Pins today's behaviour: the receiver check compares against the unsubstituted `T`, so a generic + * implicit receiver's member extension is not offered. A fix for that flips this test. + */ + @Test + fun `a member extension of a generic implicit receiver is not offered`() { + assertThat(lookUp("fun Scope.inScope()", "\"x\".length").candidates).doesNotContain("wrap") + } + + @Test + fun `member extensions offered are the ones enumerating every scope finds`() { + for ((function, qualified) in listOf("fun Builder.build()" to "\"x\".length", "fun Scope.inScope()" to "\"x\".length")) { + val lookup = lookUp(function, qualified) + + assertThat(lookup.candidates.filter { it in setOf("inherited", "shout", "wrap") }) + .containsExactlyElementsIn(lookup.enumeratedCandidates) + } + } +}