public void testRelativePath() throws Exception { final File root = new File(PathManagerEx.getTestDataPath()); final File testRoot = new File(new File(root, "vfs"), "relativePath"); VirtualFile vTestRoot = LocalFileSystem.getInstance().findFileByIoFile(testRoot); assertNotNull(vTestRoot); assertTrue(vTestRoot.isDirectory()); final File subDir = new File(testRoot, "subDir"); final VirtualFile vSubDir = LocalFileSystem.getInstance().findFileByIoFile(subDir); assertNotNull(vSubDir); final File subSubDir = new File(subDir, "subSubDir"); final VirtualFile vSubSubDir = LocalFileSystem.getInstance().findFileByIoFile(subSubDir); assertNotNull(vSubSubDir); assertEquals("subDir", VfsUtilCore.getRelativePath(vSubDir, vTestRoot, '/')); assertEquals("subDir/subSubDir", VfsUtilCore.getRelativePath(vSubSubDir, vTestRoot, '/')); assertEquals("", VfsUtilCore.getRelativePath(vTestRoot, vTestRoot, '/')); }
private static void addSourceDirectoriesFromLibraries( @NotNull Project project, @NotNull VirtualFile directory, @NotNull Collection<VirtualFile> outSourceRoots) { ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project); VirtualFile classRoot = index.getClassRootForFile(directory); if (classRoot == null) return; String relativePath = VfsUtilCore.getRelativePath(directory, classRoot); if (relativePath == null) return; for (OrderEntry orderEntry : index.getOrderEntriesForFile(directory)) { for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) { VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath); if (sourceFile != null) { outSourceRoots.add(sourceFile); } } } }