Skip to content

fix(search): 搜索按编辑器的编码识别读文件,大文件和 CRLF 行尾也能搜到 - #116

Merged
xiaoyumuxi merged 1 commit into
mainfrom
fix/2198-search-misses-content
Nov 19, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
fix/2198-search-misses-content

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue / Linked issue

PR 类型 / Type

  • fix

改动说明 / What changes

背景 / Why

“在文件中查找”漏搜,Core 这边有三个原因,#2198 里用探针复现过:

  1. 搜索用严格 UTF-8 的 read_to_string 读文件。GBK、GB18030、UTF-16 文件读失败,被当成不存在;但编辑器能正常打开这些文件。
  2. 搜索只读 2 MB 以内的文件,编辑器能打开 16 MiB。2–16 MiB 的日志、生成代码搜不到。
  3. 按 \n 分行后,CRLF 行尾的 \r 留在行里,foo$ 这类正则在 Windows 换行的文件里匹配不到。

改动 / Changes

  • 按编辑器的编码识别读文件。 encoding.rs 新增 decode_detected_text,识别顺序和 file.read 自动识别相同:带 BOM 的 UTF-8/UTF-16、UTF-8、GBK/GB18030。UTF-8 文件不拷贝;UTF-32 LE BOM 仍然拒绝(不会被误当成 UTF-16 LE)。新增单元测试,逐个对比 6 种样本和 decode_auto 的结果。
  • 二进制判断提前。 matcher.rs 新增 read_searchable_text:不是 UTF-16 的文件只要含 NUL 字节就跳过,不进解码;解码后仍按原来的 is_plain_text 排除控制字符。
  • 大小上限改为 16 MiB。 files.rs 新增 MAX_SEARCH_FILE_SIZE = 16 MiB,等于桌面编辑器的打开上限,保证每条内容结果都能打开并跳到对应行。搜索索引的 trigram 也走 read_searchable_text,所以索引同样覆盖这些文件。
  • CRLF 行尾。 新增 line_content,匹配前去掉行尾 \r。workspace.search 和 workspace.searchTextBatch 的 $、全词、sourceLine、lineText、上下文行和范围,在 CRLF 和 LF 文件上结果一致。
  • 替换保持严格 UTF-8。 workspace.replacePreview 改用 read_replaceable_text,语义和原来一样:只读 2 MiB 以内的严格 UTF-8 文件。原因是宿主会把 replacementText 按 UTF-8 写回,GBK 或 UTF-16 文件一旦被替换就会被改写编码。替换同样按去掉 \r 的行匹配,再把 \r 拼回 replacementText,CRLF 文件替换后换行符不变。
  • 契约。 rust/contracts/rust-core-api.md 的 workspace.search 一节写明搜索读哪些文件(16 MiB、编码识别、二进制判断、CRLF)和替换预览的限制。

已知限制 / Known limitation

搜索能找到的 GBK、UTF-16 文件,以及 2–16 MiB 的 UTF-8 文件,不会出现在替换预览里。按原编码写回需要宿主配合,记在 #2199。

不在本 PR 范围内 / Out of scope

  • 桌面端“在文件中查找”把文件名结果挤掉内容结果的问题(不传 maxFileResults),在配套桌面 PR 里修,本 PR 合入后那边会更新 Core 固定版本。
  • 符号链接、Latin-1 等编辑器能用十六进制或其他方式打开的文件,以及 IDEA 级别的结果数、分组和排序,都在 #2199。

类型专属:fix

  • 根因。 搜索、索引和编辑器各自读文件,规则不一致:编码只认 UTF-8、大小上限 2 MB 对 16 MiB、行尾 \r 没去掉。现在搜索读文件复用编辑器的识别规则和上限。
  • 回归测试。 rust/crates/core/src/tests/project.rs:
    • search_reads_every_encoding_and_size_the_editor_opens:GBK、UTF-16 LE/BE、2.4 MB 日志都能用字面量和正则搜到,包括中文查询;超过 16 MiB 的文件和含 NUL 的二进制文件不会出现;替换预览只含 UTF-8 文件。
    • search_and_replacement_match_line_ends_on_crlf_lines:crlfToken417$ 能匹配,sourceLine 和范围不带 \r;替换预览把 \r\n 原样保留;searchTextBatch 的 lineText 和上下文行不带 \r。
    • 在原代码上两个测试都失败,修复后通过。

范围检查 / Scope checks

  • 功能矩阵与 UI 验证。 没有新增界面和功能项,结果展示不变;功能矩阵由配套桌面 PR 检查。
  • 插件归属。 不涉及插件,也没有按语言或工具名的特判。
  • 统一日志。 没有新增日志事件。读不了的文件和原来一样静默跳过,不报错。
  • 逻辑归属。 编码识别、二进制判断和分行规则都在 Core,桌面端和 Windows 共用。
  • 性能。 UTF-8 文件比原来多一次字节级 NUL 扫描([u8]::contains 用 memchr),解码不拷贝;二进制文件在解码前就跳过。索引现在为 2–16 MiB 的文本文件建 trigram,这类文件多的仓库,建索引和增量更新的耗时、内存会随之增加,上限由 16 MiB 控制。本地两个回归测试共耗时 0.63 s,大部分花在写 16 MiB 的测试文件上。
  • 契约。 只改 Core API 文档(rust/contracts),请求和响应字段不变,不涉及插件契约或能力版本,不需要写插件协议更新记录。
  • 界面文案、设置与发行资源。 没有新增文案和设置;不写任何文件,测试文件放在临时目录并会清理。
  • 无用代码与重构。 MAX_FILE_SIZE 保留给替换和 searchTextBatch 使用,并补了文档注释说明用途。

验证 / Verification

  • cargo fmt --all -- --check
  • scripts/rust-core/verify-rust-core-comments.sh project core
  • node scripts/rust-core/verify-platform-tests.mjs
  • cargo clippy -p lithe-project -p lithe-core --all-targets --all-features -- -D warnings
  • cargo clippy -p lithe-project -p lithe-core --all-targets --target x86_64-pc-windows-msvc -- -D warnings
  • 用 node scripts/rust-core/affected-crates.mjs 选出的 crate 跑全部测试:lithe-core 855 个,lithe-lsp-engine、lithe-project 等其余 crate 全部通过

Content search and its index now read files the way the desktop editor
opens them, so matches are no longer missed (1lck/Lithe-IDEA#2198):

- Decode with the editor's automatic detection (BOM-marked UTF-8 or
  UTF-16, UTF-8, then GBK or GB18030) instead of strict UTF-8.
- Read files up to 16 MiB, the editor's open limit, instead of 2 MiB.
- Skip binary files by a NUL byte outside UTF-16 before decoding.
- Drop a CRLF line's \r before matching, so `$`, whole words, sourceLine
  and ranges are the same on CRLF and LF files, in workspace.search and
  workspace.searchTextBatch.

Replacement previews still read only strict UTF-8 files up to 2 MiB,
because hosts write replacementText back as UTF-8; they now match CRLF
lines without \r too and keep it in replacementText.
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 19, 2026 10:42
@xiaoyumuxi
xiaoyumuxi merged commit ce628c7 into main Nov 19, 2026
6 of 7 checks passed
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.

1 participant