Skip to content

Commit 61619b3

Browse files
feat: resolve todo by fetching paths async
1 parent dc24041 commit 61619b3

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import java.io.File
1010
import java.nio.file.FileSystems
1111
import java.nio.file.Files
1212
import java.nio.file.Path
13+
import java.util.concurrent.CompletableFuture
1314

1415
/**
1516
* Manages the class path (compiled JARs, etc), the Java source path
@@ -51,36 +52,46 @@ class CompilerClassPath(
5152
updateBuildScriptClassPath: Boolean = true,
5253
updateJavaSourcePath: Boolean = true
5354
): Boolean {
54-
// TODO: Fetch class path and build script class path concurrently (and asynchronously)
5555
val resolver = defaultClassPathResolver(workspaceRoots, databaseService.db)
5656
var refreshCompiler = updateJavaSourcePath
57-
58-
if (updateClassPath) {
59-
val newClassPath = resolver.classpathOrEmpty
60-
if (newClassPath != classPath) {
61-
synchronized(classPath) {
62-
syncPaths(classPath, newClassPath, "class path") { it.compiledJar }
57+
val asyncExecutor = AsyncExecutor()
58+
59+
val classPathFuture = if (updateClassPath) {
60+
asyncExecutor.compute {
61+
val newClassPath = resolver.classpathOrEmpty
62+
if (newClassPath != classPath) {
63+
synchronized(classPath) {
64+
syncPaths(classPath, newClassPath, "class path") { it.compiledJar }
65+
}
66+
refreshCompiler = true
6367
}
64-
refreshCompiler = true
65-
}
6668

67-
async.compute {
68-
val newClassPathWithSources = resolver.classpathWithSources
69-
synchronized(classPath) {
70-
syncPaths(classPath, newClassPathWithSources, "class path with sources") { it.compiledJar }
69+
asyncExecutor.compute {
70+
val newClassPathWithSources = resolver.classpathWithSources
71+
synchronized(classPath) {
72+
syncPaths(classPath, newClassPathWithSources, "class path with sources") { it.compiledJar }
73+
}
7174
}
7275
}
76+
} else {
77+
CompletableFuture.completedFuture(null)
7378
}
7479

75-
if (updateBuildScriptClassPath) {
76-
LOG.info("Update build script path")
77-
val newBuildScriptClassPath = resolver.buildScriptClasspathOrEmpty
78-
if (newBuildScriptClassPath != buildScriptClassPath) {
79-
syncPaths(buildScriptClassPath, newBuildScriptClassPath, "build script class path") { it }
80-
refreshCompiler = true
80+
val buildScriptClassPathFuture = if (updateBuildScriptClassPath) {
81+
asyncExecutor.compute {
82+
LOG.info("Update build script path")
83+
val newBuildScriptClassPath = resolver.buildScriptClasspathOrEmpty
84+
if (newBuildScriptClassPath != buildScriptClassPath) {
85+
syncPaths(buildScriptClassPath, newBuildScriptClassPath, "build script class path") { it }
86+
refreshCompiler = true
87+
}
8188
}
89+
} else {
90+
CompletableFuture.completedFuture(null)
8291
}
8392

93+
CompletableFuture.allOf(classPathFuture, buildScriptClassPathFuture).join()
94+
8495
if (refreshCompiler) {
8596
LOG.info("Reinstantiating compiler")
8697
compiler.close()
@@ -98,6 +109,7 @@ class CompilerClassPath(
98109
return refreshCompiler
99110
}
100111

112+
101113
/** Synchronizes the given two path sets and logs the differences. */
102114
private fun <T> syncPaths(dest: MutableSet<T>, new: Set<T>, name: String, toPath: (T) -> Path) {
103115
val added = new - dest

0 commit comments

Comments
 (0)