Skip to content

Commit 98336de

Browse files
committed
Compile and index on startup
1 parent 296ad33 commit 98336de

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
121121
}
122122
}
123123

124+
textDocuments.lintAll()
125+
124126
val serverInfo = ServerInfo("Kotlin Language Server", VERSION)
125127

126128
InitializeResult(serverCapabilities, serverInfo)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ class KotlinTextDocumentService(
268268
debounceLint = Debouncer(Duration.ofMillis(config.linting.debounceTime))
269269
}
270270

271+
fun lintAll() {
272+
debounceLint.submitImmediately {
273+
sp.compileAllFiles()
274+
sp.refreshDependencyIndexes()
275+
}
276+
}
277+
271278
private fun clearLint(): List<URI> {
272279
val result = lintTodo.toList()
273280
lintTodo.clear()

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class SourcePath(
3030
private val parseDataWriteLock = ReentrantLock()
3131

3232
private val indexAsync = AsyncExecutor()
33-
private var indexInitialized: Boolean = false
3433
var indexEnabled: Boolean by indexingConfig::enabled
3534
val index = SymbolIndex()
3635

@@ -105,7 +104,7 @@ class SourcePath(
105104
compiledFile = parsed
106105
}
107106

108-
refreshIndexes(container, listOfNotNull(oldFile), listOfNotNull(this))
107+
refreshWorkspaceIndexes(listOfNotNull(oldFile), listOfNotNull(this))
109108
}
110109

111110
private fun doCompileIfChanged() {
@@ -163,7 +162,7 @@ class SourcePath(
163162
}
164163

165164
fun delete(uri: URI) {
166-
files[uri]?.let { refreshIndexes(files[uri]?.compiledContainer!!, listOf(it), listOf()) }
165+
files[uri]?.let { refreshWorkspaceIndexes(listOf(it), listOf()) }
167166

168167
files.remove(uri)
169168
}
@@ -231,7 +230,7 @@ class SourcePath(
231230

232231
// Only index normal files, not build files
233232
if (kind == CompilationKind.DEFAULT) {
234-
refreshIndexes(container, oldFiles, parse.keys.toList())
233+
refreshWorkspaceIndexes(oldFiles, parse.keys.toList())
235234
}
236235

237236
return context
@@ -247,27 +246,47 @@ class SourcePath(
247246
return CompositeBindingContext.create(combined)
248247
}
249248

249+
fun compileAllFiles() {
250+
// TODO: Investigate the possibility of compiling all files at once, instead of iterating here
251+
// At the moment, compiling all files at once sometimes leads to an internal error from the TopDownAnalyzer
252+
files.keys.forEach {
253+
compileFiles(listOf(it))
254+
}
255+
}
256+
257+
fun refreshDependencyIndexes() {
258+
compileAllFiles()
259+
260+
val container = files.values.first { it.compiledContainer != null }.compiledContainer
261+
if (container != null) {
262+
refreshDependencyIndexes(container)
263+
}
264+
}
265+
250266
/**
251267
* Refreshes the indexes. If already done, refreshes only the declarations in the files that were changed.
252268
*/
253-
private fun refreshIndexes(container: ComponentProvider, oldFiles: List<SourceFile>, newFiles: List<SourceFile>) = indexAsync.execute {
269+
private fun refreshWorkspaceIndexes(oldFiles: List<SourceFile>, newFiles: List<SourceFile>) = indexAsync.execute {
254270
if (indexEnabled) {
255-
val module = container.getService(ModuleDescriptor::class.java)
256271
val oldDeclarations = getDeclarationDescriptors(oldFiles)
257272
val newDeclarations = getDeclarationDescriptors(newFiles)
258273

259-
// Index all the declarations except any new declarations that were just compiled
260-
// TODO: Move this logic to a different place when re-indexing is triggered on configuration changes
261-
if (!indexInitialized) {
262-
indexInitialized = true
263-
index.refresh(module, newDeclarations)
264-
}
265-
266274
// Index the new declarations in the Kotlin source files that were just compiled, removing the old ones
267275
index.updateIndexes(oldDeclarations, newDeclarations)
268276
}
269277
}
270278

279+
/**
280+
* Refreshes the indexes. If already done, refreshes only the declarations in the files that were changed.
281+
*/
282+
private fun refreshDependencyIndexes(container: ComponentProvider) = indexAsync.execute {
283+
if (indexEnabled) {
284+
val module = container.getService(ModuleDescriptor::class.java)
285+
val declarations = getDeclarationDescriptors(files.values)
286+
index.refresh(module, declarations)
287+
}
288+
}
289+
271290
// Gets all the declaration descriptors for the collection of files
272291
private fun getDeclarationDescriptors(files: Collection<SourceFile>) =
273292
files.flatMap { file ->

0 commit comments

Comments
 (0)