IDEA-360318 Spring Structure View: wrong view after reopening IDEA

(cherry picked from commit 9483a6374ff19de88d4c399ee1b40240904edc49)

IJ-CR-149597

GitOrigin-RevId: f8b24cd73c00220c22b263bc99b939c3c44c0654
This commit is contained in:
Anton Kozub
2024-11-18 19:13:36 +00:00
committed by intellij-monorepo-bot
parent 5e6cc9697d
commit 829b85afd1
2 changed files with 43 additions and 5 deletions
@@ -10,6 +10,7 @@ import com.intellij.ide.structureView.StructureViewWrapper
import com.intellij.ide.structureView.TextEditorBasedStructureViewModel
import com.intellij.ide.structureView.impl.StructureViewComposite
import com.intellij.ide.structureView.impl.StructureViewComposite.StructureViewDescriptor
import com.intellij.ide.structureView.impl.StructureViewState
import com.intellij.ide.structureView.newStructureView.StructureViewComponent
import com.intellij.lang.LangBundle
import com.intellij.lang.PsiStructureViewFactory
@@ -28,6 +29,7 @@ import com.intellij.openapi.module.ModuleType
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectCloseListener
import com.intellij.openapi.project.impl.ProjectManagerImpl.Companion.isLight
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.util.Comparing
@@ -72,9 +74,11 @@ import javax.swing.JPanel
import javax.swing.SwingUtilities
@OptIn(FlowPreview::class)
class StructureViewWrapperImpl(private val project: Project,
private val myToolWindow: ToolWindow,
private val coroutineScope: CoroutineScope) : StructureViewWrapper, Disposable {
class StructureViewWrapperImpl(
private val project: Project,
private val myToolWindow: ToolWindow,
private val coroutineScope: CoroutineScope,
) : StructureViewWrapper, Disposable {
private var myFile: VirtualFile? = null
private var myStructureView: StructureView? = null
private var myFileEditor: FileEditor? = null
@@ -155,6 +159,11 @@ class StructureViewWrapperImpl(private val project: Project,
PsiStructureViewFactory.EP_NAME.addChangeListener({ clearCaches() }, this)
StructureViewBuilder.EP_NAME.addChangeListener({ clearCaches() }, this)
ApplicationManager.getApplication().messageBus.connect(this).subscribe(STRUCTURE_CHANGED, Runnable { clearCaches() })
ApplicationManager.getApplication().messageBus.connect(this).subscribe(ProjectCloseListener.TOPIC, object : ProjectCloseListener {
override fun projectClosingBeforeSave(project: Project) {
StructureViewState.getInstance(project).selectedTab = myToolWindow.contentManager.selectedContent?.tabName
}
})
coroutineScope.launch {
rebuildRequests
@@ -358,7 +367,7 @@ class StructureViewWrapperImpl(private val project: Project,
myStructureView!!.storeState()
contentManager.selectedContent?.tabName
?.takeIf { it.isNotEmpty() }
?.let { project.putUserData(STRUCTURE_VIEW_SELECTED_TAB_KEY, it) }
?.let { StructureViewState.getInstance(project).selectedTab = it }
Disposer.dispose(myStructureView!!)
myStructureView = null
myFileEditor = null
@@ -440,7 +449,7 @@ class StructureViewWrapperImpl(private val project: Project,
for (i in panels.indices) {
val content = ContentFactory.getInstance().createContent(panels[i], names[i], false)
contentManager.addContent(content)
val previouslySelectedTab = project.getUserData(STRUCTURE_VIEW_SELECTED_TAB_KEY)
val previouslySelectedTab = StructureViewState.getInstance(project).selectedTab
if (panels.size > 1 && names[i] == previouslySelectedTab) {
contentManager.setSelectedContent(content)
}
@@ -0,0 +1,29 @@
// 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.ide.structureView.impl
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.util.xmlb.XmlSerializerUtil
@Service(Service.Level.PROJECT)
@State(name = "StructureViewState", storages = [(Storage(value = StoragePathMacros.WORKSPACE_FILE))])
internal class StructureViewState : PersistentStateComponent<StructureViewState> {
companion object {
@JvmStatic
fun getInstance(project: Project): StructureViewState = project.service()
@JvmStatic
fun getDefaultInstance(): StructureViewState = getInstance(ProjectManager.getInstance().defaultProject)
}
var selectedTab: String? = null
override fun loadState(state: StructureViewState) {
XmlSerializerUtil.copyBean(state, this)
}
override fun getState(): StructureViewState {
return this
}
}