Skip to content

Commit 885ec1b

Browse files
committed
Use ConcurrentHashMap in SymbolIndex
1 parent f6efbe0 commit 885ec1b

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

server/src/main/kotlin/org/javacs/kt/completion/Completions.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ private fun keywordCompletionItems(partial: String): Sequence<CompletionItem> =
8282
/** Finds completions based on the element around the user's cursor. */
8383
private fun elementCompletionItems(file: CompiledFile, cursor: Int, index: SymbolIndex, config: CompletionConfiguration, partial: String): Sequence<CompletionItem> {
8484
val surroundingElement = completableElement(file, cursor) ?: return emptySequence()
85-
var completions = elementCompletions(file, cursor, surroundingElement)
86-
87-
index.withGlobalDescriptors {
88-
completions += it // TODO: Filter non-imported (i.e. the elementCompletions already found) and auto-import these when selected by the user
89-
}
85+
val completions = elementCompletions(file, cursor, surroundingElement) + index.globalDescriptors.values // TODO: Filter non-imported (i.e. the elementCompletions already found) and auto-import these when selected by the user
9086

9187
val matchesName = completions.filter { containsCharactersInOrder(name(it), partial, caseSensitive = false) }
9288
val sorted = matchesName.takeIf { partial.length >= MIN_SORT_LENGTH }?.sortedBy { stringDistance(name(it), partial) }

server/src/main/kotlin/org/javacs/kt/index/SymbolIndex.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,32 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
44
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
55
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
66
import org.jetbrains.kotlin.resolve.scopes.MemberScope
7+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
78
import org.jetbrains.kotlin.name.FqName
89
import org.javacs.kt.compiler.Compiler
910
import org.javacs.kt.LOG
11+
import java.util.concurrent.ConcurrentHashMap
1012
import java.util.concurrent.locks.ReentrantLock
1113
import kotlin.concurrent.withLock
1214

1315
/**
1416
* A global view of all available symbols across all packages.
1517
*/
1618
class SymbolIndex {
17-
private val globalDescriptors: MutableSet<DeclarationDescriptor> = mutableSetOf()
18-
private val lock = ReentrantLock()
19+
val globalDescriptors = ConcurrentHashMap<FqName, DeclarationDescriptor>()
1920

2021
fun update(module: ModuleDescriptor) {
2122
val started = System.currentTimeMillis()
2223
LOG.info("Updating symbol index...")
2324

24-
val foundDescriptors = allDescriptors(module)
25-
lock.withLock {
26-
globalDescriptors += foundDescriptors
25+
for (descriptor in allDescriptors(module)) {
26+
globalDescriptors[descriptor.fqNameSafe] = descriptor
2727
}
2828

2929
val finished = System.currentTimeMillis()
3030
LOG.info("Updated symbol index in ${finished - started} ms! (${globalDescriptors.size} symbol(s))")
3131
}
3232

33-
fun <T> withGlobalDescriptors(action: (Set<DeclarationDescriptor>) -> T): T = lock.withLock { action(globalDescriptors) }
34-
3533
private fun allDescriptors(module: ModuleDescriptor): Collection<DeclarationDescriptor> = allPackages(module)
3634
.map(module::getPackage)
3735
.flatMap {

0 commit comments

Comments
 (0)