diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowser.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowser.kt index b51bd0bb5099..c970ecbdcd91 100644 --- a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowser.kt +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowser.kt @@ -22,7 +22,6 @@ import com.intellij.openapi.project.IndexNotReadyException import com.intellij.openapi.project.Project import com.intellij.openapi.roots.OrderEntry import com.intellij.openapi.roots.ui.configuration.ProjectSettingsService -import com.intellij.util.containers.Stack import com.intellij.util.lateinitVal import com.intellij.util.ui.EDT import kotlinx.coroutines.* @@ -40,14 +39,11 @@ internal class DocumentationBrowser private constructor( private lateinit var state: BrowserState private val stateListeners = ArrayList(2) - private val backStack = Stack() - private val forwardStack = Stack() override fun dispose() { cs.cancel() stateListeners.clear() - backStack.clear() - forwardStack.clear() + myHistory.clear() } var ui: DocumentationUI by lateinitVal() @@ -79,8 +75,7 @@ internal class DocumentationBrowser private constructor( fun resetBrowser(request: DocumentationRequest) { cs.coroutineContext.cancelChildren() cs.launch(Dispatchers.EDT) { - backStack.clear() - forwardStack.clear() + myHistory.clear() browseDocumentation(request) } } @@ -127,8 +122,7 @@ internal class DocumentationBrowser private constructor( } } is InternalLinkResult.Request -> { - backStack.push(historySnapshot()) - forwardStack.clear() + myHistory.nextPage() browseDocumentation(internalResult.request) } is InternalLinkResult.Updater -> { @@ -154,6 +148,10 @@ internal class DocumentationBrowser private constructor( return result.getCompleted()?.links?.externalUrl } + val history: DocumentationHistory get() = myHistory + + private val myHistory = DocumentationBrowserHistory(::historySnapshot, ::restore) + private class HistorySnapshot( val state: BrowserState, val ui: UISnapshot, @@ -196,27 +194,6 @@ internal class DocumentationBrowser private constructor( } } - val history: DocumentationHistory = object : DocumentationHistory { - - override fun canBackward(): Boolean { - return !backStack.isEmpty() - } - - override fun backward() { - forwardStack.push(historySnapshot()) - restore(backStack.pop()) - } - - override fun canForward(): Boolean { - return !forwardStack.isEmpty() - } - - override fun forward() { - backStack.push(historySnapshot()) - restore(forwardStack.pop()) - } - } - companion object { fun createBrowser(project: Project, initialRequest: DocumentationRequest): DocumentationBrowser { diff --git a/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowserHistory.kt b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowserHistory.kt new file mode 100644 index 000000000000..05d4e0e0f071 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/lang/documentation/ide/impl/DocumentationBrowserHistory.kt @@ -0,0 +1,48 @@ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.lang.documentation.ide.impl + +import com.intellij.util.containers.Stack +import com.intellij.util.ui.EDT + +internal class DocumentationBrowserHistory( + private val snapshot: () -> T, + private val restore: (T) -> Unit, +) : DocumentationHistory { + + private val backStack = Stack() + private val forwardStack = Stack() + + override fun canBackward(): Boolean { + EDT.assertIsEdt() + return !backStack.isEmpty() + } + + override fun backward() { + EDT.assertIsEdt() + forwardStack.push(snapshot()) + restore(backStack.pop()) + } + + override fun canForward(): Boolean { + EDT.assertIsEdt() + return !forwardStack.isEmpty() + } + + override fun forward() { + EDT.assertIsEdt() + backStack.push(snapshot()) + restore(forwardStack.pop()) + } + + fun clear() { + EDT.assertIsEdt() + backStack.clear() + forwardStack.clear() + } + + fun nextPage() { + EDT.assertIsEdt() + backStack.push(snapshot()) + forwardStack.clear() + } +}