Do not coerce inferred language levels when importing from facet

Rationaly is that facet importer knows better about which language
versions should be used, so we shouldn't interfere.

Otherwise, Gradle or Maven projects which have upgraded corresponding
plugin to 1.3 will be imported with 1.2 levels, which is undesirable
This commit is contained in:
Dmitry Savvinov
2018-08-28 20:41:30 +03:00
committed by Ilya Gorbunov
parent 90ec151258
commit 2ecea06d3e
2 changed files with 23 additions and 9 deletions
@@ -52,20 +52,24 @@ fun getRuntimeLibraryVersions(
fun getLibraryLanguageLevel( fun getLibraryLanguageLevel(
module: Module, module: Module,
rootModel: ModuleRootModel?, rootModel: ModuleRootModel?,
targetPlatform: TargetPlatformKind<*>? targetPlatform: TargetPlatformKind<*>?,
coerceRuntimeLibraryVersionToReleased: Boolean = true
): LanguageVersion { ): LanguageVersion {
val minVersion = (getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM) + val minVersion = getRuntimeLibraryVersions(module, rootModel, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM)
VersionView.RELEASED_VERSION.versionString).minWith(VersionComparatorUtil.COMPARATOR) .addReleaseVersionIfNecessary(coerceRuntimeLibraryVersionToReleased)
return getDefaultLanguageLevel(module, minVersion) .minWith(VersionComparatorUtil.COMPARATOR)
return getDefaultLanguageLevel(module, minVersion, coerceRuntimeLibraryVersionToReleased)
} }
fun getDefaultLanguageLevel( fun getDefaultLanguageLevel(
module: Module, module: Module,
explicitVersion: String? = null explicitVersion: String? = null,
coerceRuntimeLibraryVersionToReleased: Boolean = true
): LanguageVersion { ): LanguageVersion {
val libVersion = explicitVersion val libVersion = explicitVersion
?: (KotlinVersionInfoProvider.EP_NAME.extensions ?: KotlinVersionInfoProvider.EP_NAME.extensions
.mapNotNull { it.getCompilerVersion(module) } + VersionView.RELEASED_VERSION.versionString) .mapNotNull { it.getCompilerVersion(module) }
.addReleaseVersionIfNecessary(coerceRuntimeLibraryVersionToReleased)
.minWith(VersionComparatorUtil.COMPARATOR) .minWith(VersionComparatorUtil.COMPARATOR)
?: return VersionView.RELEASED_VERSION ?: return VersionView.RELEASED_VERSION
return when { return when {
@@ -77,6 +81,9 @@ fun getDefaultLanguageLevel(
} }
} }
private fun Iterable<String>.addReleaseVersionIfNecessary(shouldAdd: Boolean): Iterable<String> =
if (shouldAdd) this + VersionView.RELEASED_VERSION.versionString else this
fun getRuntimeLibraryVersion(module: Module): String? { fun getRuntimeLibraryVersion(module: Module): String? {
val targetPlatform = KotlinFacetSettingsProvider.getInstance(module.project).getInitializedSettings(module).targetPlatformKind val targetPlatform = KotlinFacetSettingsProvider.getInstance(module.project).getInitializedSettings(module).targetPlatformKind
val versions = getRuntimeLibraryVersions(module, null, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM) val versions = getRuntimeLibraryVersions(module, null, targetPlatform ?: TargetPlatformKind.DEFAULT_PLATFORM)
@@ -82,14 +82,21 @@ fun KotlinFacetSettings.initializeIfNeeded(
if (shouldInferLanguageLevel) { if (shouldInferLanguageLevel) {
languageLevel = (if (useProjectSettings) LanguageVersion.fromVersionString(commonArguments.languageVersion) else null) languageLevel = (if (useProjectSettings) LanguageVersion.fromVersionString(commonArguments.languageVersion) else null)
?: getDefaultLanguageLevel(module, compilerVersion) ?: getDefaultLanguageLevel(module, compilerVersion, coerceRuntimeLibraryVersionToReleased = compilerVersion == null)
} }
if (shouldInferAPILevel) { if (shouldInferAPILevel) {
apiLevel = if (useProjectSettings) { apiLevel = if (useProjectSettings) {
LanguageVersion.fromVersionString(commonArguments.apiVersion) ?: languageLevel LanguageVersion.fromVersionString(commonArguments.apiVersion) ?: languageLevel
} else { } else {
languageLevel!!.coerceAtMost(getLibraryLanguageLevel(module, rootModel, targetPlatformKind)) languageLevel!!.coerceAtMost(
getLibraryLanguageLevel(
module,
rootModel,
targetPlatformKind,
coerceRuntimeLibraryVersionToReleased = compilerVersion == null
)
)
} }
} }
} }