Skip to content

Commit b8ed07d

Browse files
committed
Query build script classpath and auto-update on change
Automatically update the classpath if a Gradle buildfile changes and query the build script classpath in CompilerClassPath.
1 parent a2078c9 commit b8ed07d

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,48 @@ import java.nio.file.Path
77
class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
88
private val workspaceRoots = mutableSetOf<Path>()
99
private val classPath = mutableSetOf<Path>()
10-
var compiler = Compiler(classPath)
10+
private val buildScriptClassPath = mutableSetOf<Path>()
11+
var compiler = Compiler(classPath, buildScriptClassPath)
1112
private set
1213

1314
init {
1415
compiler.updateConfiguration(config)
1516
}
1617

1718
private fun refresh() {
18-
val newClassPath = defaultClassPathResolver(workspaceRoots).classpathOrEmpty
19+
val resolver = defaultClassPathResolver(workspaceRoots)
20+
val newClassPath = resolver.classpathOrEmpty
21+
val newBuildScriptClassPath = resolver.buildScriptClasspathOrEmpty
22+
var refreshCompiler = false
1923

2024
if (newClassPath != classPath) {
21-
val added = newClassPath - classPath
22-
val removed = classPath - newClassPath
25+
syncClassPath(classPath, newClassPath)
26+
refreshCompiler = true
27+
}
2328

24-
logAdded(added)
25-
logRemoved(removed)
29+
if (newBuildScriptClassPath != buildScriptClassPath) {
30+
syncClassPath(buildScriptClassPath, newBuildScriptClassPath)
31+
refreshCompiler = true
32+
}
2633

27-
classPath.removeAll(removed)
28-
classPath.addAll(added)
34+
if (refreshCompiler) {
2935
compiler.close()
30-
compiler = Compiler(classPath)
36+
compiler = Compiler(classPath, buildScriptClassPath)
3137
updateCompilerConfiguration()
3238
}
3339
}
3440

41+
private fun syncClassPath(dest: MutableSet<Path>, new: Set<Path>) {
42+
val added = new - dest
43+
val removed = dest - new
44+
45+
logAdded(added)
46+
logRemoved(removed)
47+
48+
dest.removeAll(removed)
49+
dest.addAll(added)
50+
}
51+
3552
fun updateCompilerConfiguration() {
3653
compiler.updateConfiguration(config)
3754
}
@@ -61,7 +78,8 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
6178
}
6279

6380
fun changedOnDisk(file: Path) {
64-
if (file.fileName.toString() == "pom.xml")
81+
val name = file.fileName.toString()
82+
if (name == "pom.xml" || name == "build.gradle" || name == "build.gradle.kts")
6583
refresh()
6684
}
6785

0 commit comments

Comments
 (0)