feat(file-icons): FileIconDescriptor 支持 names / folderNames 与 open 参数

图标主题(VSCode 的 fileNames / folderNames 半边)需要按**文件名**和**目录名**
着色,扩展名规则表达不了 `package.json` / `Dockerfile` / `node_modules`。

- `names?: readonly string[]`:精确 basename(大小写不敏感),命中优先于扩展名。
- `folderNames?: readonly string[]`:精确目录 basename,命中优先于保留扩展名;
  只认领列出的目录,未列出的目录仍走 `'folder'`/`'folder-open'` 或内置 glyph。
- `exts` 变为可选:省略 = 没有扩展名规则(`names`-only 注册不再是 catch-all);
  `exts: []` 仍是全局默认 catch-all,语义不变。
- `icon(path, size, open?)`:目录行的展开态传入工厂,一条注册即可渲染开/合两态。
- `matchFileIcon` 先 names 后 exts;`matchFolderIcon(open, name?)` 先 folderNames
  后保留扩展名(新参数可选,旧调用不变)。
- 指南 §7 同步:字段、匹配顺序、四例注册(names / folderNames / 全目录 / catch-all)。

测试:`tests/file-icons.spec.tsx` 19 → 23(names 大小写、name 优先于 ext、
folderNames 只认领列出目录、open 传入工厂);`tests/consumer-types.ts` 补新字段。
This commit is contained in:
Menghuan1918
2026-09-08 21:24:03 +08:00
parent 8722bfb29c
commit a9a635138a
4 changed files with 186 additions and 63 deletions
+44 -18
View File
@@ -557,19 +557,22 @@ interface BetterSidebarService {
getFileViewers(): readonly FileViewerDescriptor[]
/** 当前已注册的文件图标描述符快照(v0.19.0+) */
getFileIcons(): readonly FileIconDescriptor[]
/** 按 path 匹配**具体扩展名**注册(priority 降序、注册序;不查 catch-all
* 与 folder 保留值)。消费方一般直接用 fileIcon/folderIcon 全链解析器。 */
/** 按 path 匹配**具体**注册(priority 降序、注册序):先 names 文件名,再
* 具体扩展名;不查 catch-all 与 folder 保留值。消费方一般直接用
* fileIcon/folderIcon 全链解析器。 */
matchFileIcon(path: string): FileIconDescriptor | undefined
/** 匹配目录行注册('folder'/'folder-open' 保留扩展名;priority 降序、注册序;
* 返回 undefined = 回退内置 VscFolder/VscFolderOpened) */
matchFolderIcon(open: boolean): FileIconDescriptor | undefined
/** 匹配目录行注册:先按 folderNames 匹配目录名(name 传 basename,可省),
* 再按 'folder'/'folder-open' 保留扩展名;priority 降序、注册序;
* 返回 undefined = 回退内置 VscFolder/VscFolderOpened */
matchFolderIcon(open: boolean, name?: string): FileIconDescriptor | undefined
/** 文件图标权威解析器(v0.19.0+),完整回退链:
* ① 具体扩展名注册 → ② 内置 glyph(md/媒体/pdf/json/代码/配置/数据库/lock/压缩包)
* ① 具体 names/扩展名注册 → ② 内置 glyph(md/媒体/pdf/json/代码/配置/数据库/lock/压缩包)
* → ③ 最优 catch-all 注册(exts: [],即全局默认)→ ④ 通用 VscFile。
* 任一注册工厂抛错都会被吞(console.error 后跳下一级),永远返回有效 ReactNode。 */
fileIcon(path: string, size: number): ReactNode
/** 目录图标解析器:注册的 'folder'(闭合)/'folder-open'(展开)图标 →
* 内置 VscFolder/VscFolderOpened;path 为目录自身路径(主题可按目录变化)。 */
/** 目录图标解析器:注册的 folderNames/'folder'(闭合)/'folder-open'(展开)图标 →
* 内置 VscFolder/VscFolderOpened;path 为目录自身路径(主题可按目录变化),
* open 会传给工厂,一条注册即可渲染开/合两态。 */
folderIcon(path: string, open: boolean, size: number): ReactNode
/** 按 id 查 tab 描述符 */
getTab(id: string): TabDescriptor | undefined
@@ -646,14 +649,23 @@ interface FileIconDescriptor {
* 而非文件扩展名:'folder'(闭合目录)、'folder-open'(展开目录)——
* 它们不会匹配真实文件(名为 x.folder 的文件不受影响)。
* [] = catch-all 全局默认:只兜内置 glyph 没认领的扩展名(注册的具体
* 扩展名与内置 glyph 永远优先于它)。 */
exts: readonly string[]
* names/扩展名与内置 glyph 永远优先于它)。
* **省略** = 完全没有扩展名规则(只有 names 的注册不是 catch-all)。 */
exts?: readonly string[]
/** 精确**文件名**(basename,大小写不敏感,如 ['package.json','Dockerfile'])——
* 图标主题的 fileNames 半边;命中优先于扩展名。省略/[] = 无文件名规则。 */
names?: readonly string[]
/** 精确**目录名**(basename,大小写不敏感,如 ['node_modules','src'])——
* 图标主题的 folderNames 半边;命中优先于保留扩展名,且**只认领列出的
* 目录**(要接管所有目录请用 'folder'/'folder-open')。省略/[] = 无规则。 */
folderNames?: readonly string[]
/** priority 高者胜,缺省 0(同级按注册先后) */
priority?: number
/** 尺寸感知的图标工厂(文件树/文件 tab 当前以 size=14 渲染)。
* 与内置图标(currentColor 单色,遵循皮肤契约)不同,注册图标可以是
* 任意 ReactNode——包括彩色图标;颜色在皮肤间的表现由注册方自行负责。 */
icon: (path: string, size: number) => ReactNode
* 任意 ReactNode——包括彩色图标;颜色在皮肤间的表现由注册方自行负责。
* open:目录行的展开态(文件行为 undefined),一条注册即可渲染开/合两态。 */
icon: (path: string, size: number, open?: boolean) => ReactNode
}
```
@@ -670,9 +682,23 @@ if (ctx.betterSidebar.features.includes('fileIcons')) {
)
ctx.effect(() =>
ctx.betterSidebar.registerFileIcon({
id: 'my-plugin:folders',
exts: ['folder'], // 闭合目录行;['folder-open'] 认领展开行——
icon: (path, size) => <MyFolderIcon size={size} />, // 开/合图标不同就注册两条
id: 'my-plugin:names', // 精确文件名:package.json 与别的 .json 区分开
names: ['package.json', 'Dockerfile'],
icon: (path, size) => <MyBrandIcon size={size} />,
})
)
ctx.effect(() =>
ctx.betterSidebar.registerFileIcon({
id: 'my-plugin:folders', // 只认领列出的目录名
folderNames: ['node_modules', 'src'],
icon: (path, size, open) => open === true ? <MyOpenFolderIcon size={size} /> : <MyFolderIcon size={size} />,
})
)
ctx.effect(() =>
ctx.betterSidebar.registerFileIcon({
id: 'my-plugin:all-folders', // 接管所有目录行(保留扩展名)
exts: ['folder', 'folder-open'],
icon: (path, size, open) => open === true ? <MyOpenFolderIcon size={size} /> : <MyFolderIcon size={size} />,
})
)
ctx.effect(() =>
@@ -688,10 +714,10 @@ if (ctx.betterSidebar.features.includes('fileIcons')) {
**消费表面与回退链**(由本插件内置消费,插件无需自己接线):
- 文件树文件行 / 编辑器文件 tab(每个文件独立窗口):`fileIcon(path, size)`
——具体扩展名注册 → 内置 glyph(markdown/媒体/pdf/json/代码/配置/数据库/lock/压缩包)
——具体 `names`/扩展名注册 → 内置 glyph(markdown/媒体/pdf/json/代码/配置/数据库/lock/压缩包)
→ catch-all 全局默认 → 通用 `VscFile`。
- 文件树目录行(含根行):`folderIcon(path, open, size)`——`'folder'`/`'folder-open'`
注册 → 内置 `VscFolder`/`VscFolderOpened`。
- 文件树目录行(含根行):`folderIcon(path, open, size)`——`folderNames` 命中
→ `'folder'`/`'folder-open'` 保留扩展名 → 内置 `VscFolder`/`VscFolderOpened`。
注册/注销即时生效(文件树与 tab 栏订阅注册表变化自动重渲染);图标工厂抛错会被吞掉
(console.error 后跳到回退链下一级),不会空白行。