(patch: StructuredPatchHunk, firstLine: string | null, filePath: string, fileContent: string | null, theme: string, width: number, dim: boolean, splitGutter: boolean)
| 48 | return maxLineNumber.toString().length + 3; // marker + 2 padding spaces |
| 49 | } |
| 50 | function renderColorDiff(patch: StructuredPatchHunk, firstLine: string | null, filePath: string, fileContent: string | null, theme: string, width: number, dim: boolean, splitGutter: boolean): CachedRender | null { |
| 51 | const ColorDiff = expectColorDiff(); |
| 52 | if (!ColorDiff) return null; |
| 53 | |
| 54 | // Defensive: if the gutter would eat the whole render width (narrow |
| 55 | // terminal), skip the split. Rust already wraps to `width` so the |
| 56 | // single-column output stays correct; we just lose noSelect. Without |
| 57 | // this, sliceAnsi(line, gutterWidth) would return empty content and |
| 58 | // RawAnsi(width<=0) is untested. |
| 59 | const rawGutterWidth = splitGutter ? computeGutterWidth(patch) : 0; |
| 60 | const gutterWidth = rawGutterWidth > 0 && rawGutterWidth < width ? rawGutterWidth : 0; |
| 61 | const key = `${theme}|${width}|${dim ? 1 : 0}|${gutterWidth}|${firstLine ?? ''}|${filePath}`; |
| 62 | let perHunk = RENDER_CACHE.get(patch); |
| 63 | const hit = perHunk?.get(key); |
| 64 | if (hit) return hit; |
| 65 | const lines = new ColorDiff(patch, firstLine, filePath, fileContent).render(theme, width, dim); |
| 66 | if (lines === null) return null; |
| 67 | |
| 68 | // Pre-split the gutter column once (cold-cache). sliceAnsi preserves |
| 69 | // styles across the cut; the Rust module already pads the gutter to |
| 70 | // gutterWidth so the narrow RawAnsi column's width matches its cells. |
| 71 | let gutters: string[] | null = null; |
| 72 | let contents: string[] | null = null; |
| 73 | if (gutterWidth > 0) { |
| 74 | gutters = lines.map(l => sliceAnsi(l, 0, gutterWidth)); |
| 75 | contents = lines.map(l => sliceAnsi(l, gutterWidth)); |
| 76 | } |
| 77 | const entry: CachedRender = { |
| 78 | lines, |
| 79 | gutterWidth, |
| 80 | gutters, |
| 81 | contents |
| 82 | }; |
| 83 | if (!perHunk) { |
| 84 | perHunk = new Map(); |
| 85 | RENDER_CACHE.set(patch, perHunk); |
| 86 | } |
| 87 | // Cap the inner map: width is part of the key, so terminal resize while a |
| 88 | // diff is visible accumulates a full render copy per distinct width. Four |
| 89 | // variants (two widths × dim on/off) covers the steady state; beyond that |
| 90 | // the user is actively resizing and old widths are stale. |
| 91 | if (perHunk.size >= 4) perHunk.clear(); |
| 92 | perHunk.set(key, entry); |
| 93 | return entry; |
| 94 | } |
| 95 | export const StructuredDiff = memo(function StructuredDiff(t0) { |
| 96 | const $ = _c(26); |
| 97 | const { |
no test coverage detected