Skip to content

Commit 4f26dc7

Browse files
committed
add support for multiplatform projects.
todo: should/can the compiler args be read from the gradle files?
1 parent e39ab58 commit 4f26dc7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ import kotlin.script.experimental.jvm.JvmDependency
7272
import org.javacs.kt.util.KotlinLSException
7373
import org.javacs.kt.util.KotlinNullableNotNullManager
7474
import org.javacs.kt.util.LoggingMessageCollector
75+
import org.jetbrains.kotlin.config.*
76+
import java.util.*
77+
7578

7679
private val GRADLE_DSL_DEPENDENCY_PATTERN = Regex("^gradle-(?:kotlin-dsl|core).*\\.jar$")
7780

@@ -93,7 +96,17 @@ private class CompilationEnvironment(
9396
parentDisposable = disposable,
9497
// Not to be confused with the CompilerConfiguration in the language server Configuration
9598
configuration = KotlinCompilerConfiguration().apply {
99+
val langFeatures = mutableMapOf<LanguageFeature, LanguageFeature.State>()
100+
langFeatures[LanguageFeature.MultiPlatformProjects] = LanguageFeature.State.ENABLED
101+
val languageVersionSettings = LanguageVersionSettingsImpl(
102+
LanguageVersion.LATEST_STABLE,
103+
ApiVersion.createByLanguageVersion(LanguageVersion.LATEST_STABLE),
104+
emptyMap(),
105+
langFeatures
106+
)
107+
96108
put(CommonConfigurationKeys.MODULE_NAME, JvmProtoBufUtil.DEFAULT_MODULE_NAME)
109+
put(CommonConfigurationKeys.LANGUAGE_VERSION_SETTINGS, languageVersionSettings)
97110
put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, LoggingMessageCollector)
98111
add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, ScriptingCompilerConfigurationComponentRegistrar())
99112
addJvmClasspathRoots(classPath.map { it.toFile() })
@@ -331,3 +344,49 @@ private fun describeExpression(expression: String): String = expression.lines().
331344
(lines.take(3) + listOf("...", lines.last())).joinToString(separator = "\n")
332345
}
333346
}
347+
348+
class LanguageVersionSettingsImpl @JvmOverloads constructor(
349+
override val languageVersion: LanguageVersion,
350+
override val apiVersion: ApiVersion,
351+
analysisFlags: Map<AnalysisFlag<*>, Any?> = emptyMap(),
352+
specificFeatures: Map<LanguageFeature, LanguageFeature.State> = emptyMap()
353+
) : LanguageVersionSettings {
354+
private val analysisFlags: Map<AnalysisFlag<*>, *> = Collections.unmodifiableMap(analysisFlags)
355+
private val specificFeatures: Map<LanguageFeature, LanguageFeature.State> = Collections.unmodifiableMap(specificFeatures)
356+
357+
@Suppress("UNCHECKED_CAST")
358+
override fun <T> getFlag(flag: AnalysisFlag<T>): T = analysisFlags[flag] as T? ?: flag.defaultValue
359+
360+
override fun getFeatureSupport(feature: LanguageFeature): LanguageFeature.State {
361+
specificFeatures[feature]?.let { return it }
362+
363+
val since = feature.sinceVersion
364+
if (since != null && languageVersion >= since && apiVersion >= feature.sinceApiVersion) {
365+
return feature.defaultState
366+
}
367+
368+
return LanguageFeature.State.DISABLED
369+
}
370+
371+
override fun toString() = buildString {
372+
append("Language = $languageVersion, API = $apiVersion")
373+
specificFeatures.forEach { (feature, state) ->
374+
val char = when (state) {
375+
LanguageFeature.State.ENABLED -> '+'
376+
LanguageFeature.State.ENABLED_WITH_WARNING -> '~'
377+
LanguageFeature.State.ENABLED_WITH_ERROR, LanguageFeature.State.DISABLED -> '-'
378+
}
379+
append(" $char$feature")
380+
}
381+
}
382+
383+
override fun isPreRelease(): Boolean = languageVersion.isPreRelease() ||
384+
specificFeatures.any { (feature, state) ->
385+
state == LanguageFeature.State.ENABLED && feature.forcesPreReleaseBinariesIfEnabled()
386+
}
387+
388+
companion object {
389+
@JvmField
390+
val DEFAULT = LanguageVersionSettingsImpl(LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE)
391+
}
392+
}

0 commit comments

Comments
 (0)