diff --git a/platform/platform-tests/testSrc/com/intellij/workspaceModel/core/fileIndex/AutoExcludeWorkspaceFileIndexContributorTest.kt b/platform/platform-tests/testSrc/com/intellij/workspaceModel/core/fileIndex/AutoExcludeWorkspaceFileIndexContributorTest.kt index 7d5c5139cf0d..b1865ac57c6f 100644 --- a/platform/platform-tests/testSrc/com/intellij/workspaceModel/core/fileIndex/AutoExcludeWorkspaceFileIndexContributorTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/workspaceModel/core/fileIndex/AutoExcludeWorkspaceFileIndexContributorTest.kt @@ -3,10 +3,14 @@ package com.intellij.workspaceModel.core.fileIndex import com.intellij.openapi.application.readAction import com.intellij.openapi.vfs.VirtualFile +import com.intellij.platform.backend.workspace.workspaceModel +import com.intellij.platform.workspace.jps.entities.ModuleEntity +import com.intellij.platform.workspace.jps.entities.modifyModuleEntity import com.intellij.testFramework.PsiTestUtil import com.intellij.testFramework.junit5.RegistryKey import com.intellij.testFramework.junit5.TestApplication import com.intellij.testFramework.rules.ProjectModelExtension +import com.intellij.workspaceModel.ide.legacyBridge.impl.java.JAVA_MODULE_ENTITY_TYPE_ID import com.intellij.workspaceModel.ide.registerProjectRoot import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions.assertFalse @@ -34,6 +38,11 @@ class AutoExcludeWorkspaceFileIndexContributorTest { @Test fun `default excluded directories are not in content after project root entity is added`() = runBlocking { + projectModel.project.workspaceModel.update("convert module to java") { + it.modifyModuleEntity(it.entities(ModuleEntity::class.java).first()) { + type = JAVA_MODULE_ENTITY_TYPE_ID + } + } val worktreesDir = projectModel.baseProjectDir.newVirtualDirectory("projectRoot/.worktrees") val claudeWorktreesDir = projectModel.baseProjectDir.newVirtualDirectory("projectRoot/.claude/worktrees") val srcDir = projectModel.baseProjectDir.newVirtualDirectory("projectRoot/src") @@ -48,6 +57,24 @@ class AutoExcludeWorkspaceFileIndexContributorTest { assertTrue(readAction { fileIndex.isInContent(srcDir) }) } + @Test + fun `default excluded directories are not in content after non-java module is added`() = runBlocking { + val moduleRoot = projectModel.baseProjectDir.newVirtualDirectory("moduleRoot") + val worktreesDir = projectModel.baseProjectDir.newVirtualDirectory("moduleRoot/.worktrees") + val claudeWorktreesDir = projectModel.baseProjectDir.newVirtualDirectory("moduleRoot/.claude/worktrees") + val srcDir = projectModel.baseProjectDir.newVirtualDirectory("moduleRoot/src") + + assertFalse(readAction { fileIndex.isInContent(worktreesDir) }) + assertFalse(readAction { fileIndex.isInContent(srcDir) }) + + val module = projectModel.createModule("nonJavaModule") + PsiTestUtil.addSourceContentToRoots(module, moduleRoot) + + assertFalse(readAction { fileIndex.isInContent(worktreesDir) }) + assertFalse(readAction { fileIndex.isInContent(claudeWorktreesDir) }) + assertTrue(readAction { fileIndex.isInContent(srcDir) }) + } + @Test @RegistryKey(key = "ide.workspace.model.relative.paths.to.exclude.automatically", value = "custom-dir") fun `custom registry value specifies which directories to exclude`() = runBlocking { diff --git a/platform/projectModel-impl/resources/META-INF/WorkspaceModelImpl.xml b/platform/projectModel-impl/resources/META-INF/WorkspaceModelImpl.xml index c69d7aa1fb6d..8915328b85b9 100644 --- a/platform/projectModel-impl/resources/META-INF/WorkspaceModelImpl.xml +++ b/platform/projectModel-impl/resources/META-INF/WorkspaceModelImpl.xml @@ -15,6 +15,7 @@ + { + override val entityClass: Class + get() = ContentRootEntity::class.java + + override val dependenciesOnOtherEntities: List> + get() = listOf(DependencyDescription.OnParent(ModuleEntity::class.java) { it.contentRoots.asSequence() }) + + override fun registerFileSets(entity: ContentRootEntity, registrar: WorkspaceFileSetRegistrar, storage: EntityStorage) { + val directories = Registry.get("ide.workspace.model.relative.paths.to.exclude.automatically").asString() + .split(";").map { it.trim() }.filter { it.isNotEmpty() && it != "." } + + if (entity.module.type?.name != "JAVA_MODULE") { // There can be too many java modules in a project. Also, java projects should be covered by ProjectRootEntity + for (dir in directories) { + registrar.registerExcludedRoot(entity.url.append(dir), entity) + } + } + } +} \ No newline at end of file