模块定位
sidex-text 是后端的共享文本类型基础——Position/Range 是 sidex-syntax(tree-sitter 增量解析)和 sidex-lsp(LSP 位置)的共同类型,Buffer 基于 ropey 提供高效文本操作,myers_diff 给编辑器 diff 视图。它被 4 处 use 引用(高扇入),是少数被多 crate 依赖的基础设施。注意:前端 Monaco 有自己的 piece-table 文本模型,这个 Rust crate 服务的是后端侧文本处理(行数统计、文件摘要、diff、行尾归一、词边界、hash/相等比较)和类型桥梁,而非编辑器本身。
模块架构
crates/sidex-text/src/(~6369 行,12 子模块)
├─ buffer.rs Buffer(ropey::Rope) + BufferSnapshot(Arc<Rope>) + 编辑/词分割/括号/缩进
├─ diff.rs myers_diff<T> 泛型 + compute_line_diff + DiffChange/LineDiff
├─ edit.rs EditOperation { range, text } 统一编辑模型
├─ text_model.rs TextModel(Buffer + 语言/URI/版本/编码/脏标记/大文件标记)
├─ position.rs / range.rs Position / Range(零基,自动归一化)
├─ line_ending.rs LineEnding 枚举 + 检测/归一化
├─ word_boundary.rs regex 词边界 + 语言特定定义
├─ utf16.rs Utf16Position + UTF-8↔UTF-16 列转换
├─ encoding.rs 19 种编码检测/解码
└─ search.rs 搜索引擎(regex/全词/大小写保留替换)
src-tauri/src/commands/text.rs count_lines/file_summary/simple_diff/normalize/word_boundaries/hash
调用链路
前端 invoke('simple_diff', { oldText, newText })
→ commands/text.rs:149 simple_diff → compute_line_diff(&old_lines, &new_lines)
→ diff.rs:189 compute_line_diff → myers_diff(&[&str]) → Vec<DiffChange>
→ diff.rs:49 myers_diff<T: PartialEq>
V 数组 k→x 映射(带 offset 处理负 k)
逐 D(编辑距离)递增搜索 → x>=n && y>=m
trace: Vec<Vec<usize>> 记录每步 V → 回溯重建编辑路径
merge_adjacent_changes 合并相邻变更块
→ 遍历 changes 生成 Vec<LineDiff>(removed/added 交集时 Modified(old,new))
→ serde JSON → 前端 diff 视图
count_lines(text.rs:20)走不同路径——不用 ropey,直接 memchr(SIMD 加速)在原始字节流上数 \n,32KB chunk 流式读,内存恒定。file_summary 需完整内容(行数+词数+字符数+编码+行尾)所以全量加载建 rope。
| 方法 | 一行职责 | 关键设计决策 |
|---|---|---|
count_lines in text.rs:20 |
流式数行 | 用 memchr SIMD 不建 rope(只需行数) |
file_summary in text.rs:40 |
文件统计 | 全量加载 + ropey 查询 + detect_encoding |
myers_diff in diff.rs:49 |
泛型 diff | T: PartialEq,可用于 char/行级 |
normalize_line_endings in line_ending.rs:131 |
行尾归一 | 字节级扫描 |
file_hash in text.rs:188 |
文件脏检测 | DefaultHasher(SipHash,非加密,快) |
files_equal in text.rs:200 |
文件相等 | 先比 size,再 32KB chunk 逐块比 |
apply_edits_with_undo in buffer.rs:1200 |
逆序批量编辑 | 从尾向头应用防 offset 失效,记 inverse_edit |
核心实现
ropey rope + Copy-on-Write 快照
Buffer(buffer.rs:171)核心 rope: Rope + eol: LineEnding。ropey 是 piece-tree + B-tree,插入/删除/替换 O(log n),Rope::from_reader 支持流式构建(buffer.rs:203),char_to_line/line_to_char/char_to_byte O(log n),line(idx) 返回 Cow<str> 零拷贝行访问。BufferSnapshot(buffer.rs:96)rope: Arc<Rope> 实现 O(1) clone——snapshot() 经 Arc::new(self.rope.clone())(ropey clone 本身是 O(1) Arc clone),交给后台线程做语法解析/搜索/diff,不阻塞编辑器主线程修改原 Buffer。
统一编辑模型 + 逆序应用
EditOperation { range, text } 一个结构体表达插入(empty range + text)、删除(range + empty)、替换(range + text),经 insert()/delete()/replace() 构造,与 LSP TextEdit 格式一致。apply_edits_with_undo(buffer.rs:1200)从文档尾部向头部应用编辑(sort_by_key(Reverse(start)))确保前面 offset 不被后面编辑失效,每个编辑记被替换的 old_text 构造 inverse_edit 供 undo,结果重排回原始顺序。
Myers diff 泛型
myers_diff<T: PartialEq>(diff.rs:49)是经典 Myers 算法:V 数组 k→x 映射带 offset 处理负 k,逐 D 递增搜索,trace: Vec<Vec<usize>> 记录每步 V 用于回溯,回溯区分 deletion(prev_x < x)/ insertion(prev_y < y),最后 merge_adjacent_changes。泛型使 &[char](字符级,compute_diff)和 &[&str](行级,compute_line_diff)共用同一实现。DiffChange 只记变更区域(不含 equal run),LineDiff 是面向用户的行级枚举(Equal/Added/Removed/Modified)。
UTF-16 互操作
LSP 协议用 UTF-16 code unit 偏移,Rust 字符串是 UTF-8。Utf16Position(utf16.rs:13)+ utf16_col_to_char_col/char_col_to_utf16_col 桥接两者,正确处理 BMP 外字符(如 emoji U+1F600 在 UTF-16 占 2 code unit)。TextModel.version(text_model.rs:48)用于 LSP 文档同步(didOpen/didChange 的 version)。大文件阈值 LARGE_FILE_THRESHOLD = 5_000_000(5MB)触发 is_large_file 标记。
词边界双系统
底层 segment_words(buffer.rs:1410)基于 classify_char(alphanumeric/whitespace/separator)O(n) 单次扫描,用于 words_at() 统计;高层 get_word_at_position(buffer.rs:1143)/word_boundary.rs:60 基于 regex 可配置词边界——不同语言用不同 regex(CSS [-\w@%]+、Shell [-.\w/]+、Markdown [\w]+),调用方按语言传入,镜像 VSCode WordOperations。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| 策略 | word_boundary.rs:35 语言特定 regex |
不同语言”什么算一个词”不同 |
| Copy-on-Write 快照 | buffer.rs:1387 snapshot() |
O(1) clone 交后台线程,不阻塞编辑器 |
| 统一编辑模型 | edit.rs EditOperation |
插/删/换一个结构,与 LSP 一致 |
| 泛型算法 | diff.rs:49 myers_diff<T: PartialEq> |
char 级与行级共用 |
模块间交互
sidex-text 被 4 个 crate 依赖:sidex-syntax(parser.rs:80 to_input_edit 把 EditOperation+Buffer 转 tree-sitter InputEdit;bracket.rs 用 Position 做 AST 括号匹配)、sidex-lsp(conversion.rs 在 Position/Range 与 lsp_types::Position/Range 间转换;多个 engine 模块用 Position)、src-tauri(commands/text.rs)、自身内部。sidex-textmate 不直接依赖 sidex-text。与 syntax 的桥梁 to_input_edit 需 Buffer 的 char_to_byte+position_to_offset;与 lsp 的桥梁是 UTF-16 列转换 + TextModel.version 文档同步。
扩展方式
新增一种行尾格式(如 Unicode LS/PS U+2028/U+2029):line_ending.rs 的 LineEnding 枚举加 variant → as_str()/line_ending_label()/detect_line_ending()/count_line_endings() 加检测(注意多字节 UTF-8 序列 \xe2\x80\xa8/\xe2\x80\xa9)→ buffer.rs::set_eol 自动适配。
改进 diff 算法:myers_diff 的 trace: Vec<Vec<usize>> 占 O((n+m)²) 空间——引入 enum DiffAlgorithm { Myers, Patience, Histogram } 或 trait DiffAlgorithm,compute_line_diff/compute_diff 接受 algorithm 参数,trace 改滚动数组优化空间。
新增文本统计指标(如平均行长、阅读时间):text.rs::FileSummary 加字段,file_summary 命令遍历 buffer.words_at() 时顺便统计(Buffer 已有 line_content_len/len_lines),阅读时间 = word_count / 200。
对应测试:
crates/sidex-text/src/diff.rs的#[cfg(test)]含 Myers 算法测试。