Repository navigation
fix(search): 搜索按编辑器的编码识别读文件,大文件和 CRLF 行尾也能搜到 - #116
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue / Linked issue
PR 类型 / Type
改动说明 / What changes
背景 / Why
“在文件中查找”漏搜,Core 这边有三个原因,#2198 里用探针复现过:
read_to_string读文件。GBK、GB18030、UTF-16 文件读失败,被当成不存在;但编辑器能正常打开这些文件。\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排除控制字符。files.rs新增MAX_SEARCH_FILE_SIZE = 16 MiB,等于桌面编辑器的打开上限,保证每条内容结果都能打开并跳到对应行。搜索索引的 trigram 也走read_searchable_text,所以索引同样覆盖这些文件。line_content,匹配前去掉行尾\r。workspace.search和workspace.searchTextBatch的$、全词、sourceLine、lineText、上下文行和范围,在 CRLF 和 LF 文件上结果一致。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 固定版本。类型专属:fix
\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
[u8]::contains用 memchr),解码不拷贝;二进制文件在解码前就跳过。索引现在为 2–16 MiB 的文本文件建 trigram,这类文件多的仓库,建索引和增量更新的耗时、内存会随之增加,上限由 16 MiB 控制。本地两个回归测试共耗时 0.63 s,大部分花在写 16 MiB 的测试文件上。rust/contracts),请求和响应字段不变,不涉及插件契约或能力版本,不需要写插件协议更新记录。MAX_FILE_SIZE保留给替换和searchTextBatch使用,并补了文档注释说明用途。验证 / Verification
cargo fmt --all -- --checkscripts/rust-core/verify-rust-core-comments.sh project corenode scripts/rust-core/verify-platform-tests.mjscargo clippy -p lithe-project -p lithe-core --all-targets --all-features -- -D warningscargo clippy -p lithe-project -p lithe-core --all-targets --target x86_64-pc-windows-msvc -- -D warningsnode scripts/rust-core/affected-crates.mjs选出的 crate 跑全部测试:lithe-core 855 个,lithe-lsp-engine、lithe-project 等其余 crate 全部通过