|
| 1 | +import type { DiffFileChange, DiffLineChange } from './parse-patch' |
| 2 | +import { diffWords } from 'diff' |
| 3 | + |
| 4 | +/** A contiguous, changed character range `[start, end)` within a line's content. */ |
| 5 | +export type WordRange = [number, number] |
| 6 | + |
| 7 | +export interface RenderLine extends DiffLineChange { |
| 8 | + /** Which reconstructed side holds this line's syntax tokens. */ |
| 9 | + tokenSide: 'old' | 'new' |
| 10 | + /** Index into that side's tokenized lines. */ |
| 11 | + tokenLine: number |
| 12 | + /** Changed word ranges within `content`, for intra-line emphasis. */ |
| 13 | + wordRanges: WordRange[] |
| 14 | +} |
| 15 | + |
| 16 | +export interface RenderHunk { |
| 17 | + header: string |
| 18 | + lines: RenderLine[] |
| 19 | +} |
| 20 | + |
| 21 | +export interface DiffFileModel { |
| 22 | + file: DiffFileChange |
| 23 | + /** Reconstructed old-side source (context + removed lines), for tokenizing. */ |
| 24 | + oldText: string |
| 25 | + /** Reconstructed new-side source (context + added lines), for tokenizing. */ |
| 26 | + newText: string |
| 27 | + hunks: RenderHunk[] |
| 28 | +} |
| 29 | + |
| 30 | +/** Changed char ranges on each side of a modified line pair, via word-level diff. */ |
| 31 | +function wordDiffRanges(oldStr: string, newStr: string): { old: WordRange[], new: WordRange[] } { |
| 32 | + const oldRanges: WordRange[] = [] |
| 33 | + const newRanges: WordRange[] = [] |
| 34 | + let oldOffset = 0 |
| 35 | + let newOffset = 0 |
| 36 | + for (const change of diffWords(oldStr, newStr)) { |
| 37 | + const len = change.value.length |
| 38 | + if (change.added) { |
| 39 | + newRanges.push([newOffset, newOffset + len]) |
| 40 | + newOffset += len |
| 41 | + } |
| 42 | + else if (change.removed) { |
| 43 | + oldRanges.push([oldOffset, oldOffset + len]) |
| 44 | + oldOffset += len |
| 45 | + } |
| 46 | + else { |
| 47 | + oldOffset += len |
| 48 | + newOffset += len |
| 49 | + } |
| 50 | + } |
| 51 | + return { old: oldRanges, new: newRanges } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Pair the removed and added lines of each contiguous change block within a |
| 56 | + * hunk (first removed with first added, and so on) and compute their word-level |
| 57 | + * ranges. Returns a map from the line's index in `lines` to its changed ranges. |
| 58 | + */ |
| 59 | +function computeWordRanges(lines: DiffLineChange[]): Map<number, WordRange[]> { |
| 60 | + const ranges = new Map<number, WordRange[]>() |
| 61 | + let i = 0 |
| 62 | + while (i < lines.length) { |
| 63 | + if (lines[i].type !== 'del') { |
| 64 | + i++ |
| 65 | + continue |
| 66 | + } |
| 67 | + const delStart = i |
| 68 | + while (i < lines.length && lines[i].type === 'del') i++ |
| 69 | + const addStart = i |
| 70 | + while (i < lines.length && lines[i].type === 'add') i++ |
| 71 | + const pairs = Math.min(addStart - delStart, i - addStart) |
| 72 | + for (let k = 0; k < pairs; k++) { |
| 73 | + const delLine = lines[delStart + k] |
| 74 | + const addLine = lines[addStart + k] |
| 75 | + const { old, new: next } = wordDiffRanges(delLine.content, addLine.content) |
| 76 | + if (old.length > 0) |
| 77 | + ranges.set(delStart + k, old) |
| 78 | + if (next.length > 0) |
| 79 | + ranges.set(addStart + k, next) |
| 80 | + } |
| 81 | + } |
| 82 | + return ranges |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Turn a parsed file diff into a render model: the reconstructed old/new side |
| 87 | + * source strings to feed the highlighter, plus per-line token coordinates and |
| 88 | + * intra-line word ranges. Context lines join both sides (so each side tokenizes |
| 89 | + * as coherent source), and are highlighted from the new side. |
| 90 | + */ |
| 91 | +export function buildFileModel(file: DiffFileChange): DiffFileModel { |
| 92 | + const oldLines: string[] = [] |
| 93 | + const newLines: string[] = [] |
| 94 | + |
| 95 | + const hunks: RenderHunk[] = file.hunks.map((hunk) => { |
| 96 | + const wordRanges = computeWordRanges(hunk.lines) |
| 97 | + const lines: RenderLine[] = hunk.lines.map((line, idx) => { |
| 98 | + let tokenSide: 'old' | 'new' |
| 99 | + let tokenLine: number |
| 100 | + if (line.type === 'del') { |
| 101 | + tokenSide = 'old' |
| 102 | + tokenLine = oldLines.length |
| 103 | + oldLines.push(line.content) |
| 104 | + } |
| 105 | + else if (line.type === 'add') { |
| 106 | + tokenSide = 'new' |
| 107 | + tokenLine = newLines.length |
| 108 | + newLines.push(line.content) |
| 109 | + } |
| 110 | + else { |
| 111 | + // Context lines belong to both reconstructed sides; highlight from the new one. |
| 112 | + oldLines.push(line.content) |
| 113 | + tokenSide = 'new' |
| 114 | + tokenLine = newLines.length |
| 115 | + newLines.push(line.content) |
| 116 | + } |
| 117 | + return { ...line, tokenSide, tokenLine, wordRanges: wordRanges.get(idx) ?? [] } |
| 118 | + }) |
| 119 | + return { header: hunk.header, lines } |
| 120 | + }) |
| 121 | + |
| 122 | + return { file, oldText: oldLines.join('\n'), newText: newLines.join('\n'), hunks } |
| 123 | +} |
0 commit comments