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

diffFileTree.logic.ts

148 lines5.3 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import type {
3 FileTreeBatchOperation,
4 FileTreeIconConfig,
5 FileTreeSortComparator,
6 GitStatus,
7} from "@pierre/trees";
8​
9import { resolveFileDiffPath } from "~/lib/diffRendering";
10import { basenameOfPath, resolvePierreIconForEntry, T3_PIERRE_ICONS } from "~/pierre-icons";
11​
12export interface DiffFileTreeEntry {
13 readonly path: string;
14 readonly status: GitStatus;
16​
17function toGitStatus(file: FileDiffMetadata): GitStatus {
18 switch (file.type) {
19 case "new":
20 return "added";
21 case "deleted":
22 return "deleted";
23 case "rename-pure":
24 case "rename-changed":
25 return "renamed";
26 case "change":
27 return "modified";
28 }
30​
31export function diffFileTreeEntries(
32 files: ReadonlyArray<FileDiffMetadata>,
33): ReadonlyArray<DiffFileTreeEntry> {
34 const statusByPath = new Map<string, GitStatus>();
35 for (const file of files) {
36 const path = resolveFileDiffPath(file);
37 const status = toGitStatus(file);
38 const previous = statusByPath.get(path);
39 statusByPath.set(path, previous === undefined || previous === status ? status : "modified");
40 }
41 return [...statusByPath].map(([path, status]) => ({ path, status }));
43​
44// Pierre cannot store a file and a directory at the same path. Give only those
45// files an invisible suffix, keeping real paths at the diff/viewer boundary.
46export function diffFileTreeModel(paths: ReadonlyArray<string>) {
47 const directories = new Set(collectDirectoryPaths(paths));
48 // Icon overrides match basenames case-insensitively, so aliases must also avoid
49 // real names in other directories, including names that already contain the suffix.
50 const occupiedNames = new Set(paths.flatMap((path) => path.toLowerCase().split("/")));
51 const selectionByModelPath = new Map<string, string>();
52 const modelBySelectionPath = new Map<string, string>();
53 const byFileName: FileTreeIconConfig["byFileName"] = { ...T3_PIERRE_ICONS.byFileName };
54 const modelPaths = paths.map((path) => {
55 if (!directories.has(`${path}/`)) return path;
56 let modelPath = path;
57 do {
58 modelPath += "\u200b";
59 } while (occupiedNames.has(basenameOfPath(modelPath).toLowerCase()));
60 occupiedNames.add(basenameOfPath(modelPath).toLowerCase());
61 selectionByModelPath.set(modelPath, path);
62 modelBySelectionPath.set(path, modelPath);
63 const icon = resolvePierreIconForEntry(path, "file");
64 if (icon !== null) byFileName[basenameOfPath(modelPath)] = icon;
65 return modelPath;
66 });
67 return {
68 paths: modelBySelectionPath.size === 0 ? paths : modelPaths,
69 selectionPath: (modelPath: string) => selectionByModelPath.get(modelPath) ?? modelPath,
70 modelPath: (path: string) => modelBySelectionPath.get(path) ?? path,
71 icons: modelBySelectionPath.size === 0 ? T3_PIERRE_ICONS : { ...T3_PIERRE_ICONS, byFileName },
72 };
74​
75export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArray<string> {
76 const directories = new Set<string>();
77 for (const path of paths) {
78 const segments = path.split("/");
79 let directory = "";
80 for (const segment of segments.slice(0, -1)) {
81 directory += `${segment}/`;
82 directories.add(directory);
83 }
84 }
85 return [...directories];
87​
88export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
89 const positions = new Map<string, number>();
90 paths.forEach((path, index) => {
91 positions.set(path, index);
92 let directory = "";
93 for (const segment of path.split("/").slice(0, -1)) {
94 directory += `${segment}/`;
95 if (!positions.has(directory)) positions.set(directory, index);
96 }
97 });
98 return positions;
100​
101export function compareDiffFileTreeEntries(
102 getPositions: () => ReadonlyMap<string, number>,
103): FileTreeSortComparator {
104 return (left, right) => {
105 const positions = getPositions();
106 return (
107 (positions.get(left.path) ?? Number.MAX_SAFE_INTEGER) -
108 (positions.get(right.path) ?? Number.MAX_SAFE_INTEGER) ||
109 left.depth - right.depth ||
110 left.path.localeCompare(right.path)
111 );
112 };
114​
115function pathDepth(path: string): number {
116 return path.split("/").filter(Boolean).length;
118​
119export function buildDiffFileTreeUpdates(
120 previousPaths: ReadonlyArray<string>,
121 nextPaths: ReadonlyArray<string>,
122): FileTreeBatchOperation[] {
123 const previousDirectories = new Set(collectDirectoryPaths(previousPaths));
124 const nextDirectories = new Set(collectDirectoryPaths(nextPaths));
125 const previous = new Set(previousPaths);
126 const next = new Set(nextPaths);
127 const updates: FileTreeBatchOperation[] = [];
128​
129 for (const path of previousPaths) {
130 if (!next.has(path)) updates.push({ type: "remove", path });
131 }
132 const removedDirectories = [...previousDirectories]
133 .filter((directory) => !nextDirectories.has(directory))
134 .toSorted((left, right) => pathDepth(right) - pathDepth(left));
135 for (const directory of removedDirectories) {
136 updates.push({ type: "remove", path: directory, recursive: true });
137 }
138​
139 const addedDirectories = [...nextDirectories]
140 .filter((directory) => !previousDirectories.has(directory))
141 .toSorted((left, right) => pathDepth(left) - pathDepth(right));
142 for (const directory of addedDirectories) updates.push({ type: "add", path: directory });
143 for (const path of nextPaths) {
144 if (!previous.has(path)) updates.push({ type: "add", path });
145 }
146​
147 return updates;