fix(editor): blame memo 在变更集消失时也失效(#212)

effect 原先在「文件已无变更集」(提交 / 丢弃后 entry === undefined)的
早退分支里直接 setKinds(EMPTY_KINDS) 返回,forgetBlame 只在有变更集的
分支才跑。于是提交前 hover 过的行继续返回提交前的 blame——包括
「Not Committed Yet」这类未提交占位——直到该文件再次产生变更集,或
memo 触顶 500 被整体清空。

把 forgetBlame 提到早退之前:变更集被重算与被清空都是失效条件。不改
过期时间,也不加缓存层。

测试:
- 新增(判别性)「提交前 hover → 变更集消失 → 再取同一行」用例,断言第
  二次重新请求且不再返回提交前答案;还原源码改动后该用例红在
  `expected "vi.fn()" to be called 2 times, but got 1 times`。
- 补一条原无守护的用例:挂载 code 视图后 api.gitBlame 调用数为 0(不预
  取),同时断言变更集确实读到了(gitDiffHead 1 次 + 色条 gutter 在
  DOM 里),避免断言因「gutter 根本没装」而空过。
- 既有的「同一行 hover 两次只打一次 git」保持为 memo 生效的守护。
This commit is contained in:
Menghuan1918 committed 2026-10-05 22:36:10 +08:00
1 parent bce22cfaf1
commit ce8366fb11
2 files changed
+84 -8

No files matched your search

+12 -6
View File
@@ -19,8 +19,10 @@
* file tree already uses for its rows.
* - Blame is fetched ON HOVER for the hovered line only, memoized per
* session + path + line. Scrolling never re-requests a range, a file is
* never prefetched, and the memo is dropped when the file's change set is
* recomputed (a new diff means new commits may be involved).
* never prefetched, and the memo is dropped whenever the file's change set
* is recomputed OR cleared — a new diff means new commits may be involved,
* and a vanished diff (the file was committed or discarded) must not leave
* the pre-commit line answering the next hover.
*/
import { useEffect, useMemo, useState } from 'react'
import { RangeSet, Compartment, StateEffect, StateField, type EditorState, type Extension } from '@codemirror/state'
@@ -247,7 +249,7 @@ const blameCache = new Map<string, Promise<GitBlameLine | null>>()
const blameKey = (sessionId: string, path: string, line: number): string =>
`${sessionId}\u0000${path}\u0000${String(line)}`
/** Drop the memo of one file (its change set was recomputed). */
/** Drop the memo of one file (its change set was recomputed or cleared). */
export function forgetBlame(sessionId: string, path: string): void {
const prefix = `${sessionId}\u0000${path}\u0000`
for (const key of [...blameCache.keys()]) {
@@ -321,14 +323,18 @@ export function useEditorGitGutter(options: EditorGitGutterOptions): ReadonlyMap
const [kinds, setKinds] = useState<ReadonlyMap<number, GitLineKind>>(EMPTY_KINDS)
useEffect(() => {
// A fresh change set invalidates the file's blame memo (the lines that
// moved may now belong to other commits) — and so does a change set that
// DISAPPEARS, which is the branch below: once the file is committed or
// discarded there is no diff to recompute, so an invalidation kept inside
// the enabled branch would never run and the next hover would be answered
// with the pre-commit line (the '[Not Committed Yet]' placeholder too).
forgetBlame(scope.sessionId, path)
if (!enabled || entry === undefined) {
setKinds(EMPTY_KINDS)
return undefined
}
let cancelled = false
// A fresh change set invalidates the file's blame memo (the lines that
// moved may now belong to other commits).
forgetBlame(scope.sessionId, path)
if (entry.tone === 'untracked') {
setKinds(addedLineKinds(content === undefined || content === '' ? 0 : content.split('\n').length))
return undefined