feat(diff): 折叠行异步展开状态机与内容切片
- src/client/api.ts: 仿 gitCommitDiff 新增 gitShow 封装,走既有 git.show 服务路由(payload {rev, path},返回 content 或 null),服务端零改动
- src/client/diff/rows.ts: 新增 foldRowsFromContents 纯函数——old 侧按 fold 行号切片为 context 行(new 侧经区间偏移映射,落界才带行号),new 侧补纯增行;越界截断、\r 保留
- src/client/diff/DiffRows.tsx: 新增 resolveFold prop;foldData/foldLoading/foldFailed 三态,加载中不可点,失败降级静态标记;foldEpoch 使 segments 重置后 in-flight 回调失效
- src/client/diff/DiffFiles.tsx: resolveFold 下传各文件 DiffRows,fold 级 Promise 缓存去重并发点击与重挂载
- tests/diff-rows.spec.ts: foldRowsFromContents 六用例(对称/纯删/纯增/\r\n/空内容/越界)
背景:git -U3 不输出 hunk 间未改动行,gap fold 只有行号范围没有 rows,渲染复用「点击展开」文案却无交互;展开能力建为独立状态机(含 api 封装),git lens 只需注入取数 loader。
设计 docs/plans/2026-09-07-git-fold-expand-design.md §4.1-4.4。
This commit is contained in:
+58
-3
@@ -2,11 +2,12 @@
|
||||
* The unified-diff adapter: git's own hunks (from parseUnifiedDiff) convert
|
||||
* into the shared DiffRow segments — rewrite pairing applies inside git
|
||||
* hunks exactly like session-op diffs, the unemitted context gaps between
|
||||
* hunks become non-expandable folds carrying their line ranges, and the
|
||||
* header stats (+n −m) count mods on both sides.
|
||||
* hunks become folds carrying their line ranges (expandable on demand via
|
||||
* foldRowsFromContents), and the header stats (+n −m) count mods on both
|
||||
* sides.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { parseUnifiedDiff, unifiedSegments, diffLines, pairMods, diffStats, untrackedFile } from '../src/client/diff/rows.ts'
|
||||
import { parseUnifiedDiff, unifiedSegments, diffLines, pairMods, diffStats, untrackedFile, foldRowsFromContents, type FoldSegment } from '../src/client/diff/rows.ts'
|
||||
|
||||
const twoHunks = [
|
||||
'diff --git a/a.ts b/a.ts',
|
||||
@@ -121,3 +122,57 @@ describe('pairMods', () => {
|
||||
expect(rows.map(r => r.kind)).toEqual(['mod', 'mod'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('foldRowsFromContents', () => {
|
||||
it('slices an aligned gap into context rows carrying both line numbers', () => {
|
||||
const fold: FoldSegment = { kind: 'fold', count: 4, oldStart: 4, oldEnd: 7, newStart: 6, newEnd: 9 }
|
||||
const rows = foldRowsFromContents(fold, 'a\nb\nc\nd\ne\nf\ng\nh\n', 'A\nB\nC\nD\nE\nF\nG\nH\nI\n')
|
||||
expect(rows).toEqual([
|
||||
{ kind: 'context', oldLine: 4, newLine: 6, text: 'd' },
|
||||
{ kind: 'context', oldLine: 5, newLine: 7, text: 'e' },
|
||||
{ kind: 'context', oldLine: 6, newLine: 8, text: 'f' },
|
||||
{ kind: 'context', oldLine: 7, newLine: 9, text: 'g' },
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps the old-side number alone when the new range cannot map (pure deletion gap)', () => {
|
||||
const fold: FoldSegment = { kind: 'fold', count: 2, oldStart: 3, oldEnd: 4, newStart: 3, newEnd: 2 }
|
||||
const rows = foldRowsFromContents(fold, 'a\nb\nc\nd\n', 'a\nb\n')
|
||||
expect(rows).toEqual([
|
||||
{ kind: 'context', oldLine: 3, text: 'c' },
|
||||
{ kind: 'context', oldLine: 4, text: 'd' },
|
||||
])
|
||||
})
|
||||
|
||||
it('emits pure additions for new lines the old range never reached (pure addition gap)', () => {
|
||||
const fold: FoldSegment = { kind: 'fold', count: 3, oldStart: 3, oldEnd: 2, newStart: 3, newEnd: 5 }
|
||||
const rows = foldRowsFromContents(fold, 'a\nb\n', 'a\nb\nc\nd\ne\n')
|
||||
expect(rows).toEqual([
|
||||
{ kind: 'add', newLine: 3, text: 'c' },
|
||||
{ kind: 'add', newLine: 4, text: 'd' },
|
||||
{ kind: 'add', newLine: 5, text: 'e' },
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps \\r endings verbatim like git context lines', () => {
|
||||
const fold: FoldSegment = { kind: 'fold', count: 2, oldStart: 1, oldEnd: 2, newStart: 1, newEnd: 2 }
|
||||
const rows = foldRowsFromContents(fold, 'a\r\nb\r\n', 'a\r\nb\r\n')
|
||||
expect(rows.map(r => r.text)).toEqual(['a\r', 'b\r'])
|
||||
})
|
||||
|
||||
it('returns no rows for empty contents', () => {
|
||||
const fold: FoldSegment = { kind: 'fold', count: 5, oldStart: 1, oldEnd: 5, newStart: 1, newEnd: 5 }
|
||||
expect(foldRowsFromContents(fold, '', '')).toEqual([])
|
||||
})
|
||||
|
||||
it('clips line numbers to the actual content (no-newline overrun)', () => {
|
||||
// A file without a trailing newline: the fold's ranges can overrun the
|
||||
// actual line count by one; the slice stops at the real last line.
|
||||
const fold: FoldSegment = { kind: 'fold', count: 3, oldStart: 2, oldEnd: 4, newStart: 2, newEnd: 4 }
|
||||
const rows = foldRowsFromContents(fold, 'a\nb\nc', 'a\nb\nc')
|
||||
expect(rows).toEqual([
|
||||
{ kind: 'context', oldLine: 2, newLine: 2, text: 'b' },
|
||||
{ kind: 'context', oldLine: 3, newLine: 3, text: 'c' },
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user