Skip to content

Commit f0b497c

Browse files
committed
Move index initialization logic into SourcePath
1 parent a5abcf3 commit f0b497c

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

server/src/main/kotlin/org/javacs/kt/SourcePath.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ class SourcePath(
3131
) {
3232
private val files = mutableMapOf<URI, SourceFile>()
3333
private val parseDataWriteLock = ReentrantLock()
34-
private val indexAsync = AsyncExecutor()
3534

36-
val index = SymbolIndex()
35+
private val indexAsync = AsyncExecutor()
36+
private var indexInitialized: Boolean = false
3737
var indexEnabled: Boolean by indexingConfig::enabled
38+
val index = SymbolIndex()
39+
3840
var beforeCompileCallback: () -> Unit = {}
3941

4042
var progressFactory: Progress.Factory = Progress.Factory.None
@@ -104,7 +106,7 @@ class SourcePath(
104106
compiledFile = parsed
105107
}
106108

107-
updateIndexAsync(container)
109+
initializeIndexAsyncIfNeeded(container)
108110
}
109111

110112
private fun doCompileIfChanged() {
@@ -211,8 +213,8 @@ class SourcePath(
211213
}
212214

213215
// Only index normal files, not build files
214-
if (indexEnabled && kind == CompilationKind.DEFAULT) {
215-
updateIndexAsync(container)
216+
if (kind == CompilationKind.DEFAULT) {
217+
initializeIndexAsyncIfNeeded(container)
216218
}
217219

218220
return context
@@ -229,11 +231,15 @@ class SourcePath(
229231
}
230232

231233
/**
232-
* Updates the symbol index asynchronously.
234+
* Initialized the symbol index asynchronously, if not
235+
* already done.
233236
*/
234-
private fun updateIndexAsync(container: ComponentProvider) = indexAsync.execute {
235-
val module = container.getService(ModuleDescriptor::class.java)
236-
index.update(module)
237+
private fun initializeIndexAsyncIfNeeded(container: ComponentProvider) = indexAsync.execute {
238+
if (indexEnabled && !indexInitialized) {
239+
indexInitialized = true
240+
val module = container.getService(ModuleDescriptor::class.java)
241+
index.refresh(module)
242+
}
237243
}
238244

239245
/**

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private object Symbols : Table() {
2626
*/
2727
class SymbolIndex {
2828
private val db = Database.connect("jdbc:h2:mem:symbolindex;DB_CLOSE_DELAY=-1", "org.h2.Driver")
29-
private var initialized = false
3029

3130
var progressFactory: Progress.Factory = Progress.Factory.None
3231

@@ -36,11 +35,8 @@ class SymbolIndex {
3635
}
3736
}
3837

39-
fun update(module: ModuleDescriptor) {
40-
// TODO: Remove this once a proper debounce mechanism (and ideally incremental updates) are in place.
41-
if (initialized) return
42-
initialized = true
43-
38+
/** Rebuilds the entire index. May take a while. */
39+
fun refresh(module: ModuleDescriptor) {
4440
val started = System.currentTimeMillis()
4541
LOG.info("Updating symbol index...")
4642

0 commit comments

Comments
 (0)