Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code generation (for Java interoperability) opt-in #585

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>()
Expand All @@ -33,6 +34,7 @@ class CompilerClassPath(
classPath.map { it.compiledJar }.toSet(),
buildScriptClassPath,
scriptsConfig,
codegenConfig,
outputDirectory
)
private set
Expand Down Expand Up @@ -87,6 +89,7 @@ class CompilerClassPath(
classPath.map { it.compiledJar }.toSet(),
buildScriptClassPath,
scriptsConfig,
codegenConfig,
outputDirectory
)
updateCompilerConfiguration()
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = false
)

public data class CompletionConfiguration(
val snippets: SnippetsConfiguration = SnippetsConfiguration()
)
Expand Down Expand Up @@ -100,6 +105,7 @@ class GsonPathConverter : JsonDeserializer<Path?> {
}

public data class Configuration(
val codegen: CodegenConfiguration = CodegenConfiguration(),
val compiler: CompilerConfiguration = CompilerConfiguration(),
val completion: CompletionConfiguration = CompletionConfiguration(),
val diagnostics: DiagnosticsConfiguration = DiagnosticsConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion server/src/main/kotlin/org/javacs/kt/compiler/Compiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -460,6 +461,7 @@ class Compiler(
classPath: Set<Path>,
buildScriptClassPath: Set<Path> = emptySet(),
scriptsConfig: ScriptsConfiguration,
private val codegenConfig: CodegenConfiguration,
private val outputDirectory: File,
) : Closeable {
private var closed = false
Expand Down Expand Up @@ -584,7 +586,7 @@ class Compiler(
}

fun generateCode(module: ModuleDescriptor, bindingContext: BindingContext, files: Collection<KtFile>) {
outputDirectory.let {
outputDirectory.takeIf { codegenConfig.enabled }?.let {
compileLock.withLock {
val compileEnv = compileEnvironmentFor(CompilationKind.DEFAULT)
val state = GenerationState.Builder(
Expand Down
3 changes: 2 additions & 1 deletion server/src/test/kotlin/org/javacs/kt/CompiledFileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions server/src/test/kotlin/org/javacs/kt/CompilerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private class FileToEdit {
javaSourcePath = setOf(),
classPath = setOf(),
scriptsConfig = ScriptsConfiguration(),
codegenConfig = CodegenConfiguration(),
outputDirectory = outputDirectory
)
}
Expand Down
Loading