diff --git a/platform/configuration-store-impl/testSrc/ModuleStoreRenameTest.kt b/platform/configuration-store-impl/testSrc/ModuleStoreRenameTest.kt index cf0797c25888..14724b6c30da 100644 --- a/platform/configuration-store-impl/testSrc/ModuleStoreRenameTest.kt +++ b/platform/configuration-store-impl/testSrc/ModuleStoreRenameTest.kt @@ -9,8 +9,10 @@ import com.intellij.openapi.components.stateStore import com.intellij.openapi.module.Module import com.intellij.openapi.project.ModuleListener import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.impl.ModuleRootManagerComponent import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.project.modifyModules +import com.intellij.project.rootManager import com.intellij.testFramework.* import com.intellij.util.Function import com.intellij.util.SmartList @@ -67,7 +69,8 @@ internal class ModuleStoreRenameTest { } } }, - DisposeModulesRule(projectRule) + DisposeModulesRule(projectRule), + EdtRule() ) @Rule fun getChain() = ruleChain @@ -131,4 +134,24 @@ internal class ModuleStoreRenameTest { runInEdtAndWait { runWriteAction { parentVirtualDir.delete(this) } } } } + + @Test + @RunsInEdt + fun `rename module source root`() { + runInEdtAndWait { module.saveStore() } + val storage = module.storage + val parentVirtualDir = storage.virtualFile!!.parent + val src = VfsTestUtil.createDir(parentVirtualDir, "foo") + runWriteAction { + PsiTestUtil.addSourceContentToRoots(module, src, false) + } + module.saveStore() + + val rootManager = module.rootManager as ModuleRootManagerComponent + val stateModificationCount = rootManager.stateModificationCount + + runWriteAction { src.rename(null, UUID.randomUUID().toString()) } + + assertThat(stateModificationCount).isLessThan(rootManager.stateModificationCount) + } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/openapi/module/impl/moduleFileListener.kt b/platform/lang-impl/src/com/intellij/openapi/module/impl/moduleFileListener.kt index 3a65f970d09f..29179ea2eb6b 100644 --- a/platform/lang-impl/src/com/intellij/openapi/module/impl/moduleFileListener.kt +++ b/platform/lang-impl/src/com/intellij/openapi/module/impl/moduleFileListener.kt @@ -47,12 +47,15 @@ internal class ModuleFileListener(private val moduleManager: ModuleManagerCompon } private fun propertyChanged(event: VFilePropertyChangeEvent) { - if (event.requestor is StateStorage || VirtualFile.PROP_NAME != event.propertyName) { + if (!event.file.isDirectory || event.requestor is StateStorage || event.propertyName != VirtualFile.PROP_NAME) { return } + val roots = THashSet() + val parentPath = event.file.parent?.path ?: return var someModulePathIsChanged = false + val newAncestorPath = "${parentPath}/${event.newValue}" for (module in moduleManager.modules) { if (!module.isLoaded || module.isDisposed) { continue @@ -61,9 +64,11 @@ internal class ModuleFileListener(private val moduleManager: ModuleManagerCompon val ancestorPath = "$parentPath/${event.oldValue}" val moduleFilePath = module.moduleFilePath if (FileUtil.isAncestor(ancestorPath, moduleFilePath, true)) { - setModuleFilePath(module, "$parentPath/${event.newValue}/${FileUtil.getRelativePath(ancestorPath, moduleFilePath, '/')}") + setModuleFilePath(module, "$newAncestorPath/${FileUtil.getRelativePath(ancestorPath, moduleFilePath, '/')}") someModulePathIsChanged = true } + + checkRootModification(module, newAncestorPath, roots) } if (someModulePathIsChanged) { @@ -91,17 +96,20 @@ internal class ModuleFileListener(private val moduleManager: ModuleManagerCompon setModuleFilePath(module, "${event.newParent.path}/$dirName/${FileUtil.getRelativePath(ancestorPath, moduleFilePath, '/')}") } - // https://youtrack.jetbrains.com/issue/IDEA-168933 - roots.clear() + checkRootModification(module, newAncestorPath, roots) + } + } - val moduleRootManager = module.rootManager as? ModuleRootManagerImpl ?: continue - ProjectRootManagerComponent.addRootsToTrack(moduleRootManager.contentRootUrls, roots, roots) - ProjectRootManagerComponent.addRootsToTrack(moduleRootManager.sourceRootUrls, roots, roots) + // https://youtrack.jetbrains.com/issue/IDEA-168933 + private fun checkRootModification(module: Module, newAncestorPath: String, roots: THashSet) { + roots.clear() - if (roots.contains(newAncestorPath)) { - moduleRootManager.stateChanged() - break - } + val moduleRootManager = module.rootManager as? ModuleRootManagerImpl ?: return + ProjectRootManagerComponent.addRootsToTrack(moduleRootManager.contentRootUrls, roots, roots) + ProjectRootManagerComponent.addRootsToTrack(moduleRootManager.sourceRootUrls, roots, roots) + + if (roots.contains(newAncestorPath)) { + moduleRootManager.stateChanged() } }