jetbrains – refresh workspace dirs

This commit is contained in:
mq200 2025-09-25 10:12:03 -07:00
parent 60419a6cbd
commit fc8bcf6c61

View file

@ -29,7 +29,9 @@ import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import javax.swing.*
import com.intellij.openapi.components.service
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.ModuleListener
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
@ -39,6 +41,7 @@ import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent
import com.intellij.ide.ui.LafManagerListener
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.Function
fun showTutorial(project: Project) {
val tutorialFileName = getTutorialFileName()
@ -193,7 +196,41 @@ class ContinuePluginStartupActivity : StartupActivity, DumbAware {
}
})
// TODO add listeners for ModuleListener.TOPIC events
// Handle workspace directories changes
connection.subscribe(
ModuleListener.TOPIC,
object : ModuleListener {
override fun modulesAdded(project: Project, modules: MutableList<out Module>) {
val allModulePaths = ModuleManager.getInstance(project).modules
.flatMap { module -> ModuleRootManager.getInstance(module).contentRoots.mapNotNull { it.toUriOrNull() } }
val topLevelModulePaths = allModulePaths
.filter { modulePath -> allModulePaths.none { it != modulePath && modulePath.startsWith(it) } }
continuePluginService.workspacePaths = topLevelModulePaths.toTypedArray();
}
override fun moduleRemoved(project: Project, module: Module) {
val removedPaths = ModuleRootManager.getInstance(module).contentRoots.mapNotNull { it.toUriOrNull() } ;
continuePluginService.workspacePaths = continuePluginService.workspacePaths?.toList()?.filter { path -> removedPaths.none {removedPath -> path == removedPath }}?.toTypedArray();
}
override fun modulesRenamed(
project: Project,
modules: MutableList<out Module>,
oldNameProvider: Function<in Module, String>
) {
val allModulePaths = ModuleManager.getInstance(project).modules
.flatMap { module -> ModuleRootManager.getInstance(module).contentRoots.mapNotNull { it.toUriOrNull() } }
val topLevelModulePaths = allModulePaths
.filter { modulePath -> allModulePaths.none { it != modulePath && modulePath.startsWith(it) } }
continuePluginService.workspacePaths = topLevelModulePaths.toTypedArray()
}
}
)
connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener {
override fun fileClosed(source: FileEditorManager, file: VirtualFile) {