按当前 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
122 lines
6.1 KiB
TypeScript
122 lines
6.1 KiB
TypeScript
/**
|
|
* Unit tests for the explorer's VS Code-style exclude pattern compiler
|
|
* (src/exclude-patterns.ts): the one matcher both host surfaces (fs.trees
|
|
* listing and fs.search walk) share, plus the {@link ExcludeMatcher.key} the
|
|
* host's level cache keys filtered listings on.
|
|
*/
|
|
import { describe, expect, it } from 'vitest'
|
|
import { compileExcludePatterns, EXCLUDE_PATTERN_LIMIT, EXCLUDE_PATTERN_MAX_LENGTH } from '../src/exclude-patterns.ts'
|
|
|
|
const ROOT = '/work/project'
|
|
const test = (patterns: unknown[], path: string, name = path.split('/').pop()!) =>
|
|
compileExcludePatterns(patterns, ROOT, 'posix')?.(path, name) ?? false
|
|
|
|
describe('compileExcludePatterns', () => {
|
|
it('returns undefined for missing / empty / non-array input (no-pattern fast path)', () => {
|
|
expect(compileExcludePatterns(undefined, ROOT, 'posix')).toBeUndefined()
|
|
expect(compileExcludePatterns([], ROOT, 'posix')).toBeUndefined()
|
|
expect(compileExcludePatterns('node_modules', ROOT, 'posix')).toBeUndefined()
|
|
expect(compileExcludePatterns(['', ' ', '**'], ROOT, 'posix')).toBeUndefined()
|
|
})
|
|
|
|
it('matches plain names at any depth', () => {
|
|
const patterns = ['Thumbs.db', 'node_modules']
|
|
expect(test(patterns, `${ROOT}/Thumbs.db`)).toBe(true)
|
|
expect(test(patterns, `${ROOT}/deep/dir/Thumbs.db`)).toBe(true)
|
|
expect(test(patterns, `${ROOT}/node_modules`)).toBe(true)
|
|
expect(test(patterns, `${ROOT}/keep.txt`)).toBe(false)
|
|
// A name pattern is anchored: a substring or superstring does not match.
|
|
expect(test(patterns, `${ROOT}/Thumbs.db2`)).toBe(false)
|
|
expect(test(patterns, `${ROOT}/my-node_modules-x`)).toBe(false)
|
|
})
|
|
|
|
it('supports * and ? wildcards within one segment', () => {
|
|
expect(test(['*.log'], `${ROOT}/app.log`)).toBe(true)
|
|
expect(test(['*.log'], `${ROOT}/deep/app.log`)).toBe(true)
|
|
// `*` never crosses a segment boundary (the rel path stays unmatchable).
|
|
expect(test(['*.log'], `${ROOT}/log`)).toBe(false)
|
|
expect(test(['temp-?'], `${ROOT}/temp-1`)).toBe(true)
|
|
expect(test(['temp-?'], `${ROOT}/temp-12`)).toBe(false)
|
|
})
|
|
|
|
it('matches path patterns anchored at the session cwd', () => {
|
|
expect(test(['build/out'], `${ROOT}/build/out`)).toBe(true)
|
|
expect(test(['build/out'], `${ROOT}/src/build/out`)).toBe(false)
|
|
expect(test(['build/*'], `${ROOT}/build/main.o`)).toBe(true)
|
|
expect(test(['build/*'], `${ROOT}/build/sub/main.o`)).toBe(false)
|
|
expect(test(['build/**'], `${ROOT}/build/sub/main.o`)).toBe(true)
|
|
})
|
|
|
|
it('matches a doublestar-slash head at any depth (name and segment runs)', () => {
|
|
expect(test(['**/.git'], `${ROOT}/.git`)).toBe(true)
|
|
expect(test(['**/.git'], `${ROOT}/deep/.git`)).toBe(true)
|
|
expect(test(['**/cache/tmp'], `${ROOT}/cache/tmp`)).toBe(true)
|
|
expect(test(['**/cache/tmp'], `${ROOT}/deep/cache/tmp`)).toBe(true)
|
|
expect(test(['**/cache/tmp'], `${ROOT}/deep/cache/other`)).toBe(false)
|
|
})
|
|
|
|
it('normalizes decorations: backslashes, ./, leading and trailing slashes', () => {
|
|
expect(test(['node_modules\\'], `${ROOT}/node_modules`)).toBe(true)
|
|
expect(test(['./dist/'], `${ROOT}/dist`)).toBe(true)
|
|
expect(test(['/dist'], `${ROOT}/dist`)).toBe(true)
|
|
expect(test(['build\\out'], `${ROOT}/build/out`)).toBe(true)
|
|
})
|
|
|
|
it('escapes regex specials literally', () => {
|
|
expect(test(['a.b'], `${ROOT}/a.b`)).toBe(true)
|
|
expect(test(['a.b'], `${ROOT}/axb`)).toBe(false)
|
|
expect(test(['(x)'], `${ROOT}/(x)`)).toBe(true)
|
|
})
|
|
|
|
it('matches case-insensitively on win32 only', () => {
|
|
const win = compileExcludePatterns(['Thumbs.db'], 'C:\\work', 'win32')!
|
|
expect(win('C:\\work\\thumbs.db', 'thumbs.db')).toBe(true)
|
|
expect(test(['Thumbs.db'], `${ROOT}/thumbs.db`)).toBe(false)
|
|
})
|
|
|
|
it('drops malformed entries without breaking the rest', () => {
|
|
const patterns = ['', 3, null, '**', ' ', '*.tmp'] as unknown[]
|
|
const matcher = compileExcludePatterns(patterns, ROOT, 'posix')!
|
|
expect(matcher(`${ROOT}/scratch.tmp`, 'scratch.tmp')).toBe(true)
|
|
expect(matcher(`${ROOT}/keep.txt`, 'keep.txt')).toBe(false)
|
|
})
|
|
|
|
it('caps pattern count and length', () => {
|
|
const many = Array.from({ length: EXCLUDE_PATTERN_LIMIT + 5 }, (_, i) => `file-${i}`)
|
|
const matcher = compileExcludePatterns(many, ROOT, 'posix')!
|
|
expect(matcher(`${ROOT}/file-0`, 'file-0')).toBe(true)
|
|
expect(matcher(`${ROOT}/file-${EXCLUDE_PATTERN_LIMIT + 1}`, `file-${EXCLUDE_PATTERN_LIMIT + 1}`)).toBe(false)
|
|
const long = 'x'.repeat(EXCLUDE_PATTERN_MAX_LENGTH + 1)
|
|
expect(compileExcludePatterns([long], ROOT, 'posix')).toBeUndefined()
|
|
})
|
|
|
|
// The host's level cache keys listings on this identity, so a matcher that
|
|
// lied about its patterns would serve rows filtered by somebody else's list.
|
|
describe('the matcher key (the host cache dimension)', () => {
|
|
it('is equal for pattern sets that normalize alike', () => {
|
|
const a = compileExcludePatterns(['dist/', './dist'], ROOT, 'posix')!
|
|
const b = compileExcludePatterns(['/dist', 'dist'], ROOT, 'posix')!
|
|
expect(a.key).toBe(b.key)
|
|
// The key is derived from the normalized list, not deduplicated: a
|
|
// repeated pattern splits the key. That direction is safe — an
|
|
// over-split costs one cache miss, never a wrongly filtered listing.
|
|
expect(a.key).not.toBe(compileExcludePatterns(['dist'], ROOT, 'posix')!.key)
|
|
})
|
|
|
|
it('differs when the patterns differ, and when the anchor root differs', () => {
|
|
const base = compileExcludePatterns(['*.log'], ROOT, 'posix')!
|
|
expect(base.key).not.toBe(compileExcludePatterns(['*.tmp'], ROOT, 'posix')!.key)
|
|
// Path-anchored kinds resolve against the root, so two sessions with the
|
|
// same patterns but different cwds match one directory differently.
|
|
expect(base.key).not.toBe(compileExcludePatterns(['*.log'], '/other', 'posix')!.key)
|
|
// Case-folding is part of the compiled regex, hence part of the identity.
|
|
expect(compileExcludePatterns(['a'], '/x', 'posix')!.key)
|
|
.not.toBe(compileExcludePatterns(['a'], '/x', 'win32')!.key)
|
|
})
|
|
|
|
it('gives malformed-only input the undefined fast path (never an empty key)', () => {
|
|
expect(compileExcludePatterns(['', ' ', '**'], ROOT, 'posix')).toBeUndefined()
|
|
})
|
|
})
|
|
})
|