Skip to content

fix(editor): keep ShowContent from reporting edits back to the host (#2377) - #2399

Merged
xiaoyumuxi merged 2 commits into
mainfrom
fix/2377-diff-autosave
Nov 24, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
fix/2377-diff-autosave

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

关联 Issue / Linked issue

PR 类型 / Type

  • fix:缺陷修复

改动说明 / What changes

背景 / Why

在 Git 工作区打开未跟踪文本文件的 Diff 页签时,即使用户没有任何输入,文件也会在自动保存后每隔一轮在末尾追加一个换行符(LF 文件追加 LF,CRLF 文件追加 CRLF),不断污染工作区并可能触发写入占用错误。

原因是 editorview.View.ShowContent 内部复用了 replaceFromDisk,在宿主将文档内容同步给只读或从属视图展示时,仍计算差异并通过 OnEdit 上报。diffSideEditor 首次以不含末尾换行的 pane.text 初始化右侧视图后,无标签文档加载完成并调用 ShowContent(doc.view.CopyContent()),上报出的尾部换行差异被 diffEdited 再次 ApplyEdit 回无标签文档,触发自动保存 -> reloadDiffsOfPath -> ShowContent 死循环。

改动 / Changes

  • desktop/internal/editorview/view.go:将 replaceFromDisk 的主体抽出为 replaceContent(content Content, fromDisk bool);“保留撤销”和“上报 OnEdit”总是同时成立或同时不成立,所以只用一个参数。Reload 和 ResolveWithDisk 经 replaceFromDisk 传 fromDisk=true,行为不变。Config.OnEdit 的注释原来写“报告每一次替换”,现在写明 ShowContent 是例外。
  • desktop/internal/editorview/diffdecor.go:ShowContent 改为调用 v.replaceContent(content, false) 并更新注释说明——替换宿主展示内容时不保留撤销/重做历史,也不向 OnEdit 上报编辑(同时省去窗口线程上的 rope.Diff 和临时撤销快照)。
  • desktop/internal/workbench/source_preview.go:移除 previewEdited 中因 ShowContent 不再触发 OnEdit 而变成冗余的 p.diffs.mirroring++/-- 护栏(保留 mirrorPreviewEdit 中 ApplyEdit 路径的 mirroring 计数)。
  • desktop/internal/editorview/diffdecor_test.go:新增 TestShowContentDoesNotReportEditOrMirrorBack,验证 ShowContent 在多轮同步中不上报 OnEdit、不清空/改写来源文档且不产生撤销历史。
  • desktop/internal/workbench/difftab_test.go:新增 TestDiffTabUntrackedFileDoesNotEditOrAutosaveOnOpenAndReload,验证未跟踪 LF / CRLF 文件打开工作区 Diff、无标签文档加载、多轮 reloadDiffsOfPath 与关闭页签过程中文档保持干净、不调度自动保存且磁盘字节不变。
  • desktop/internal/workbench/pluginsemantics_test.go:测试助手 placeCompletionCaret 原来用 ShowContent 给已同步语言服务器的文档换上磁盘新内容。改动后 ShowContent 不再通知语言服务器,测试里的文档和语言服务器副本会不一致,所以改用 Reload(与“磁盘文件变化”的真实路径一致,也恢复了这个助手在本 PR 之前的同步行为)。
  • shared/platform-feature-matrix/features/git-diff-review.json:补充 #2377 验证说明与备注(保持 pending,待 Windows 实机验收)。
  • .agents/notes/implemented/bug-fix/2026-11-24-diff-show-content-edit-loop.md:记录 ShowContent 不上报 OnEdit 的行为约束与备选方案取舍。

不在本 PR 范围内 / Out of scope

  • 无;Windows 平台实机/虚拟机整机流程待后续在 Windows 环境验收。

类型专属:fix

复现与根因

  • 复现步骤、实际结果、期望结果:
    1. 在 Git 工作区放置带尾部 \n / \r\n 的未跟踪文件并打开工作区 Diff。
    2. 实际结果(修复前):TestShowContentDoesNotReportEditOrMirrorBack 报出 ShowContent reported [{StartLine:1 StartColumn:3 EndLine:1 EndColumn:3 Text:\n}];TestDiffTabUntrackedFileDoesNotEditOrAutosaveOnOpenAndReload 在首轮即失败,提示 untracked_lf.txt round 0: document became dirty with "alpha\nbeta\n\n"。
    3. 期望结果:ShowContent 不向 OnEdit 上报编辑,文档保持 clean、不调度自动保存、磁盘文件字节不变。
  • 根因(引入该行为的提交或 PR,用 git log -S 或 git blame 查):Viva 迁移提交 0faecba49(PR Epic(0.7.0): 基于 Viva 框架的跨平台方案的迁移(#1794) #1799)引入 ShowContent 时复用 replaceFromDisk 并仅在事后清空 undo/redo,未关闭 reportEdit;而 difftab.go 中的 diffSideEditor 未像后来 feat(desktop): 问题预览按严重程度着色并可直接编辑,选中色加深 #2110 的 source_preview.go 那样包裹 mirroring 屏蔽。
  • 影响平台:跨平台 Viva 桌面端(macOS 与 Windows 共用 editorview 和 workbench)。

检查项

  • 已先写回归测试,确认它在修复前失败、修复后通过;测试路径与修复前的失败输出:desktop/internal/editorview/diffdecor_test.go(TestShowContentDoesNotReportEditOrMirrorBack,修复前第 1 轮报出尾部 \n 编辑)与 desktop/internal/workbench/difftab_test.go(TestDiffTabUntrackedFileDoesNotEditOrAutosaveOnOpenAndReload,修复前首轮文档即变为 dirty "alpha\nbeta\n\n")。
  • [N/A] 无法自动化时,已写明手动复现与验证步骤:已覆盖自动化回归测试。
  • 已搜索被修改函数、类型、契约字段的全部调用方(含另一平台、插件、SDK、脚本和按字符串分发的调用):全仓库检索 ShowContent 全部 4 处生产调用点(difftab.go:629、source_preview.go:288、source_preview.go:302、plugin_workflow.go:239)与 replaceFromDisk 全部调用点(Reload、ResolveWithDisk、ShowContent)。其中 plugin_workflow.go:239 是唯一对主文档调用 ShowContent 的生产路径(刷新已打开的 lithe-plugin-preview:// 虚拟文档):它以前经 OnEdit 进入 documentEdited/mirrorDocumentEdit,但这些副作用对虚拟文档本来就全部跳过(自动保存、恢复草稿、最近文件、编辑器动作按 virtual 提前返回;languageID 为空,找不到语言服务器;没有 Diff 页签或问题预览显示这类路径)。Markdown/HTML 预览按 Revision() 刷新,replaceContent 仍递增版本号,所以预览照常更新。
  • 已检查另一平台是否有同类缺陷(有则本 PR 一并修复,或已提交 Issue):Viva 桌面端在 macOS 与 Windows 共用同一份 Go 代码,已一并修复。
  • 只改了修复所需的代码,没有夹带重构;修复过程中发现的其他问题已提交 Issue:无(source_preview.go 中因本次改动变成冗余的 ShowContent mirroring 护栏已按第九节规则在本次一并删除)。
  • 修复改变了行为约束或容易复发,已在 .agents/notes/implemented/bug-fix/ 记录:.agents/notes/implemented/bug-fix/2026-11-24-diff-show-content-edit-loop.md。

范围检查 / Scope checks

功能矩阵 / Feature matrix

  • 已更新 shared/platform-feature-matrix/features/<id>.json:shared/platform-feature-matrix/features/git-diff-review.json 补充了 #2377 验证依据与备注,verificationStatus 保持 pending。
  • 已运行 ./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh;改了平台实现路径的,还运行了 ./scripts/platform-feature-matrix/verify-platform-feature-matrix-change.sh origin/main HEAD。
  • 没有提交 .artifacts/ 下生成的矩阵视图。
  • [N/A] 没有更新矩阵的,理由:已更新 git-diff-review.json。

插件归属 / Plugin ownership

  • 已在仓库根目录运行 develop-lithe 第三节“插件归属与宿主零依赖”的自查命令,结果为空。
  • 新增或修改的测试只使用测试内联的合成插件、Core 契约 fixture 或插件市场索引快照;没有读取具体插件的插件包、仓库检出、构建产物或清单,没有使用真实插件 ID,没有“缺少插件时跳过”的测试。
  • 没有为具体插件提供的能力新建或更新宿主功能矩阵条目;宿主条目的证据和验证动作不引用插件的测试、插件包或插件仓库。
  • 插件文案没有写进 shared/i18n/;插件的构建步骤、资源路径和补丁没有写进 docs/ci-builds.md、Agent Note 或 Skill。
  • [N/A] 修改了通用扩展点、可能影响已发布插件的,已在受影响插件的仓库运行它的测试:未修改插件扩展点。

UI 验证 / UI evidence

N/A:本次修复不涉及布局、控件、样式、文案、菜单、弹窗或可见交互状态变更,仅修复 ShowContent 不再误报 OnEdit。

统一日志系统 / Structured logging

N/A:未新增或改动日志事件、写入器、界面警告/错误或丢弃错误的处理边界。

界面文案 / i18n

N/A:未新增或修改任何用户可见文案。

逻辑归属 / Logic placement

  • [N/A] 可共享的逻辑在 Rust Core:本次改动属于 Viva 视图层控件生命周期边界(editorview.View.ShowContent 是否触发视图 OnEdit 回调及视图层 mirroring 护栏),不涉及可共享业务规则或原生 I/O 规则变更。
  • [N/A] 修改平台层已有的可共享逻辑时已迁入 Core:未修改可共享业务逻辑。
  • 工具或运行时专属的知识放在插件或成熟上游,宿主和 Core 没有按工具名加分支。

性能与内存 / Performance and memory

  • 功能、子进程、语言服务器、WebView 首次需要才启动;没有新增空闲轮询;缓存和缓冲有上限;关闭项目、窗口、面板、标签时释放独占资源。
  • 主线程没有文件读写、目录遍历、进程启动;跨 C ABI、web view 只传增量或可见窗口;且 ShowContent 不再在窗口线程上调用 diskEdit(rope.Diff)和分配撤销快照。
  • 涉及编辑器模块(desktop/internal/editorview/**),已在同一台空闲 Apple M4 机器上以 --count 6 运行改动前后性能基线:
    • 命令:./scripts/desktop/perf/editor-bench.sh run --count 6 --out .artifacts/editor-perf/before.txt 与 --out .artifacts/editor-perf/after.txt --base .artifacts/editor-perf/before.txt,并用 compare 对比。
    • 结果:125 项基准中 122 项在预算内;改动后全部 editorview 基准均为 ✅ within(无任何新增超预算项);3 项既有超预算项(BenchmarkEditorSyntaxFullPass/go|java|markdown/100KiB)为改动前已存在的语法全量扫描耗时项。perfhistory compare 提示 1 项统计波动(BenchmarkEditorViewPaste/go/10MiB: 样本最大值 2093 allocs/op vs 原 2092,而均值由 2090 降至 2086,与 ShowContent 无关)。

设置 / Settings

N/A:未新增或修改任何设置项或设置页面。

插件契约 / Plugin contract

N/A:未修改 Rust Core 插件契约、能力版本、插件函数协议或宿主方法/通知。

发行包与资源 / Bundle and resources

N/A:未新增或修改任何运行时资源、缓存路径或打包步骤。

无用代码与重构 / Dead code and refactoring

  • 本次改动导致变成无用的代码已在本 PR 删除:移除 desktop/internal/workbench/source_preview.go 中包裹 view.ShowContent 的冗余 p.diffs.mirroring++/--。
  • [N/A] 本次之前就存在的无用代码或可重构之处,已提交带 refactor 标签的 Issue:本次未在改动范围内发现需建单的既有债务。

验证 / Verification

命令或操作 平台 结果
./scripts/desktop/desktop-go.sh test -count=1 ./internal/editorview/ macOS arm64 通过(0.746s)
./scripts/desktop/desktop-go.sh test -count=1 ./internal/workbench/ -run 'TestDiff|TestProblems|TestSourcePreview' macOS arm64 通过(11.842s,最终编辑后重跑)
./scripts/desktop/desktop-go.sh test -race ./internal/editorview/ macOS arm64 通过(8.586s)
./scripts/desktop/desktop-go.sh test -race ./internal/workbench/ -run 'TestDiff|TestDocument' macOS arm64 通过(15.707s)
./scripts/desktop/desktop-go.sh test ./... macOS arm64 internal/workbench(363.244s)等全部业务包通过;并发负载下 internal/localshell(TestClosingAnIntegratedShellRightAfterACommandHangsUpItsJobs/zsh)超时 1 次,单独重跑 ./scripts/desktop/desktop-go.sh test -count=1 ./internal/localshell/ 通过(8.474s)
./scripts/desktop/perf/editor-bench.sh run --count 6(改动前后对比) macOS arm64 (Apple M4) 全部 editorview 基准 ✅ within,无新增超预算项
./.agents/skills/write-stable-tests/scripts/verify-test-stability.sh & --base origin/main macOS arm64 退出码 0(Go 测试文件未纳入静态扫描范围)
./.agents/skills/write-stable-tests/scripts/test-stability-macos.sh(两条新增回归用例) macOS arm64 通过,结果写入 .artifacts/test-stability/index.html
./scripts/agent-notes/verify-agent-notes.sh macOS arm64 通过(208 篇活跃 Note 校验通过)
./scripts/platform-feature-matrix/verify-platform-feature-matrix.sh & verify-platform-feature-matrix-change.sh origin/main HEAD macOS arm64 通过(222 项能力校验通过)
插件零依赖自查 rg 命令 & gofmt -l macOS arm64 输出均为空
跟进提交:临时还原 view.go/diffdecor.go 到 origin/main 后运行两条新回归测试 macOS arm64 均失败(round 1: ShowContent reported …Text:\n;untracked_lf.txt round 0: document became dirty with "alpha\nbeta\n\n"),恢复修复后通过
跟进提交:./scripts/desktop/desktop-go.sh test -count=1 ./internal/editorview/ macOS arm64 通过
跟进提交:./scripts/desktop/desktop-go.sh test -count=3 ./internal/workbench/ -run 'TestLSPSignatureHelpStaysClearOfCaretWhenCompletionIsAbove|TestCompletionPopup|TestDiffTabUntrackedFileDoesNotEdit' macOS arm64 3 轮全部通过
跟进提交:-tags 'vivahead lithevisual' 运行 TestVisualCompletionPopup(使用 placeCompletionCaret) macOS arm64 通过
跟进提交:./scripts/desktop/desktop-go.sh test -count=1 ./internal/workbench/ -run 'TestDiff|TestSourcePreview|TestProblems|Preview|TestCompletion|TestLSP|TestDocument|Tabless|Semantic|TestPlugin|TestSignature' macOS arm64 通过(26.2s)
跟进提交:verify-agent-notes.sh、verify-platform-feature-matrix.sh、verify-platform-feature-matrix-change.sh origin/main HEAD macOS arm64 通过

未能执行的检查和原因:

  • 跟进提交只把 replaceContent 的两个总是相同的布尔参数合成一个,执行的分支和原提交完全相同,所以没有重跑编辑器性能基线;上面的前后对比仍然适用。
  • Windows 实机/本机虚拟机原生验证及 Windows CI 尚待执行(当前在本机 macOS 开发);功能矩阵中对应状态保持 pending。

提交前确认 / Final checks

  • 分支从最新的 main 创建;目标分支:main
  • 以 Draft 提交,本地验证通过后才标记 ready
  • 没有无关改动,没有提交 .build/、target/、dist/、.artifacts/ 等产物,没有凭据、个人路径和未脱敏的日志或截图
  • 本次构建、测试或预览启动的 Lithe 应用和子进程已全部关闭,没有进程残留

@ghfind-review ghfind-review Bot added the review: high ghfind author score; see https://ghfind.com label Nov 24, 2026
…the OnEdit exception (#2377)

- replaceContent took recordUndo and report, which always moved together;
  one fromDisk flag leaves no meaningless combination.
- Config.OnEdit said it reports every replacement; it now names the
  ShowContent exception.
- placeCompletionCaret replaced an LSP-synced document's text with
  ShowContent, which no longer tells the language server; it now uses
  Reload, as a changed file on disk does.
- The Agent Note records why the plugin preview path loses nothing, and
  the matrix text no longer names the internal signature.
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 24, 2026 14:52
@xiaoyumuxi
xiaoyumuxi merged commit ac40654 into main Nov 24, 2026
10 of 12 checks passed
@xiaoyumuxi xiaoyumuxi mentioned this pull request Nov 25, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: high ghfind author score; see https://ghfind.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 打开未跟踪文件的工作区 Diff 后,文件在没有任何编辑操作时被自动保存,尾部每轮多一个空行

1 participant