Files
apes-sidebar/src/client/file-tree-compact.ts
T
Menghuan1918andshyuan-hub 5254c031b1 feat(explorer): 文件树面包屑折叠 + VS Code 风格可配置排除模式(移植 #264)
按当前 dev 重写贡献者 PR #264 的两项核心意图:

1. 面包屑折叠(VSCode "compact folders"):host 给「内容恰为一个有效
   子项」的目录行标记 `compact`,client 把单目录链折叠成一行 `a/b/c`
   并整链展开/折叠。
2. `explorerExclude`(VS Code `files.exclude` 风格的 glob 子集):匹配
   条目从文件树与文件名搜索中彻底移除;树与搜索共用同一份 host 侧编译
   好的 matcher,两个界面永不失配。

为什么按 dev 现有结构重写而不是机械解冲突——证据卡逐条点名的 dev 侧变化:

- **客户端树已换成 DirRow 记忆化组件 + loadLevels/api.fsTrees 批量路由**。
  PR 改的是旧的 `expanded.includes(...)` 内联目录行与单路径 `api.fsTree`,
  该上下文在 dev 已不存在。折叠因此接在 renderLevel/visibleRows 的链上,
  并新增「每级缓存修订一次」的链缓存:DirRow 按 prop 身份 memo,每次渲染
  新建数组会让选择/悬停把每个目录行都重渲。
- **`api.fsTree(scope, path, signal?)` 的第三位已经是 AbortSignal**,PR 的
  `api.fsTree(scope, dir, exclude)` 会把数组当 signal。exclude 统一排在
  signal 之前(`fsTree`/`fsTrees`/`fsSearch` 三处一致)。
- **dev 的 `listDirectory` 有 1.5s TTL 缓存,PR 是直接读盘**。照搬会让不同
  排除集共用一份缓存(改完排除列表仍读到旧行),而 pref 变更没有可搭的
  失效钩子。故把模式集的身份并入 cacheKey(新增 `ExcludeMatcher.key`,含
  锚点 root 与大小写折叠语义):同列表命中缓存,改列表即换键。
- **文件树的真实入口是批量路由 `fs.trees`**(挂载/展开一次 POST),PR 只
  改了 `fs.tree`/`fs.search` 两处——不接批量路由等于 exclude 不生效。
- PR body 声称移除 `explorerShowHidden`,但净 diff 对它零命中、dev 与 PR
  合并基也都没有该键,属失效声明,不做产品取舍。

复用的部分(与 dev 无耦合):`src/exclude-patterns.ts` 与
`src/client/file-tree-compact.ts` 两个纯逻辑模块逐字取用(前者仅新增缓存
身份 `ExcludeMatcher.key`)。设置 UI 沿用 PR 的声明式 `patterns` 行类型
(chips + 删除钮 + 常驻添加输入框,原子提交整表),5 个新词条按仓库规则
同步全部 21 份词典;`src/client/service.ts` 的能力清单新增 `settingPatterns`
并同步 docs/external-plugin-guide.md。

门禁:pnpm typecheck 0;pnpm exec vitest run --maxWorkers=3 全绿
(165 files / 1847 passed / 13 skipped);npx eslint 45 个改动文件 0;
新增与改动的 spec 单跑 154 passed;pnpm build 0。

Refs #264

Co-authored-by: shyuan-hub <311810858+shyuan-hub@users.noreply.github.com>
2026-10-05 02:13:53 +08:00

78 lines
3.4 KiB
TypeScript

/**
* Pure logic behind the explorer's breadcrumb folder compaction (VSCode's
* "compact folders"): a directory whose contents are exactly one child
* (host flag `compact`) folds together with its sole directory children
* into one `a/b/c` row — the chain extends only through DIRECTORY links,
* the terminal link's sole child may be a file. The host only ever marks
* non-symlink children, so a fold chain can never cycle through the
* filesystem — the visited guard is belt-and-braces against a stale or
* hostile cache. The chain walk skips POSIX-hidden entries (the host probe
* marks `compact` with the same filter), so junk like macOS's `.DS_Store`
* beside the sole child never snaps a chain open — regardless of whether
* the user's "show hidden files" switch renders them.
*/
import type { FsEntry } from './api.ts'
/** One cached level as FileTree stores it (entries arrive async; error set on failure). */
export interface CompactLevel {
entries?: FsEntry[]
error?: string
}
/**
* Safety cap on one fold chain. Real directories cannot cycle, so this is
* never reached on a healthy cache — it bounds the chain when a stale cache
* lies about a level's contents.
*/
export const MAX_COMPACT_DEPTH = 32
/**
* The fold chain starting at `entry`: successive sole directory children
* while each link's level is loaded, still holds exactly one healthy
* directory child, and the chain stays within the depth cap. The returned
* chain always starts with `entry` itself; its LAST element is the level
* the expanded row renders.
*/
export function compactChain(entry: FsEntry, levelOf: (path: string) => CompactLevel | undefined): FsEntry[] {
const chain = [entry]
const visited = new Set<string>([entry.path])
let current = entry
while (current.compact === true && chain.length < MAX_COMPACT_DEPTH) {
const level = levelOf(current.path)
// Unloaded / failed / stale level: the chain pauses at the current link
// (the caller keeps loading while the tail stays `compact`).
if (level === undefined || level.error !== undefined || level.entries === undefined) break
// Hidden (dot-prefixed) entries don't count as children — the host
// probe marked `compact` with the same filter, so hidden junk beside
// the sole child must not snap the chain open here.
const visible = level.entries.filter(entry => entry.hidden !== true)
if (visible.length !== 1) break
const child = visible[0]!
if (!child.isDir || child.broken || visited.has(child.path)) break
visited.add(child.path)
chain.push(child)
current = child
}
return chain
}
/**
* The collapsed singleton directories whose levels still need loading, so
* fold chains can extend past what the user has expanded. Scans every
* LOADED level once; each arrival may reveal the next link, so the caller
* re-runs this after every cache update. Chains cannot cycle (host never
* marks symlink children), so the walk terminates at the chain's real end.
*/
export function compactLoadTargets(levels: Record<string, CompactLevel>): string[] {
const targets = new Set<string>()
for (const level of Object.values(levels)) {
if (level.entries === undefined) continue
for (const entry of level.entries) {
if (entry.isDir && entry.compact === true && levels[entry.path] === undefined) {
targets.add(entry.path)
}
}
}
return [...targets]
}