diff --git a/platform/configuration-store-impl/src/ComponentStoreImpl.kt b/platform/configuration-store-impl/src/ComponentStoreImpl.kt index 0a9d847e3b85..be72f34d9085 100644 --- a/platform/configuration-store-impl/src/ComponentStoreImpl.kt +++ b/platform/configuration-store-impl/src/ComponentStoreImpl.kt @@ -33,7 +33,6 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.util import com.intellij.openapi.util.* import com.intellij.openapi.util.Pair -import com.intellij.openapi.util.registry.Registry import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess import com.intellij.util.ArrayUtilRt @@ -249,7 +248,7 @@ abstract class ComponentStoreImpl : IComponentStore { } val storage = storageManager.getStateStorage(storageSpec) - var stateGetter = if (Registry.`is`("use.loaded.state.as.existing", false)) (storage as? StorageBaseEx<*>)?.createGetSession(component, name, stateClass) else null + var stateGetter = (storage as? StorageBaseEx<*>)?.createGetSession(component, name, stateClass) var state = if (stateGetter == null) storage.getState(component, name, stateClass, defaultState, reloadData) else stateGetter.getState(defaultState) if (state == null) { if (changedStorages != null && changedStorages.contains(storage)) { @@ -262,12 +261,7 @@ abstract class ComponentStoreImpl : IComponentStore { } } - try { - component.loadState(state) - } - finally { - stateGetter?.close() - } + stateGetter?.use { component.loadState(state) } return name } diff --git a/platform/configuration-store-impl/src/StorageBaseEx.kt b/platform/configuration-store-impl/src/StorageBaseEx.kt index 94affaa5693e..217b257d5f35 100644 --- a/platform/configuration-store-impl/src/StorageBaseEx.kt +++ b/platform/configuration-store-impl/src/StorageBaseEx.kt @@ -15,9 +15,12 @@ */ package com.intellij.configurationStore +import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.components.PersistentStateComponent import com.intellij.openapi.components.impl.stores.StateStorageBase +import com.intellij.openapi.util.registry.Registry import org.jdom.Element +import java.io.Closeable abstract class StorageBaseEx : StateStorageBase() { fun createGetSession(component: PersistentStateComponent, componentName: String, stateClass: Class, reload: Boolean = false) = StateGetter(component, componentName, getStorageData(reload), stateClass, this) @@ -28,7 +31,7 @@ abstract class StorageBaseEx : StateStorageBase() { abstract fun archiveState(storageData: T, componentName: String, serializedState: Element?) } -class StateGetter(private val component: PersistentStateComponent, private val componentName: String, private val storageData: T, private val stateClass: Class, private val storage: StorageBaseEx) { +class StateGetter(private val component: PersistentStateComponent, private val componentName: String, private val storageData: T, private val stateClass: Class, private val storage: StorageBaseEx) : Closeable { var serializedState: Element? = null fun getState(mergeInto: S? = null): S? { @@ -41,10 +44,7 @@ class StateGetter(private val component: PersistentStateCompon return storage.deserializeState(serializedState, stateClass, mergeInto) } - /** - * nullable - because PersistentStateComponent can return nullable state - */ - fun close() { + override fun close() { if (serializedState == null) { return } @@ -53,7 +53,7 @@ class StateGetter(private val component: PersistentStateCompon val stateAfterLoad: S? try { - stateAfterLoad = component.getState() + stateAfterLoad = if (ApplicationManager.getApplication().isUnitTestMode() || Registry.`is`("use.loaded.state.as.existing", false)) component.getState() else null } catch(e: Throwable) { LOG.error("Cannot get state after load", e) diff --git a/platform/configuration-store-impl/src/XmlElementStorage.kt b/platform/configuration-store-impl/src/XmlElementStorage.kt index 08f77b834051..67bed1a75dac 100644 --- a/platform/configuration-store-impl/src/XmlElementStorage.kt +++ b/platform/configuration-store-impl/src/XmlElementStorage.kt @@ -20,7 +20,6 @@ import com.intellij.openapi.components.StateStorage import com.intellij.openapi.components.TrackingPathMacroSubstitutor import com.intellij.openapi.components.impl.stores.FileStorageCoreUtil import com.intellij.openapi.util.JDOMUtil -import com.intellij.openapi.util.registry.Registry import com.intellij.util.containers.ContainerUtil import com.intellij.util.containers.SmartHashSet import gnu.trove.THashMap @@ -38,9 +37,7 @@ abstract class XmlElementStorage protected constructor(protected val fileSpec: S protected abstract fun loadLocalData(): Element? - override final fun getState(storageData: StateMap, component: Any?, componentName: String): Element? { - return if (Registry.`is`("use.loaded.state.as.existing", false)) storageData.getState(componentName) else storageData.getStateAndArchive(componentName) - } + override final fun getState(storageData: StateMap, component: Any?, componentName: String) = storageData.getState(componentName) override fun archiveState(storageData: StateMap, componentName: String, serializedState: Element?) { storageData.archive(componentName, serializedState)