extract PsiVFSModuleRootListener to toplevel

GitOrigin-RevId: ed324d0501686ff0e998107ce71d36ecf63b8c38
This commit is contained in:
Max Medvedev
2024-08-13 11:44:36 +02:00
committed by intellij-monorepo-bot
parent d43ac97ddb
commit 170895dc20
2 changed files with 56 additions and 56 deletions

View File

@@ -504,61 +504,6 @@ class PsiVFSListener internal constructor(private val project: Project) {
)
}
internal class MyModuleRootListener(private val listenerProject: Project) : ModuleRootListener {
// accessed from within write action only
private var depthCounter = 0
override fun beforeRootsChange(event: ModuleRootEvent) {
LOG.trace { "beforeRootsChanged call" }
if (event.isCausedByFileTypesChange) {
return
}
LOG.trace { "Event is not caused by file types change" }
ApplicationManager.getApplication().runWriteAction(ExternalChangeAction {
depthCounter++
LOG.trace { "depthCounter increased $depthCounter" }
if (depthCounter > 1) {
return@ExternalChangeAction
}
val psiManager = PsiManager.getInstance(listenerProject) as PsiManagerImpl
val treeEvent = PsiTreeChangeEventImpl(psiManager)
treeEvent.propertyName = PsiTreeChangeEvent.PROP_ROOTS
psiManager.beforePropertyChange(treeEvent)
})
}
override fun rootsChanged(event: ModuleRootEvent) {
LOG.trace { "rootsChanged call" }
val psiManager = PsiManager.getInstance(listenerProject) as PsiManagerImpl
val fileManager = psiManager.fileManager as FileManagerImpl
fileManager.dispatchPendingEvents()
if (event.isCausedByFileTypesChange) {
return
}
LOG.trace { "Event is not caused by file types change" }
ApplicationManager.getApplication().runWriteAction(
ExternalChangeAction {
depthCounter--
LOG.trace { "depthCounter decreased $depthCounter" }
assert(depthCounter >= 0) { "unbalanced `beforeRootsChange`/`rootsChanged`: $depthCounter" }
if (depthCounter > 0) {
return@ExternalChangeAction
}
DebugUtil.performPsiModification<RuntimeException>(null) { fileManager.possiblyInvalidatePhysicalPsi() }
val treeEvent = PsiTreeChangeEventImpl(psiManager)
treeEvent.propertyName = PsiTreeChangeEvent.PROP_ROOTS
psiManager.propertyChanged(treeEvent)
}
)
}
}
internal fun handleVfsChangeWithoutPsi(vFile: VirtualFile) {
if (!reportedUnloadedPsiChange && isInRootModel(vFile)) {
fileManager.firePropertyChangedForUnloadedPsi()
@@ -630,6 +575,61 @@ class PsiVFSListener internal constructor(private val project: Project) {
}
}
internal class PsiVFSModuleRootListener(private val listenerProject: Project) : ModuleRootListener {
// accessed from within write action only
private var depthCounter = 0
override fun beforeRootsChange(event: ModuleRootEvent) {
LOG.trace { "beforeRootsChanged call" }
if (event.isCausedByFileTypesChange) {
return
}
LOG.trace { "Event is not caused by file types change" }
ApplicationManager.getApplication().runWriteAction(ExternalChangeAction {
depthCounter++
LOG.trace { "depthCounter increased $depthCounter" }
if (depthCounter > 1) {
return@ExternalChangeAction
}
val psiManager = PsiManager.getInstance(listenerProject) as PsiManagerImpl
val treeEvent = PsiTreeChangeEventImpl(psiManager)
treeEvent.propertyName = PsiTreeChangeEvent.PROP_ROOTS
psiManager.beforePropertyChange(treeEvent)
})
}
override fun rootsChanged(event: ModuleRootEvent) {
LOG.trace { "rootsChanged call" }
val psiManager = PsiManager.getInstance(listenerProject) as PsiManagerImpl
val fileManager = psiManager.fileManager as FileManagerImpl
fileManager.dispatchPendingEvents()
if (event.isCausedByFileTypesChange) {
return
}
LOG.trace { "Event is not caused by file types change" }
ApplicationManager.getApplication().runWriteAction(
ExternalChangeAction {
depthCounter--
LOG.trace { "depthCounter decreased $depthCounter" }
assert(depthCounter >= 0) { "unbalanced `beforeRootsChange`/`rootsChanged`: $depthCounter" }
if (depthCounter > 0) {
return@ExternalChangeAction
}
DebugUtil.performPsiModification<RuntimeException>(null) { fileManager.possiblyInvalidatePhysicalPsi() }
val treeEvent = PsiTreeChangeEventImpl(psiManager)
treeEvent.propertyName = PsiTreeChangeEvent.PROP_ROOTS
psiManager.propertyChanged(treeEvent)
}
)
}
}
private class MyFileDocumentManagerListener(private val project: Project) : FileDocumentManagerListener {
private val fileManager = PsiManagerEx.getInstanceEx(project).fileManager as FileManagerImpl

View File

@@ -1797,7 +1797,7 @@
topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>
<listener class="com.intellij.lang.documentation.ide.impl.DocumentationAutoPopupListener"
topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>
<listener class="com.intellij.psi.impl.file.impl.PsiVFSListener$MyModuleRootListener"
<listener class="com.intellij.psi.impl.file.impl.PsiVFSModuleRootListener"
topic="com.intellij.openapi.roots.ModuleRootListener"/>
<listener class="com.intellij.workspaceModel.ide.impl.legacyBridge.facet.FacetEntityChangeListener$WorkspaceModelListener"
topic="com.intellij.platform.backend.workspace.WorkspaceModelChangeListener"/>