Skip to content

Commit 3c0de00

Browse files
authored
Merge pull request #585 from fwcd/make-codegen-optional
Make code generation (for Java interoperability) opt-in
2 parents febdf6b + c1e44a2 commit 3c0de00

File tree

7 files changed

+22
-3
lines changed

7 files changed

+22
-3
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.nio.file.Path
1818
class CompilerClassPath(
1919
private val config: CompilerConfiguration,
2020
private val scriptsConfig: ScriptsConfiguration,
21+
private val codegenConfig: CodegenConfiguration,
2122
private val databaseService: DatabaseService
2223
) : Closeable {
2324
val workspaceRoots = mutableSetOf<Path>()
@@ -33,6 +34,7 @@ class CompilerClassPath(
3334
classPath.map { it.compiledJar }.toSet(),
3435
buildScriptClassPath,
3536
scriptsConfig,
37+
codegenConfig,
3638
outputDirectory
3739
)
3840
private set
@@ -87,6 +89,7 @@ class CompilerClassPath(
8789
classPath.map { it.compiledJar }.toSet(),
8890
buildScriptClassPath,
8991
scriptsConfig,
92+
codegenConfig,
9093
outputDirectory
9194
)
9295
updateCompilerConfiguration()

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

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public data class SnippetsConfiguration(
1717
var enabled: Boolean = true
1818
)
1919

20+
public data class CodegenConfiguration(
21+
/** Whether to enable code generation to a temporary build directory for Java interoperability. */
22+
var enabled: Boolean = false
23+
)
24+
2025
public data class CompletionConfiguration(
2126
val snippets: SnippetsConfiguration = SnippetsConfiguration()
2227
)
@@ -100,6 +105,7 @@ class GsonPathConverter : JsonDeserializer<Path?> {
100105
}
101106

102107
public data class Configuration(
108+
val codegen: CodegenConfiguration = CodegenConfiguration(),
103109
val compiler: CompilerConfiguration = CompilerConfiguration(),
104110
val completion: CompletionConfiguration = CompletionConfiguration(),
105111
val diagnostics: DiagnosticsConfiguration = DiagnosticsConfiguration(),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KotlinLanguageServer(
2626
val config: Configuration = Configuration()
2727
) : LanguageServer, LanguageClientAware, Closeable {
2828
val databaseService = DatabaseService()
29-
val classPath = CompilerClassPath(config.compiler, config.scripts, databaseService)
29+
val classPath = CompilerClassPath(config.compiler, config.scripts, config.codegen, databaseService)
3030

3131
private val tempDirectory = TemporaryDirectory()
3232
private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath))))

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

+6
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ class KotlinWorkspaceService(
159159
sf.updateExclusions()
160160
}
161161

162+
// Update code generation options
163+
get("codegen")?.asJsonObject?.apply {
164+
val codegen = config.codegen
165+
get("enabled")?.asBoolean?.let { codegen.enabled = it }
166+
}
167+
162168
// Update code-completion options
163169
get("completion")?.asJsonObject?.apply {
164170
val completion = config.completion

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import kotlin.script.experimental.host.configurationDependencies
5757
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
5858
import kotlin.script.experimental.jvm.JvmDependency
5959
import org.javacs.kt.LOG
60+
import org.javacs.kt.CodegenConfiguration
6061
import org.javacs.kt.CompilerConfiguration
6162
import org.javacs.kt.ScriptsConfiguration
6263
import org.javacs.kt.util.KotlinLSException
@@ -460,6 +461,7 @@ class Compiler(
460461
classPath: Set<Path>,
461462
buildScriptClassPath: Set<Path> = emptySet(),
462463
scriptsConfig: ScriptsConfiguration,
464+
private val codegenConfig: CodegenConfiguration,
463465
private val outputDirectory: File,
464466
) : Closeable {
465467
private var closed = false
@@ -584,7 +586,7 @@ class Compiler(
584586
}
585587

586588
fun generateCode(module: ModuleDescriptor, bindingContext: BindingContext, files: Collection<KtFile>) {
587-
outputDirectory.let {
589+
outputDirectory.takeIf { codegenConfig.enabled }?.let {
588590
compileLock.withLock {
589591
val compileEnv = compileEnvironmentFor(CompilationKind.DEFAULT)
590592
val state = GenerationState.Builder(

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class CompiledFileTest {
3030
javaSourcePath = setOf(),
3131
classPath = setOf(),
3232
scriptsConfig = ScriptsConfiguration(),
33+
codegenConfig = CodegenConfiguration(),
3334
outputDirectory = outputDirectory
3435
).use { compiler ->
3536
val file = testResourcesRoot().resolve("compiledFile/CompiledFileExample.kt")
3637
val content = Files.readAllLines(file).joinToString("\n")
3738
val parse = compiler.createKtFile(content, file)
38-
val classPath = CompilerClassPath(CompilerConfiguration(), ScriptsConfiguration(), DatabaseService())
39+
val classPath = CompilerClassPath(CompilerConfiguration(), ScriptsConfiguration(), CodegenConfiguration(), DatabaseService())
3940
val sourcePath = listOf(parse)
4041
val (context, container) = compiler.compileKtFiles(sourcePath, sourcePath)
4142
CompiledFile(content, parse, context, container, sourcePath, classPath)

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

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private class FileToEdit {
3333
javaSourcePath = setOf(),
3434
classPath = setOf(),
3535
scriptsConfig = ScriptsConfiguration(),
36+
codegenConfig = CodegenConfiguration(),
3637
outputDirectory = outputDirectory
3738
)
3839
}

0 commit comments

Comments
 (0)