export interface DiffLine { type: 'added' | 'removed' | 'unchanged'; value: string; } export function computeDiff(oldText: string, newText: string): DiffLine[] { // Normalize line endings to avoid ghost diffs from CRLF/LF mismatch const cleanOld = (oldText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n'); const cleanNew = (newText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n'); const oldLines = cleanOld.split('\n').map(l => l.trimEnd()); const newLines = cleanNew.split('\n').map(l => l.trimEnd()); const diff: DiffLine[] = []; let i = 0; let j = 0; while (i < oldLines.length || j < newLines.length) { if (i < oldLines.length && j < newLines.length && oldLines[i] === newLines[j]) { diff.push({ type: 'unchanged', value: oldLines[i] }); i++; j++; } else if (j < newLines.length && (i === oldLines.length || !oldLines.slice(i).includes(newLines[j]))) { diff.push({ type: 'added', value: newLines[j] }); j++; } else { diff.push({ type: 'removed', value: oldLines[i] }); i++; } } return diff; }