Refactor skipMetadataVersionCheck flag

To make addition of other flags easier in the future, provide a more
abstract 'isFlagEnabled' in LanguageVersionSettings
This commit is contained in:
Alexander Udalov
2017-03-14 15:08:54 +03:00
parent 7a240b63c7
commit 56201a6dc4
7 changed files with 73 additions and 27 deletions
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.config
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class AnalysisFlag internal constructor(private val name: String) {
override fun equals(other: Any?): Boolean = other is AnalysisFlag && other.name == name
override fun hashCode(): Int = name.hashCode()
override fun toString(): String = name
companion object
}
private operator fun AnalysisFlag.Companion.provideDelegate(instance: Any?, property: KProperty<*>) =
object : ReadOnlyProperty<Any?, AnalysisFlag> {
private val flag = AnalysisFlag(property.name)
override fun getValue(thisRef: Any?, property: KProperty<*>): AnalysisFlag = flag
}
object AnalysisFlags {
@JvmStatic
val skipMetadataVersionCheck by AnalysisFlag
}
@@ -106,20 +106,32 @@ interface LanguageVersionSettings {
fun supportsFeature(feature: LanguageFeature): Boolean =
getFeatureSupport(feature).let { it == LanguageFeature.State.ENABLED || it == LanguageFeature.State.ENABLED_WITH_WARNING }
fun isFlagEnabled(flag: AnalysisFlag): Boolean
val apiVersion: ApiVersion
// Please do not use this to enable/disable specific features/checks. Instead add a new LanguageFeature entry and call supportsFeature
val languageVersion: LanguageVersion
val skipMetadataVersionCheck: Boolean
}
class LanguageVersionSettingsImpl @JvmOverloads constructor(
override val languageVersion: LanguageVersion,
override val apiVersion: ApiVersion,
override val skipMetadataVersionCheck: Boolean = false,
private val specificFeatures: Map<LanguageFeature, LanguageFeature.State> = emptyMap()
) : LanguageVersionSettings {
private val enabledFlags = hashSetOf<AnalysisFlag>()
override fun isFlagEnabled(flag: AnalysisFlag): Boolean = flag in enabledFlags
fun switchFlag(flag: AnalysisFlag, enable: Boolean) {
if (enable) {
enabledFlags.add(flag)
}
else {
enabledFlags.remove(flag)
}
}
override fun getFeatureSupport(feature: LanguageFeature): LanguageFeature.State {
specificFeatures[feature]?.let { return it }