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 778ae4f759b0..03541cdbe2dd 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
@@ -858,7 +858,7 @@ public class DirectoryIndexTest extends IdeaTestCase {
private void checkInfo(VirtualFile file,
@Nullable Module module,
- boolean isInLibrary,
+ boolean isInLibraryClasses,
boolean isInLibrarySource,
@Nullable String packageName,
@Nullable final JpsModuleSourceRootType> moduleSourceRootType,
@@ -872,8 +872,9 @@ public class DirectoryIndexTest extends IdeaTestCase {
else {
assertFalse("isInModuleSource", info.isInModuleSource());
}
- assertEquals(isInLibrary, info.hasLibraryClassRoot());
+ assertEquals(isInLibraryClasses, info.hasLibraryClassRoot());
assertEquals(isInLibrarySource, info.isInLibrarySource());
+ assertEquals(isInLibraryClasses || isInLibrarySource, myFileIndex.isInLibrary(file));
if (file.isDirectory()) {
assertEquals(packageName, myFileIndex.getPackageNameByDirectory(file));
diff --git a/platform/projectModel-api/src/com/intellij/openapi/roots/ProjectFileIndex.java b/platform/projectModel-api/src/com/intellij/openapi/roots/ProjectFileIndex.java
index ee6c141050b0..22b6241b681c 100644
--- a/platform/projectModel-api/src/com/intellij/openapi/roots/ProjectFileIndex.java
+++ b/platform/projectModel-api/src/com/intellij/openapi/roots/ProjectFileIndex.java
@@ -140,6 +140,16 @@ public interface ProjectFileIndex extends FileIndex {
*/
boolean isInLibraryClasses(@NotNull VirtualFile fileOrDir);
+ /**
+ * Returns true if fileOrDir is a file which belongs to the classes or sources of some library.
+ *
+ * @param fileOrDir the file or directory to check.
+ * @return true if the file belongs to the classes or sources of some library, false otherwise.
+ */
+ default boolean isInLibrary(@NotNull VirtualFile fileOrDir) {
+ return isInLibraryClasses(fileOrDir) || isInLibrarySource(fileOrDir);
+ }
+
/**
* Returns true if fileOrDir is a file or directory from library source.
*
@@ -173,4 +183,4 @@ public interface ProjectFileIndex extends FileIndex {
* @return true if file is ignored, false otherwise.
*/
boolean isUnderIgnored(@NotNull VirtualFile file);
-}
\ No newline at end of file
+}
diff --git a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ProjectFileIndexImpl.java b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ProjectFileIndexImpl.java
index d669a2b870f9..a9bfea4bc768 100644
--- a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ProjectFileIndexImpl.java
+++ b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ProjectFileIndexImpl.java
@@ -191,6 +191,12 @@ public class ProjectFileIndexImpl extends FileIndexBase implements ProjectFileIn
return getInfoForFileOrDirectory(fileOrDir).isInLibrarySource();
}
+ // a slightly faster implementation then the default one
+ public boolean isInLibrary(@NotNull VirtualFile fileOrDir) {
+ DirectoryInfo info = getInfoForFileOrDirectory(fileOrDir);
+ return info.isInProject() && info.hasLibraryClassRoot() || info.isInLibrarySource();
+ }
+
@Override
public boolean isIgnored(@NotNull VirtualFile file) {
return isExcluded(file);