The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { FileTreeBatchOperation } from "@pierre/trees"; |
| 2 | |
| 3 | // A batched operation reworks its folder's siblings, while a rebuild reworks every path. On flat |
| 4 | // folders of 50,000 and 332,000 entries a rebuild won somewhere between 200 and 500 operations. |
| 5 | const REBUILD_COST_IN_FOLDER_OPERATIONS = 300; |
| 6 | |
| 7 | function parentOf(path: string): string { |
| 8 | const end = path.endsWith("/") ? path.length - 1 : path.length; |
| 9 | return path.slice(0, Math.max(0, path.lastIndexOf("/", end - 1))); |
| 10 | } |
| 11 | |
| 12 | function countByParent(paths: readonly string[]): Map<string, number> { |
| 13 | const counts = new Map<string, number>(); |
| 14 | for (const path of paths) { |
| 15 | const parent = parentOf(path); |
| 16 | counts.set(parent, (counts.get(parent) ?? 0) + 1); |
| 17 | } |
| 18 | return counts; |
| 19 | } |
| 20 | |
| 21 | export function shouldRebuildFileTree( |
| 22 | previousPaths: readonly string[], |
| 23 | nextPaths: readonly string[], |
| 24 | updates: readonly FileTreeBatchOperation[], |
| 25 | ): boolean { |
| 26 | // Up to this many operations, the estimate below can't exceed the rebuild, so skip counting. |
| 27 | if (updates.length <= REBUILD_COST_IN_FOLDER_OPERATIONS) return false; |
| 28 | const previousCounts = countByParent(previousPaths); |
| 29 | const nextCounts = countByParent(nextPaths); |
| 30 | let batchWork = 0; |
| 31 | for (const update of updates) { |
| 32 | if (update.type !== "add" && update.type !== "remove") continue; |
| 33 | const parent = parentOf(update.path); |
| 34 | batchWork += Math.max(previousCounts.get(parent) ?? 0, nextCounts.get(parent) ?? 0); |
| 35 | } |
| 36 | const rebuildWork = Math.max(previousPaths.length, nextPaths.length); |
| 37 | return batchWork > REBUILD_COST_IN_FOLDER_OPERATIONS * rebuildWork; |
| 38 | } |
| 39 | |
| 40 | function pathDepth(path: string): number { |
| 41 | return path.split("/").filter(Boolean).length; |
| 42 | } |
| 43 | |
| 44 | function isInsideRemovedDirectory(path: string, removedRoots: ReadonlySet<string>): boolean { |
| 45 | for (let slash = path.indexOf("/"); slash !== -1; slash = path.indexOf("/", slash + 1)) { |
| 46 | if (removedRoots.has(path.slice(0, slash + 1))) return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | export function buildFileTreePathUpdates( |
| 52 | previousPaths: readonly string[], |
| 53 | nextPaths: readonly string[], |
| 54 | ): FileTreeBatchOperation[] { |
| 55 | const previous = new Set(previousPaths); |
| 56 | const next = new Set(nextPaths); |
| 57 | const removedDirectoryRoots = new Set<string>(); |
| 58 | const updates: FileTreeBatchOperation[] = []; |
| 59 | |
| 60 | const removedPaths = previousPaths |
| 61 | .filter((path) => !next.has(path)) |
| 62 | .toSorted((left, right) => pathDepth(left) - pathDepth(right)); |
| 63 | for (const path of removedPaths) { |
| 64 | if (isInsideRemovedDirectory(path, removedDirectoryRoots)) continue; |
| 65 | const recursive = path.endsWith("/"); |
| 66 | updates.push({ type: "remove", path, ...(recursive ? { recursive: true } : {}) }); |
| 67 | if (recursive) removedDirectoryRoots.add(path); |
| 68 | } |
| 69 | |
| 70 | const addedPaths = nextPaths |
| 71 | .filter((path) => !previous.has(path)) |
| 72 | .toSorted((left, right) => pathDepth(left) - pathDepth(right)); |
| 73 | for (const path of addedPaths) updates.push({ type: "add", path }); |
| 74 | |
| 75 | return updates; |
| 76 | } |