Skip to content

Commit e6ac863

Browse files
committed
Add workspaceRoots as Java source roots in Kotlin compiler config
1 parent 2780cab commit e6ac863

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
1616
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
1717
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
1818
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
19+
import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoots
1920
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
2021
import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser
2122
import org.jetbrains.kotlin.config.CommonConfigurationKeys
@@ -86,6 +87,7 @@ private val GRADLE_DSL_DEPENDENCY_PATTERN = Regex("^gradle-(?:kotlin-dsl|core).*
8687
* files and expressions.
8788
*/
8889
private class CompilationEnvironment(
90+
workspaceRoots: Set<Path>,
8991
classPath: Set<Path>
9092
) : Closeable {
9193
private val disposable = Disposer.newDisposable()
@@ -112,6 +114,7 @@ private class CompilationEnvironment(
112114
put(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS, languageVersionSettings)
113115
put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, LoggingMessageCollector)
114116
add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, ScriptingCompilerConfigurationComponentRegistrar())
117+
addJavaSourceRoots(workspaceRoots.map { it.toFile() })
115118
addJvmClasspathRoots(classPath.map { it.toFile() })
116119

117120
// Setup script templates (e.g. used by Gradle's Kotlin DSL)
@@ -160,6 +163,8 @@ private class CompilationEnvironment(
160163
configFiles = EnvironmentConfigFiles.JVM_CONFIG_FILES
161164
)
162165

166+
// TODO: Use environment.addKotlinSourceRoots(workspaceRoots) and update those instead of using a custom SourcePath/SourceFile mechanism
167+
163168
val project = environment.project
164169
if (project is MockProject) {
165170
project.registerService(NullableNotNullManager::class.java, KotlinNullableNotNullManager(project))
@@ -219,12 +224,12 @@ enum class CompilationKind {
219224
* Incrementally compiles files and expressions.
220225
* The basic strategy for compiling one file at-a-time is outlined in OneFilePerformance.
221226
*/
222-
class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet()) : Closeable {
227+
class Compiler(workspaceRoots: Set<Path>, classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet()) : Closeable {
223228
private var closed = false
224229
private val localFileSystem: VirtualFileSystem
225230

226-
private val defaultCompileEnvironment = CompilationEnvironment(classPath)
227-
private val buildScriptCompileEnvironment = buildScriptClassPath.takeIf { it.isNotEmpty() }?.let(::CompilationEnvironment)
231+
private val defaultCompileEnvironment = CompilationEnvironment(workspaceRoots, classPath)
232+
private val buildScriptCompileEnvironment = buildScriptClassPath.takeIf { it.isNotEmpty() }?.let { CompilationEnvironment(workspaceRoots, it) }
228233
private val compileLock = ReentrantLock() // TODO: Lock at file-level
229234

230235
companion object {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
88
private val workspaceRoots = mutableSetOf<Path>()
99
private val classPath = mutableSetOf<Path>()
1010
private val buildScriptClassPath = mutableSetOf<Path>()
11-
var compiler = Compiler(classPath, buildScriptClassPath)
11+
var compiler = Compiler(workspaceRoots, classPath, buildScriptClassPath)
1212
private set
1313

1414
init {
@@ -36,7 +36,7 @@ class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
3636

3737
if (refreshCompiler) {
3838
compiler.close()
39-
compiler = Compiler(classPath, buildScriptClassPath)
39+
compiler = Compiler(workspaceRoots, classPath, buildScriptClassPath)
4040
updateCompilerConfiguration()
4141
}
4242
}

server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class CompiledFileTest {
1515
}
1616
}
1717

18-
fun compileFile(): CompiledFile = Compiler(setOf()).use { compiler ->
18+
fun compileFile(): CompiledFile = Compiler(setOf(), setOf()).use { compiler ->
1919
val file = testResourcesRoot().resolve("compiledFile/CompiledFileExample.kt")
2020
val content = Files.readAllLines(file).joinToString("\n")
21-
val parse = compiler.createFile(content, file)
21+
val parse = compiler.createKtFile(content, file)
2222
val classPath = CompilerClassPath(CompilerConfiguration())
2323
val sourcePath = listOf(parse)
24-
val (context, container) = compiler.compileFiles(sourcePath, sourcePath)
24+
val (context, container) = compiler.compileKtFiles(sourcePath, sourcePath)
2525
CompiledFile(content, parse, context, container, sourcePath, classPath)
2626
}
2727

server/src/test/kotlin/org/javacs/kt/CompilerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.junit.BeforeClass
1313
import java.nio.file.Files
1414

1515
class CompilerTest {
16-
val compiler = Compiler(setOf())
16+
val compiler = Compiler(setOf(), setOf())
1717
val myTestResources = testResourcesRoot().resolve("compiler")
1818
val file = myTestResources.resolve("FileToEdit.kt")
1919
val editedText = """

0 commit comments

Comments
 (0)