Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

diffFileTree.logic.ts

121 lines4.2 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { FileTreeBatchOperation, FileTreeSortComparator, GitStatus } from "@pierre/trees";
3​
4import { resolveFileDiffPath } from "~/lib/diffRendering";
5​
6/** One changed file as the tree shows it: its current path and how it changed. */
7export interface DiffFileTreeEntry {
8 readonly path: string;
9 readonly status: GitStatus;
11​
12function toGitStatus(file: FileDiffMetadata): GitStatus {
13 switch (file.type) {
14 case "new":
15 return "added";
16 case "deleted":
17 return "deleted";
18 case "rename-pure":
19 case "rename-changed":
20 return "renamed";
21 case "change":
22 return "modified";
23 }
25​
26/** Maps parsed diff files to tree entries, keeping the diff's own order. */
27export function diffFileTreeEntries(
28 files: ReadonlyArray<FileDiffMetadata>,
29): ReadonlyArray<DiffFileTreeEntry> {
30 return files.map((file) => ({ path: resolveFileDiffPath(file), status: toGitStatus(file) }));
32​
33/**
34 * Every directory on the way to each file, registered with the trailing slash Pierre uses for
35 * directory ids. Parents come before children so the tree can add them in order.
36 */
37export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArray<string> {
38 const directories = new Set<string>();
39 for (const path of paths) {
40 const segments = path.split("/");
41 let directory = "";
42 for (const segment of segments.slice(0, -1)) {
43 directory += `${segment}/`;
44 directories.add(directory);
45 }
46 }
47 return [...directories];
49​
50/** A folder takes the position of its first file in the diff. */
51export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
52 const positions = new Map<string, number>();
53 paths.forEach((path, index) => {
54 positions.set(path, index);
55 let directory = "";
56 for (const segment of path.split("/").slice(0, -1)) {
57 directory += `${segment}/`;
58 if (!positions.has(directory)) positions.set(directory, index);
59 }
60 });
61 return positions;
63​
64export function compareDiffFileTreeEntries(
65 getPositions: () => ReadonlyMap<string, number>,
66): FileTreeSortComparator {
67 return (left, right) => {
68 const positions = getPositions();
69 return (
70 (positions.get(left.path) ?? Number.MAX_SAFE_INTEGER) -
71 (positions.get(right.path) ?? Number.MAX_SAFE_INTEGER) ||
72 left.depth - right.depth ||
73 left.path.localeCompare(right.path)
74 );
75 };
77​
78function pathDepth(path: string): number {
79 return path.split("/").filter(Boolean).length;
81​
82/**
83 * The adds and removes that turn one set of file paths into another, so a diff that changes
84 * under the reader (a new slice, a refresh after an agent edit) keeps the directories they
85 * have already opened or closed instead of rebuilding the tree from scratch.
86 *
87 * Directories are removed only once no file needs them; a directory that gains its first file
88 * is added before that file.
89 */
90export function buildDiffFileTreeUpdates(
91 previousPaths: ReadonlyArray<string>,
92 nextPaths: ReadonlyArray<string>,
93): FileTreeBatchOperation[] {
94 const previousDirectories = new Set(collectDirectoryPaths(previousPaths));
95 const nextDirectories = new Set(collectDirectoryPaths(nextPaths));
96 const previous = new Set(previousPaths);
97 const next = new Set(nextPaths);
98 const updates: FileTreeBatchOperation[] = [];
99​
100 for (const path of previousPaths) {
101 if (!next.has(path)) updates.push({ type: "remove", path });
102 }
103 // Deepest first: a directory can only go once everything under it has.
104 const removedDirectories = [...previousDirectories]
105 .filter((directory) => !nextDirectories.has(directory))
106 .toSorted((left, right) => pathDepth(right) - pathDepth(left));
107 for (const directory of removedDirectories) {
108 updates.push({ type: "remove", path: directory, recursive: true });
109 }
110​
111 // Shallowest first: a file's directory has to exist before the file does.
112 const addedDirectories = [...nextDirectories]
113 .filter((directory) => !previousDirectories.has(directory))
114 .toSorted((left, right) => pathDepth(left) - pathDepth(right));
115 for (const directory of addedDirectories) updates.push({ type: "add", path: directory });
116 for (const path of nextPaths) {
117 if (!previous.has(path)) updates.push({ type: "add", path });
118 }
119​
120 return updates;