[documentation] extract DocumentationBrowserHistory from DocumentationBrowser

GitOrigin-RevId: 6fec825c39845dfbd548f11cb456e2128c159423
This commit is contained in:
Daniil Ovchinnikov
2022-02-09 12:17:22 +00:00
committed by intellij-monorepo-bot
parent 321e185aae
commit fc8922348c
2 changed files with 55 additions and 30 deletions
@@ -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<BrowserStateListener>(2)
private val backStack = Stack<HistorySnapshot>()
private val forwardStack = Stack<HistorySnapshot>()
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 {
@@ -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<T>(
private val snapshot: () -> T,
private val restore: (T) -> Unit,
) : DocumentationHistory {
private val backStack = Stack<T>()
private val forwardStack = Stack<T>()
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()
}
}