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:
1 parent
6b02285742
commit
d990c10e59
5 files changed
+195
-23
No files matched your search
@@ -451,6 +451,41 @@ export function untrackedFile(path: string, content: string): DiffFile {
|
||||
return { oldPath: '/dev/null', newPath: `b/${path}`, binary: false, hunks: [{ oldStart: 0, newStart: 1, header: '', lines }] }
|
||||
}
|
||||
|
||||
/**
|
||||
* Materialize a git gap fold's hidden rows from the two sides' full file
|
||||
* contents, by the fold's known line ranges: the old side drives context
|
||||
* rows (each mapped onto the new side through the fold's offset — a gap is
|
||||
* an unchanged run, so the sides align), and new-side lines the old range
|
||||
* never reaches become pure additions. Line numbers clip to the actual
|
||||
* content (a no-newline file's ranges can overrun by one); `\r` endings
|
||||
* survive verbatim, like git's own context lines.
|
||||
*/
|
||||
export function foldRowsFromContents(fold: FoldSegment, oldContent: string, newContent: string): DiffRow[] {
|
||||
const oldLines = oldContent.length === 0 ? [] : oldContent.split('\n')
|
||||
const newLines = newContent.length === 0 ? [] : newContent.split('\n')
|
||||
const offset = fold.newStart - fold.oldStart
|
||||
const rows: DiffRow[] = []
|
||||
const oldFrom = Math.max(fold.oldStart, 1)
|
||||
const oldTo = Math.min(fold.oldEnd, oldLines.length)
|
||||
for (let oldLine = oldFrom; oldLine <= oldTo; oldLine += 1) {
|
||||
const text = oldLines[oldLine - 1]!
|
||||
const newLine = oldLine + offset
|
||||
rows.push(
|
||||
newLine >= fold.newStart && newLine <= fold.newEnd && newLine <= newLines.length
|
||||
? { kind: 'context', oldLine, newLine, text }
|
||||
: { kind: 'context', oldLine, text },
|
||||
)
|
||||
}
|
||||
// New-side lines beyond what the old range reached (a pure-addition gap):
|
||||
// rows carrying only the new-side number, exactly like added lines.
|
||||
const newFrom = Math.max(fold.newStart, 1)
|
||||
const newTo = Math.min(fold.newEnd, newLines.length)
|
||||
for (let newLine = Math.max(newFrom, oldTo + offset + 1); newLine <= newTo; newLine += 1) {
|
||||
rows.push({ kind: 'add', newLine, text: newLines[newLine - 1]! })
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
/** Strip the `a/` / `b/` prefix git puts on diff paths (not on /dev/null). */
|
||||
export function displayPath(path: string): string {
|
||||
if (path === '/dev/null') return path
|
||||
|
||||
Reference in new issue
Block a user