FileTreeUtil { public static int getPathDepth(@NotNull FilePath path, @Nullable FilePath parent) { if (parent == null) { return -1; } if (path.equals(parent)) { return 0; } List<FilePath> tree = getTreeTo(path, parent); if (tree == null) { return -1; } return tree.size() - 1; }  @NotNull static List<FilePath> getTree(@NotNull FilePath path); @Nullable static List<FilePath> getTreeTo(@NotNull FilePath path, @Nullable FilePath parent); @Nullable static List<VirtualFile> getTreeTo(@NotNull VirtualFile path, @Nullable VirtualFile parent); static int getPathDepth(@NotNull FilePath path, @Nullable FilePath parent); static int getPathDepth(@NotNull VirtualFile path, @Nullable VirtualFile parent); static int getPathDepth(@NotNull FilePath path, @Nullable VirtualFile parent); static boolean isSameOrUnder(@Nullable FilePath parent, @NotNull FilePath child); static boolean isSameOrUnder(@Nullable VirtualFile parent, @NotNull FilePath child); @Nullable static FilePath getCommonRoot(@NotNull Collection<FilePath> files);  }
@Test void getPathDepth_FilePath_FilePath() { Map<String, MockVirtualFile> vfs = MockVirtualFileSystem.createTree( "one/two/three/four/five.txt", "contents", "root.txt", "contents", "one/root.txt", "contents", "two/root.txt", "contents", "other/file.txt", "contents" ); FilePath five = vfs.get("one/two/three/four/five.txt").asFilePath(); FilePath other = vfs.get("other/file.txt").asFilePath(); assertEquals( 0, FileTreeUtil.getPathDepth(five, five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, (FilePath) null) ); assertEquals( -1, FileTreeUtil.getPathDepth(five.getParentPath(), five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, other) ); assertEquals( 1, FileTreeUtil.getPathDepth(five, five.getParentPath()) ); assertEquals( 2, FileTreeUtil.getPathDepth(five, five.getParentPath().getParentPath()) ); assertEquals( 3, FileTreeUtil.getPathDepth(five, five.getParentPath().getParentPath().getParentPath()) ); assertEquals( 4, FileTreeUtil.getPathDepth(five, five.getParentPath().getParentPath().getParentPath().getParentPath()) ); assertEquals( 5, FileTreeUtil.getPathDepth( five, five.getParentPath().getParentPath().getParentPath().getParentPath().getParentPath()) ); }
@Test void getPathDepth_VirtualFile_VirtualFile() { Map<String, MockVirtualFile> vfs = MockVirtualFileSystem.createTree( "one/two/three/four/five.txt", "contents", "root.txt", "contents", "one/root.txt", "contents", "two/root.txt", "contents", "other/file.txt", "contents" ); VirtualFile five = vfs.get("one/two/three/four/five.txt"); VirtualFile other = vfs.get("other/file.txt"); assertEquals( 0, FileTreeUtil.getPathDepth(five, five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, null) ); assertEquals( -1, FileTreeUtil.getPathDepth(five.getParent(), five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, other) ); assertEquals( 1, FileTreeUtil.getPathDepth(five, five.getParent()) ); assertEquals( 2, FileTreeUtil.getPathDepth(five, five.getParent().getParent()) ); assertEquals( 3, FileTreeUtil.getPathDepth(five, five.getParent().getParent().getParent()) ); assertEquals( 4, FileTreeUtil.getPathDepth(five, five.getParent().getParent().getParent().getParent()) ); assertEquals( 5, FileTreeUtil.getPathDepth( five, five.getParent().getParent().getParent().getParent().getParent()) ); }
@Test void getPathDepth_FilePath_VirtualFile() { Map<String, MockVirtualFile> vfs = MockVirtualFileSystem.createTree( "one/two/three/four/five.txt", "contents", "one/two/three/four/five/six.txt", "contents", "root.txt", "contents", "one/root.txt", "contents", "two/root.txt", "contents", "other/file.txt", "contents" ); FilePath five = vfs.get("one/two/three/four/five.txt").asFilePath(); VirtualFile other = vfs.get("other/file.txt"); VirtualFile p4 = vfs.get("one/two/three/four/five.txt").getParent(); VirtualFile p3 = p4.getParent(); VirtualFile p2 = p3.getParent(); VirtualFile p1 = p2.getParent(); VirtualFile p0 = p1.getParent(); assertEquals( 0, FileTreeUtil.getPathDepth(five, five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, (VirtualFile) null) ); assertEquals( -1, FileTreeUtil.getPathDepth(vfs.get("one/two/three/four/five/six.txt").asFilePath(), five) ); assertEquals( -1, FileTreeUtil.getPathDepth(five, other) ); assertEquals( 1, FileTreeUtil.getPathDepth(five, p4) ); assertEquals( 2, FileTreeUtil.getPathDepth(five, p3) ); assertEquals( 3, FileTreeUtil.getPathDepth(five, p2) ); assertEquals( 4, FileTreeUtil.getPathDepth(five, p1) ); assertEquals( 5, FileTreeUtil.getPathDepth(five, p0) ); }