IDEA-CR-18986 renaming tested and fixed

This commit is contained in:
Vladimir Krivosheev
2017-03-06 16:35:42 +01:00
parent af68a92e3f
commit 0ecc4bf49a
2 changed files with 43 additions and 12 deletions
@@ -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)
}
}
@@ -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<String>()
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<String>) {
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()
}
}