diff --git a/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTest.java b/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTest.java index a129c711f70a..bd16d4c7139b 100644 --- a/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTest.java +++ b/java/java-tests/testSrc/com/intellij/openapi/roots/impl/DirectoryIndexTest.java @@ -214,6 +214,8 @@ public class DirectoryIndexTest extends IdeaTestCase { checkInfoNull(myExcludedLibClsDir); checkInfoNull(myExcludedLibSrcDir); + assertEquals(myModule1Dir, checkInfoNotNull(myLibClsDir).getContentRoot()); + checkInfo(myModule3Dir, myModule3, false, false, null, null); } @@ -495,6 +497,11 @@ public class DirectoryIndexTest extends IdeaTestCase { checkInfo(myModule1Dir, myModule, true, false, "", null, myModule); checkInfo(mySrcDir1, myModule, true, false, "", JavaSourceRootType.SOURCE, myModule); + + checkInfo(myLibDir, myModule, true, false, "lib", null, myModule); + checkInfo(myLibClsDir, myModule, true, false, "", null, myModule2); + checkInfo(myLibSrcDir, myModule, true, true, "", null, myModule); + checkInfo(myResDir, myModule, true, false, "", JavaResourceRootType.RESOURCE, myModule); } @@ -509,7 +516,8 @@ public class DirectoryIndexTest extends IdeaTestCase { @Nullable Module module, boolean isInLibrary, boolean isInLibrarySource, - @Nullable String packageName, final JpsModuleSourceRootType moduleSourceRootType, + @Nullable String packageName, + @Nullable final JpsModuleSourceRootType moduleSourceRootType, Module... modulesOfOrderEntries) { DirectoryInfo info = checkInfoNotNull(dir); assertEquals(module, info.getModule()); diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java index 285c3a4f211c..e145ad638352 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/impl/RootIndex.java @@ -180,9 +180,12 @@ class RootIndex { for (Map.Entry entry : myRoots.entrySet()) { DirectoryInfo info = entry.getValue(); Module module = info.getModule(); + VirtualFile contentRoot = info.getContentRoot(); VirtualFile libraryClassRoot = info.getLibraryClassRoot(); boolean inModuleSource = info.isInModuleSource(); boolean inLibrarySource = info.isInLibrarySource(); + + OrderEntry[] orderEntries = info.getOrderEntries(); boolean nested = false; @@ -191,12 +194,20 @@ class RootIndex { DirectoryInfo eachInfo = myRoots.get(eachFile); if (eachInfo != null) { nested = true; + + if (eachInfo.getLibraryClassRoot() != null && libraryClassRoot == null && !info.isInModuleSource()) { + orderEntries = eachInfo.getOrderEntries(); + } + if (module == null) { module = eachInfo.getModule(); } if (libraryClassRoot == null) { libraryClassRoot = eachInfo.getLibraryClassRoot(); } + if (contentRoot == null) { + contentRoot = eachInfo.getContentRoot(); + } inModuleSource |= eachInfo.isInModuleSource(); inLibrarySource |= eachInfo.isInLibrarySource(); } @@ -208,7 +219,8 @@ class RootIndex { } if (nested) { int sourceRootTypeData = DirectoryInfo.createSourceRootTypeData(inModuleSource, inLibrarySource, info.getSourceRootTypeId()); - entry.setValue(info.with(module, info.getContentRoot(), info.getSourceRoot(), libraryClassRoot, sourceRootTypeData, info.getOrderEntries())); + if (orderEntries.length == 0) orderEntries = null; + entry.setValue(info.with(module, contentRoot, info.getSourceRoot(), libraryClassRoot, sourceRootTypeData, orderEntries)); } } } @@ -293,7 +305,15 @@ class RootIndex { myPackagePrefixRoots.get(packagePrefix).add(root); } + @Nullable public DirectoryInfo getInfoForDirectory(@NotNull final VirtualFile dir) { + if (!dir.isValid()) { + return null; + } + if (!dir.isDirectory()) { + return myRoots.get(dir); + } + int count = 0; for (VirtualFile root = dir; root != null; root = root.getParent()) { if (++count > 1000) {