From 05c79ef42fd23332a920f57fbe31ee71ac1c7211 Mon Sep 17 00:00:00 2001 From: Dmitry Zhuravlev Date: Mon, 9 Sep 2024 15:46:44 +0200 Subject: [PATCH] [lvcs] extract local history facade API (IJPL-161713) GitOrigin-RevId: 0419a95c4dad771dfee05a6d1f0d100f30d6466e --- platform/lvcs-impl/api-dump.txt | 7 ++- .../resources/intellij.platform.lvcs.impl.xml | 3 +- .../history/core/LocalHistoryFacade.kt | 61 +++++++++++++------ .../history/integration/LocalHistoryImpl.kt | 24 +++----- .../core/InMemoryLocalHistoryFacade.java | 26 +++----- 5 files changed, 68 insertions(+), 53 deletions(-) diff --git a/platform/lvcs-impl/api-dump.txt b/platform/lvcs-impl/api-dump.txt index b72d429bf31b..7d81d95378da 100644 --- a/platform/lvcs-impl/api-dump.txt +++ b/platform/lvcs-impl/api-dump.txt @@ -72,12 +72,14 @@ com.intellij.history.core.LabelImpl - a:getByteContent(com.intellij.history.core.tree.RootEntry,java.lang.String):com.intellij.history.ByteContent - a:getLabelChangeId():J c:com.intellij.history.core.LocalHistoryFacade -- (com.intellij.history.core.ChangeList):V +- sf:Companion:com.intellij.history.core.LocalHistoryFacade$Companion +- ():V - f:accept(com.intellij.history.core.changes.ChangeVisitor):V - f:addChangeInTests(com.intellij.history.core.changes.StructuralChange):V - f:addListener(com.intellij.history.core.LocalHistoryFacade$Listener,com.intellij.openapi.Disposable):V - f:beginChangeSet():V - f:contentChanged(java.lang.String,com.intellij.history.core.Content,J):V +- p:createStorage():com.intellij.history.core.ChangeListStorage - f:created(java.lang.String,Z):V - f:deleted(java.lang.String,com.intellij.history.core.tree.Entry):V - f:endChangeSet(java.lang.String):V @@ -85,6 +87,7 @@ c:com.intellij.history.core.LocalHistoryFacade - bs:endChangeSet$default(com.intellij.history.core.LocalHistoryFacade,java.lang.String,com.intellij.history.ActivityId,I,java.lang.Object):V - f:forceBeginChangeSet():V - f:getChangeListInTests():com.intellij.history.core.ChangeList +- sf:getInstance():com.intellij.history.core.LocalHistoryFacade - f:moved(java.lang.String,java.lang.String):V - f:putLabelInTests(com.intellij.history.core.changes.PutLabelChange):V - f:putSystemLabel(java.lang.String,java.lang.String,I):com.intellij.history.core.LabelImpl @@ -94,6 +97,8 @@ c:com.intellij.history.core.LocalHistoryFacade - f:renamed(java.lang.String,java.lang.String):V - f:revertUpToChange(com.intellij.history.core.tree.RootEntry,J,java.lang.String,Z,Z):java.lang.String - f:revertUpToChangeSet(com.intellij.history.core.tree.RootEntry,J,java.lang.String,Z,Z):java.lang.String +f:com.intellij.history.core.LocalHistoryFacade$Companion +- f:getInstance():com.intellij.history.core.LocalHistoryFacade a:com.intellij.history.core.LocalHistoryFacade$Listener - ():V - changeAdded(com.intellij.history.core.changes.Change):V diff --git a/platform/lvcs-impl/resources/intellij.platform.lvcs.impl.xml b/platform/lvcs-impl/resources/intellij.platform.lvcs.impl.xml index 8a98e3eff858..0d832dcccbbf 100644 --- a/platform/lvcs-impl/resources/intellij.platform.lvcs.impl.xml +++ b/platform/lvcs-impl/resources/intellij.platform.lvcs.impl.xml @@ -81,6 +81,7 @@ + @@ -103,4 +104,4 @@ - \ No newline at end of file + diff --git a/platform/lvcs-impl/src/com/intellij/history/core/LocalHistoryFacade.kt b/platform/lvcs-impl/src/com/intellij/history/core/LocalHistoryFacade.kt index ca4c07250db4..70306855e549 100644 --- a/platform/lvcs-impl/src/com/intellij/history/core/LocalHistoryFacade.kt +++ b/platform/lvcs-impl/src/com/intellij/history/core/LocalHistoryFacade.kt @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2009 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. - */ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.history.core import com.intellij.history.ActivityId @@ -21,7 +7,10 @@ import com.intellij.history.core.changes.* import com.intellij.history.core.tree.Entry import com.intellij.history.core.tree.RootEntry import com.intellij.history.integration.IdeaGateway +import com.intellij.history.utils.LocalHistoryLog import com.intellij.openapi.Disposable +import com.intellij.openapi.application.PathManager +import com.intellij.openapi.components.service import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.project.Project @@ -30,11 +19,44 @@ import com.intellij.openapi.util.Disposer import com.intellij.openapi.util.NlsContexts import com.intellij.psi.codeStyle.MinusculeMatcher import com.intellij.psi.codeStyle.NameUtil +import com.intellij.util.application import com.intellij.util.containers.ContainerUtil +import com.intellij.util.io.delete import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.TestOnly +import java.nio.file.Path + +open class LocalHistoryFacade { + + private val storageDir: Path + get() = Path.of(PathManager.getSystemPath(), "LocalHistory") + + internal var changeList: ChangeList + private set + + init { + changeList = ChangeList(createStorage()) + } + + @TestOnly + internal fun reset() { + storageDir.delete() + changeList = ChangeList(createStorage()) + } + + protected open fun createStorage(): ChangeListStorage { + var storage: ChangeListStorage + try { + storage = ChangeListStorageImpl(storageDir) + } + catch (e: Throwable) { + LocalHistoryLog.LOG.warn("cannot create storage, in-memory implementation will be used", e) + storage = InMemoryChangeListStorage() + } + + return storage + } -open class LocalHistoryFacade(internal val changeList: ChangeList) { private val listeners: MutableList = ContainerUtil.createLockFreeCopyOnWriteList() @get:TestOnly @@ -188,6 +210,11 @@ open class LocalHistoryFacade(internal val changeList: ChangeList) { open fun changeAdded(c: Change) = Unit open fun changeSetFinished(changeSet: ChangeSet) = Unit } + + companion object { + @JvmStatic + fun getInstance(): LocalHistoryFacade = application.service() + } } @ApiStatus.Internal @@ -322,4 +349,4 @@ fun LocalHistoryFacade.processContents( if (before && !processContents(changeSet.id, path)) break } -} \ No newline at end of file +} diff --git a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.kt b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.kt index 85c23436085b..954bc87326a6 100644 --- a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.kt +++ b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryImpl.kt @@ -11,7 +11,6 @@ import com.intellij.history.integration.revertion.DifferenceReverter import com.intellij.history.utils.LocalHistoryLog import com.intellij.openapi.Disposable import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.application.PathManager import com.intellij.openapi.application.runReadAction import com.intellij.openapi.options.advanced.AdvancedSettings.Companion.getInt import com.intellij.openapi.options.advanced.AdvancedSettingsChangeListener @@ -25,13 +24,11 @@ import com.intellij.platform.lvcs.impl.RevisionId import com.intellij.platform.lvcs.impl.diff.findEntry import com.intellij.platform.lvcs.impl.operations.getRevertCommandName import com.intellij.util.SystemProperties -import com.intellij.util.io.delete import kotlinx.coroutines.* import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.TestOnly -import java.lang.Runnable -import java.nio.file.Path import java.util.concurrent.atomic.AtomicBoolean +import kotlin.Throws import kotlin.time.Duration.Companion.seconds @ApiStatus.Internal @@ -39,12 +36,13 @@ class LocalHistoryImpl(private val coroutineScope: CoroutineScope) : LocalHistor companion object { private const val DAYS_TO_KEEP = "localHistory.daysToKeep" + /** + * @see [LocalHistory.getInstance] + * @see [LocalHistoryFacade.getInstance] + */ @JvmStatic fun getInstanceImpl(): LocalHistoryImpl = getInstance() as LocalHistoryImpl - val storageDir: Path - get() = Path.of(PathManager.getSystemPath(), "LocalHistory") - private fun getProjectId(p: Project): String = p.getLocationHash() } @@ -113,15 +111,7 @@ class LocalHistoryImpl(private val coroutineScope: CoroutineScope) : LocalHistor } private fun initHistory() { - var storage: ChangeListStorage - try { - storage = ChangeListStorageImpl(storageDir) - } - catch (e: Throwable) { - LocalHistoryLog.LOG.warn("cannot create storage, in-memory implementation will be used", e) - storage = InMemoryChangeListStorage() - } - facade = LocalHistoryFacade(ChangeList(storage)) + facade = LocalHistoryFacade.getInstance() eventDispatcher = LocalHistoryEventDispatcher(facade!!, gateway) } @@ -151,7 +141,7 @@ class LocalHistoryImpl(private val coroutineScope: CoroutineScope) : LocalHistor @TestOnly fun cleanupForNextTest() { doDispose() - storageDir.delete() + facade?.reset() init() } diff --git a/platform/lvcs-impl/testSrc/com/intellij/history/core/InMemoryLocalHistoryFacade.java b/platform/lvcs-impl/testSrc/com/intellij/history/core/InMemoryLocalHistoryFacade.java index 673a392c1196..6cb6f8ad236b 100644 --- a/platform/lvcs-impl/testSrc/com/intellij/history/core/InMemoryLocalHistoryFacade.java +++ b/platform/lvcs-impl/testSrc/com/intellij/history/core/InMemoryLocalHistoryFacade.java @@ -1,23 +1,15 @@ -/* - * Copyright 2000-2009 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. - */ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.history.core; -public class InMemoryLocalHistoryFacade extends LocalHistoryFacade { +import org.jetbrains.annotations.NotNull; + +public final class InMemoryLocalHistoryFacade extends LocalHistoryFacade { public InMemoryLocalHistoryFacade() { - super(new ChangeList(new InMemoryChangeListStorage())); + } + + @Override + protected @NotNull ChangeListStorage createStorage() { + return new InMemoryChangeListStorage(); } }