@@ -39,6 +39,7 @@ import { createFileTreeDragMentionController } from "./fileTreeDragMention"; |
39 | 39 | import { |
40 | 40 | areAllDirectoriesExpanded, |
41 | 41 | coalesceReentrantCalls, |
| 42 | + onlyChildren, |
42 | 43 | readExpandedDirectories, |
43 | 44 | setAllDirectoriesExpanded, |
44 | 45 | writeExpandedDirectories, |
@@ -165,6 +166,9 @@ function buildTreeEntries( |
165 | 166 | return { entries, entryKinds, treePaths, directoryPaths, expandableDirectoryPaths }; |
166 | 167 | } |
167 | 168 |
|
| 169 | +// A symlink to its own parent lists itself as an only child without end. |
| 170 | +const MAX_FOLDERS_OPENED_WITH_PARENT = 16; |
| 171 | + |
168 | 172 | export default function FileBrowserPanel({ |
169 | 173 | environmentId, |
170 | 174 | cwd, |
@@ -390,29 +394,63 @@ export default function FileBrowserPanel({ |
390 | 394 | const expansionRoot = `${environmentId}:${cwd}`; |
391 | 395 | // Folders expanded when the panel last unmounted; each reopens once lazy loading lists it. |
392 | 396 | const [pendingExpandedPaths] = useState(() => new Set(readExpandedDirectories(expansionRoot))); |
| 397 | + // Folders just opened and not listed yet, with how many only-child folders may open below each. |
| 398 | + // Pierre merges such a chain into one row that takes its open state from the last folder. |
| 399 | + const openingPathsRef = useRef(new Map<string, number>()); |
393 | 400 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the Pierre tree model to load directories as they are expanded |
394 | 401 | useEffect(() => { |
395 | 402 | const currentPaths = new Set(directoryPaths); |
396 | 403 | for (const path of expandedPathsRef.current) { |
397 | 404 | if (!currentPaths.has(path)) expandedPathsRef.current.delete(path); |
398 | 405 | } |
| 406 | + for (const path of openingPathsRef.current.keys()) { |
| 407 | + if (!currentPaths.has(path)) openingPathsRef.current.delete(path); |
| 408 | + } |
| 409 | + let childrenByParent: ReadonlyMap<string, ProjectEntry | null> | null = null; |
| 410 | + const openOnlyChild = (path: string) => { |
| 411 | + const remaining = openingPathsRef.current.get(path); |
| 412 | + if (remaining === undefined) return; |
| 413 | + if (childrenByParent === null) childrenByParent = onlyChildren(directoryEntries); |
| 414 | + const child = childrenByParent.get(path.replace(/\/$/, "")); |
| 415 | + if (child === undefined) return; |
| 416 | + // Like expand all, leave an ignored folder such as `node_modules` to open by hand. |
| 417 | + const opensChild = |
| 418 | + child?.kind === "directory" && |
| 419 | + (!child.ignored || |
| 420 | + directoryEntries.some((entry) => entry.ignored && treePath(entry) === path)); |
| 421 | + if (!opensChild || remaining === 0) { |
| 422 | + openingPathsRef.current.delete(path); |
| 423 | + return; |
| 424 | + } |
| 425 | + const childItem = model.getItem(treePath(child)); |
| 426 | + // The listing reaches the tree after this effect, which notifies again. |
| 427 | + if (!childItem || !("expand" in childItem)) return; |
| 428 | + openingPathsRef.current.delete(path); |
| 429 | + if (childItem.isExpanded()) return; |
| 430 | + openingPathsRef.current.set(treePath(child), remaining - 1); |
| 431 | + childItem.expand(); |
| 432 | + }; |
399 | 433 | // Restoring calls item.expand(), which notifies subscribers synchronously. |
400 | 434 | const loadExpanded = coalesceReentrantCalls(() => { |
401 | 435 | if (model.isSearchOpen()) return; |
402 | 436 | let changed = false; |
403 | 437 | for (const path of directoryPaths) { |
404 | 438 | const item = model.getItem(path); |
405 | | - if (item?.isDirectory() && pendingExpandedPaths.delete(path) && "expand" in item) { |
406 | | - item.expand(); |
407 | | - } |
| 439 | + const restored = item?.isDirectory() && pendingExpandedPaths.delete(path); |
| 440 | + if (restored && item && "expand" in item) item.expand(); |
408 | 441 | if (item?.isDirectory() && "isExpanded" in item && item.isExpanded()) { |
409 | 442 | if (!expandedPathsRef.current.has(path)) { |
410 | 443 | expandedPathsRef.current.add(path); |
411 | 444 | changed = true; |
| 445 | + if (!restored && !openingPathsRef.current.has(path)) { |
| 446 | + openingPathsRef.current.set(path, MAX_FOLDERS_OPENED_WITH_PARENT); |
| 447 | + } |
412 | 448 | void load(path.replace(/\/$/, "")); |
413 | 449 | } |
| 450 | + openOnlyChild(path); |
414 | 451 | } else { |
415 | 452 | if (item?.isDirectory() && expandedPathsRef.current.has(path)) setExpandAll(false); |
| 453 | + openingPathsRef.current.delete(path); |
416 | 454 | if (expandedPathsRef.current.delete(path)) changed = true; |
417 | 455 | } |
418 | 456 | } |
@@ -425,7 +463,7 @@ export default function FileBrowserPanel({ |
425 | 463 | }); |
426 | 464 | loadExpanded(); |
427 | 465 | return model.subscribe(loadExpanded); |
428 | | - }, [directoryPaths, expansionRoot, load, model, pendingExpandedPaths]); |
| 466 | + }, [directoryEntries, directoryPaths, expansionRoot, load, model, pendingExpandedPaths]); |
429 | 467 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes ignored-file status into the imperative Pierre tree model |
430 | 468 | useEffect(() => { |
431 | 469 | model.setGitStatus( |
|