Skip to content

feat: detect text files by content instead of an extension allowlist - #25

Merged
1lck merged 6 commits into
1lck:preview/0.2.0from
xiaoyumuxi:xiaoyumuxi-feature-add
Aug 9, 2026
Merged

1lck merged 6 commits into
1lck:preview/0.2.0from
xiaoyumuxi:xiaoyumuxi-feature-add

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Collaborator

概述

将"文件是否可作为文本打开/搜索/记录历史"的判定从扩展名白名单改为内容检测。
以前只有白名单里的后缀(.swift/.md/.json 等)才会被当作文本,导致 Package.resolved、
Makefile、无后缀或未知后缀的纯文本文件被误判为二进制、无法打开或被搜索/本地历史忽略。
现在改为读取内容、按控制字符判断是否为纯文本,后缀不再参与决策。

同时为后续的二进制文件查看器预留一套注册表基础设施(见下方"预留,暂无消费者")。

改动分组

A. 文本判定:扩展名白名单 → 内容检测(本 PR 的主体)

  • WorkspaceTextFilePolicy:删除后缀白名单,新增 isPlainText(_:)(String / Data 两个重载),
    拒绝除 tab/换行等正常空白之外的控制字符(NUL、0x01–0x08、0x0E–0x1F、0x7F)。
  • Rust workspace.rs:新增 is_plain_text,与 Swift 侧逻辑逐位对齐;read_file 与 search
    路径改用内容检测,search 的大小上限检查保持在读取内容之前。
  • Rust history.rs:record 记录前做纯文本校验;并先 stat 判断大小再读取内容,避免把超限
    的大文件整个读进内存。
  • ProjectHistoryFeatureModel / LocalHistoryService:移除 isReadableTextFile 预过滤,
    统一交由核心层判定。

B. 二进制查看器注册表(预留基础设施,暂无消费者)

  • 新增 BinaryFileViewerRegistry:支持按 magic signature 或后缀注册查看器,magic 优先于后缀;
    默认拒绝(不注册任何查看器时,对二进制文件的行为与改动前一致)。
  • FileStorage / MacFileStorage 新增 readPrefix(from:byteCount:),用于读取文件头。
  • 依赖接线:AppServices → MacServiceContainer → AppModel → DocumentFeatureModel。
  • 文本打开失败时,会读取文件头 4KB 交给注册表尝试匹配查看器。

⚠️ 注意:目前生产代码中没有任何地方调用 .register(),注册表是空的(因为不知道后续是否打算支持对图片等内容的预览操作,因此仅仅预留但是不引入对应功能)。也就是说这套流程
现在是预留路径——每个二进制文件仍会匹配失败并落到 "cannot be displayed as text",可见行为
与改动前相同(只是多读 4KB 文件头)。基础设施先合入,真实查看器(如图片预览)将在后续 PR 接入。

C. 无关清理

  • CodeEditorView:移除 NSLayoutManagerDelegate conformance 上多余的 @preconcurrency。
    与本 PR 主题无关,顺手清理。

测试(均在本地执行,全部通过)

  • swift build:通过。
  • swift test:59 个测试 / 4 个套件全部通过,含更新后的 WorkspaceTextFilePolicy 与
    BinaryFileViewerRegistry 用例(magic 优先、后缀回退、默认拒绝)。
  • cargo test(lithe-core):34/34 通过,含新增的"未知后缀纯文本可被搜索/读取、二进制文件被
    拒绝"用例。

@xiaoyumuxi
xiaoyumuxi requested a review from 1lck as a code owner August 9, 2026 09:11
@xiaoyumuxi
xiaoyumuxi changed the base branch from main to preview/0.2.0 August 9, 2026 09:46
@1lck
1lck merged commit 9ba2b5e into 1lck:preview/0.2.0 Aug 9, 2026
2 checks passed
puppyben1 pushed a commit to puppyben1/Lithe-IDEA that referenced this pull request Oct 14, 2026
chore(plugins): 将 dev.lithe.plugin.php-support 1.0.0 加入官方插件索引

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
xiaoyumuxi added a commit that referenced this pull request Nov 13, 2026
…hared/fixtures 副本 (#1853) (#1861)

- 桌面测试改用 contractfixture.CoreFixture 读取所链接 Core 检出的 rust/fixtures,
  desktop-go.sh 导出 LITHE_CORE_FIXTURE_DIR
- 删除 shared/contracts(28 个文件)和 shared/fixtures(295 个文件),
  i18n 格式样例移到 shared/i18n/fixtures
- PINNED_CORE_REVISION 升到 Core main f113e9e(含 #23、#24、#25)
- .gitignore 只放行 shared/i18n 和 shared/platform-feature-matrix
- 资源复用清单删除 phpunit-fixture,功能矩阵删除无法本地校验的证据路径
- Note、Skill 和文档的引用改指 Core,新增 Note 记录决策
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants