Skip to content
Draft
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
@@ -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
Expand All @@ -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

Expand All @@ -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<KtModule>
private lateinit var project: Project

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<String>()
// 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<KaModule, Boolean>())

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, KaModule?>(INITIAL_CAPACITY, LOAD_FACTOR, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, KaModule?>): 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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.itsaky.androidide.lsp.kotlin.completion

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
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name

/**
* 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]).
*
* 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,
scopeContext: KaScopeContext,
indexedExtensionNames: (receiverTypeNames: Set<String>) -> Set<Name>,
nameFilter: (Name) -> Boolean,
): Sequence<KaCallableSymbol> {
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
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<String> {
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))
}

/**
* 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<Name> {
val names = mutableSetOf<Name>()
val visited = mutableSetOf<KaClassSymbol>()
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<Name>,
scopeKinds: (KaScopeKind) -> Boolean,
): Sequence<KaCallableSymbol> = if (names.isEmpty()) emptySequence() else scopeContext.compositeScope(scopeKinds).callables(names)

private fun KaScopeKind.isEnumerated(): Boolean =
this is KaScopeKind.LocalScope ||
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<String> =
(sequenceOf(receiverType) + receiverType.allSupertypes)
.mapNotNull { type -> type.lowerBoundIfFlexible() as? KaClassType }
.flatMapTo(LinkedHashSet()) { extensionIndexReceiverNames(it.classId) }
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -364,18 +366,39 @@ private fun KaSession.collectExtensionFunctions(
to: MutableList<CompletionItem>,
) {
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)
}

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<String>): Set<Name> {
val indexes = listOfNotNull(ctx.env.sourceIndex, ctx.env.generatedIndex, ctx.env.libraryIndex)
val names = mutableSetOf<Name>()
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<CompletionItem>) {
if (ctx.partial.isBlank()) {
Expand Down Expand Up @@ -428,22 +451,13 @@ private fun KaSession.collectUnimportedSymbols(to: MutableList<CompletionItem>)
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
Expand Down
Loading
Loading