From 8b3eafcbf878d09aaa30f85be9727d09bc44f222 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 29 Jul 2024 01:24:51 +0200 Subject: [PATCH 1/3] Add codegen configuration --- server/src/main/kotlin/org/javacs/kt/Configuration.kt | 6 ++++++ .../src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/server/src/main/kotlin/org/javacs/kt/Configuration.kt b/server/src/main/kotlin/org/javacs/kt/Configuration.kt index 13774c38e..1880987d3 100644 --- a/server/src/main/kotlin/org/javacs/kt/Configuration.kt +++ b/server/src/main/kotlin/org/javacs/kt/Configuration.kt @@ -17,6 +17,11 @@ public data class SnippetsConfiguration( var enabled: Boolean = true ) +public data class CodegenConfiguration( + /** Whether to enable code generation to a temporary build directory for Java interoperability. */ + var enabled: Boolean = true +) + public data class CompletionConfiguration( val snippets: SnippetsConfiguration = SnippetsConfiguration() ) @@ -100,6 +105,7 @@ class GsonPathConverter : JsonDeserializer { } public data class Configuration( + val codegen: CodegenConfiguration = CodegenConfiguration(), val compiler: CompilerConfiguration = CompilerConfiguration(), val completion: CompletionConfiguration = CompletionConfiguration(), val diagnostics: DiagnosticsConfiguration = DiagnosticsConfiguration(), diff --git a/server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt b/server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt index 82ec771bd..de5afdfa0 100644 --- a/server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt +++ b/server/src/main/kotlin/org/javacs/kt/KotlinWorkspaceService.kt @@ -159,6 +159,12 @@ class KotlinWorkspaceService( sf.updateExclusions() } + // Update code generation options + get("codegen")?.asJsonObject?.apply { + val codegen = config.codegen + get("enabled")?.asBoolean?.let { codegen.enabled = it } + } + // Update code-completion options get("completion")?.asJsonObject?.apply { val completion = config.completion From 188f41f0107f7c53c06393071c34feab2c2570d1 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 29 Jul 2024 01:32:05 +0200 Subject: [PATCH 2/3] Only generate code if codegen is enabled --- server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt | 3 +++ server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt | 2 +- server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt | 4 +++- server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt | 3 ++- server/src/test/kotlin/org/javacs/kt/CompilerTest.kt | 1 + 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt b/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt index d3ef00e2a..794377454 100644 --- a/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt +++ b/server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt @@ -18,6 +18,7 @@ import java.nio.file.Path class CompilerClassPath( private val config: CompilerConfiguration, private val scriptsConfig: ScriptsConfiguration, + private val codegenConfig: CodegenConfiguration, private val databaseService: DatabaseService ) : Closeable { val workspaceRoots = mutableSetOf() @@ -33,6 +34,7 @@ class CompilerClassPath( classPath.map { it.compiledJar }.toSet(), buildScriptClassPath, scriptsConfig, + codegenConfig, outputDirectory ) private set @@ -87,6 +89,7 @@ class CompilerClassPath( classPath.map { it.compiledJar }.toSet(), buildScriptClassPath, scriptsConfig, + codegenConfig, outputDirectory ) updateCompilerConfiguration() diff --git a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt index 29998dc3a..e8da0ff99 100644 --- a/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt +++ b/server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt @@ -26,7 +26,7 @@ class KotlinLanguageServer( val config: Configuration = Configuration() ) : LanguageServer, LanguageClientAware, Closeable { val databaseService = DatabaseService() - val classPath = CompilerClassPath(config.compiler, config.scripts, databaseService) + val classPath = CompilerClassPath(config.compiler, config.scripts, config.codegen, databaseService) private val tempDirectory = TemporaryDirectory() private val uriContentProvider = URIContentProvider(ClassContentProvider(config.externalSources, classPath, tempDirectory, CompositeSourceArchiveProvider(JdkSourceArchiveProvider(classPath), ClassPathSourceArchiveProvider(classPath)))) diff --git a/server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt b/server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt index eef04b1dd..f81d1f0ee 100644 --- a/server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt +++ b/server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt @@ -57,6 +57,7 @@ import kotlin.script.experimental.host.configurationDependencies import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration import kotlin.script.experimental.jvm.JvmDependency import org.javacs.kt.LOG +import org.javacs.kt.CodegenConfiguration import org.javacs.kt.CompilerConfiguration import org.javacs.kt.ScriptsConfiguration import org.javacs.kt.util.KotlinLSException @@ -460,6 +461,7 @@ class Compiler( classPath: Set, buildScriptClassPath: Set = emptySet(), scriptsConfig: ScriptsConfiguration, + private val codegenConfig: CodegenConfiguration, private val outputDirectory: File, ) : Closeable { private var closed = false @@ -584,7 +586,7 @@ class Compiler( } fun generateCode(module: ModuleDescriptor, bindingContext: BindingContext, files: Collection) { - outputDirectory.let { + outputDirectory.takeIf { codegenConfig.enabled }?.let { compileLock.withLock { val compileEnv = compileEnvironmentFor(CompilationKind.DEFAULT) val state = GenerationState.Builder( diff --git a/server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt b/server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt index a1bd15bcb..c6552e272 100644 --- a/server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt +++ b/server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt @@ -30,12 +30,13 @@ class CompiledFileTest { javaSourcePath = setOf(), classPath = setOf(), scriptsConfig = ScriptsConfiguration(), + codegenConfig = CodegenConfiguration(), outputDirectory = outputDirectory ).use { compiler -> val file = testResourcesRoot().resolve("compiledFile/CompiledFileExample.kt") val content = Files.readAllLines(file).joinToString("\n") val parse = compiler.createKtFile(content, file) - val classPath = CompilerClassPath(CompilerConfiguration(), ScriptsConfiguration(), DatabaseService()) + val classPath = CompilerClassPath(CompilerConfiguration(), ScriptsConfiguration(), CodegenConfiguration(), DatabaseService()) val sourcePath = listOf(parse) val (context, container) = compiler.compileKtFiles(sourcePath, sourcePath) CompiledFile(content, parse, context, container, sourcePath, classPath) diff --git a/server/src/test/kotlin/org/javacs/kt/CompilerTest.kt b/server/src/test/kotlin/org/javacs/kt/CompilerTest.kt index 6ae232cfe..acfa569e4 100644 --- a/server/src/test/kotlin/org/javacs/kt/CompilerTest.kt +++ b/server/src/test/kotlin/org/javacs/kt/CompilerTest.kt @@ -33,6 +33,7 @@ private class FileToEdit { javaSourcePath = setOf(), classPath = setOf(), scriptsConfig = ScriptsConfiguration(), + codegenConfig = CodegenConfiguration(), outputDirectory = outputDirectory ) } From c1e44a2533aa3b21eb2b453ed04d2c485746e9f6 Mon Sep 17 00:00:00 2001 From: fwcd Date: Mon, 29 Jul 2024 01:32:23 +0200 Subject: [PATCH 3/3] Disable codegen by default --- server/src/main/kotlin/org/javacs/kt/Configuration.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/kotlin/org/javacs/kt/Configuration.kt b/server/src/main/kotlin/org/javacs/kt/Configuration.kt index 1880987d3..58d615ab2 100644 --- a/server/src/main/kotlin/org/javacs/kt/Configuration.kt +++ b/server/src/main/kotlin/org/javacs/kt/Configuration.kt @@ -19,7 +19,7 @@ public data class SnippetsConfiguration( public data class CodegenConfiguration( /** Whether to enable code generation to a temporary build directory for Java interoperability. */ - var enabled: Boolean = true + var enabled: Boolean = false ) public data class CompletionConfiguration(