FileTreeUtil { @Nullable public static List<FilePath> getTreeTo(@NotNull FilePath path, @Nullable FilePath parent) { if (parent == null) { return null; } List<FilePath> ret = new ArrayList<>(); FilePath prev; FilePath next = path; do { ret.add(next); prev = next; next = next.getParentPath(); } while (next != null && !next.equals(prev) && !parent.equals(prev)); if (parent.equals(prev) || parent.equals(next)) { return ret; } return null; }  @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 getTreeTo_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" ); FilePath five = vfs.get("one/two/three/four/five.txt").asFilePath(); FilePath parent = five.getParentPath().getParentPath().getParentPath(); List<FilePath> tree = FileTreeUtil.getTreeTo(five, parent); assertSize(4, tree); assertEquals(five, tree.get(0)); assertEquals(five.getParentPath(), tree.get(1)); assertEquals(five.getParentPath().getParentPath(), tree.get(2)); assertEquals(parent, tree.get(3)); }
@Test void getTreeTo_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" ); VirtualFile five = vfs.get("one/two/three/four/five.txt"); VirtualFile parent = five.getParent().getParent().getParent(); List<VirtualFile> tree = FileTreeUtil.getTreeTo(five, parent); assertSize(4, tree); assertEquals(five, tree.get(0)); assertEquals(five.getParent(), tree.get(1)); assertEquals(five.getParent().getParent(), tree.get(2)); assertEquals(parent, tree.get(3)); }