/// Parses a server-recorded unified patch into hunks, lines and the true /// `+N -M` stat. Ported from `web/src/changes.js` so the Flutter viewer numbers /// and groups lines exactly like the browser and Android clients. /// /// The server builds these patches with the Go `internal/filediff` writer: a /// standard `---`/`+++` header, optional metadata, `@@ -a,b +c,d @@` hunk /// headers and a ` `/`+`/`-` body. Anything unrecognised before the first hunk /// is carried verbatim in [ParsedPatch.headerLines]; a truncated tail or exotic /// marker inside a hunk stays an unnumbered context line. The raw patch is /// always retained byte-for-byte so Copy patch never rewrites the record. library; export 'patch_models.dart'; import 'patch_models.dart'; const _emptyPatch = ParsedPatch( raw: '', headerLines: [], hunks: [], added: 0, removed: 0, ); /// Parses a patch with the same total behaviour as the browser: a blank or /// absent patch is an empty result, never a fabricated diff. ParsedPatch parseUnifiedPatch(dynamic patch) { final text = patch == null ? '' : '$patch'; if (text.trim().isEmpty) return _emptyPatch; final headers = []; final hunks = []; _HunkBuilder? current; for (final line in text.split('\n')) { final start = _parseHunkHeader(line); if (start != null) { if (current != null) hunks.add(current.build()); current = _HunkBuilder(line, start); } else if (line.startsWith('--- ') || line.startsWith('+++ ')) { // An aggregated entry can contain one complete patch per contributing // stage. A new file header closes the preceding hunk instead of becoming // an unnumbered context line inside it. if (current != null) { hunks.add(current.build()); current = null; } headers.add(line); } else if (current == null) { headers.add(line); } else { current.lines.add(current.parseBody(line)); } } if (current != null) hunks.add(current.build()); var added = 0; var removed = 0; for (final hunk in hunks) { for (final line in hunk.lines) { if (line.kind == ChangeLineKind.add) { added++; } else if (line.kind == ChangeLineKind.remove) { removed++; } } } return ParsedPatch( raw: text, headerLines: headers, hunks: hunks, added: added, removed: removed, ); } /// Mutable accumulator carrying the running old/new counters so each body line /// is numbered in one pass. class _HunkBuilder { _HunkBuilder(this.header, _HunkStart start) : oldStart = start.oldStart, oldCount = start.oldCount, newStart = start.newStart, newCount = start.newCount, oldLine = start.oldStart, newLine = start.newStart; final String header; final int oldStart; final int oldCount; final int newStart; final int newCount; final List lines = []; int oldLine; int newLine; PatchHunk build() => PatchHunk( header: header, oldStart: oldStart, oldCount: oldCount, newStart: newStart, newCount: newCount, lines: List.unmodifiable(lines), ); PatchLine parseBody(String line) { if (line.startsWith('+')) { return PatchLine( kind: ChangeLineKind.add, text: line.substring(1), oldLine: 0, newLine: newLine++, ); } if (line.startsWith('-')) { return PatchLine( kind: ChangeLineKind.remove, text: line.substring(1), oldLine: oldLine++, newLine: 0, ); } if (line.startsWith('\\')) { return PatchLine( kind: ChangeLineKind.context, text: line, oldLine: 0, newLine: 0, noNewlineMarker: true, ); } if (line.startsWith(' ')) { return PatchLine( kind: ChangeLineKind.context, text: line.substring(1), oldLine: oldLine++, newLine: newLine++, ); } if (line.isEmpty) { return PatchLine( kind: ChangeLineKind.context, text: '', oldLine: oldLine++, newLine: newLine++, ); } // A truncated tail or an unexpected marker: keep the text, do not number it. return PatchLine( kind: ChangeLineKind.context, text: line, oldLine: 0, newLine: 0, ); } } class _HunkStart { const _HunkStart(this.oldStart, this.oldCount, this.newStart, this.newCount); final int oldStart; final int oldCount; final int newStart; final int newCount; } _HunkStart? _parseHunkHeader(String line) { if (!line.startsWith('@@')) return null; final end = line.indexOf('@@', 2); if (end < 0) return null; final ranges = _tokenizeRanges(line.substring(2, end)); return ranges == null ? null : _HunkStart(ranges[0], ranges[1], ranges[2], ranges[3]); } List? _tokenizeRanges(String body) { final plus = body.indexOf('+'); final old = plus == -1 ? body : body.substring(0, plus); final next = plus == -1 ? '' : body.substring(plus + 1); if (old.isEmpty || next.isEmpty) return null; final oldRange = _parseRange(old); if (oldRange == null) return null; final newRange = _parseRange(next); if (newRange == null) return null; return [oldRange[0], oldRange[1], newRange[0], newRange[1]]; } List? _parseRange(String text) { var trimmed = text.trim(); if (trimmed.startsWith('-') || trimmed.startsWith('+')) { trimmed = trimmed.substring(1); } if (trimmed.isEmpty) return null; final comma = trimmed.indexOf(','); final startText = (comma == -1 ? trimmed : trimmed.substring(0, comma)) .trim(); final start = _intOrNull(startText); if (start == null) return null; final countText = (comma == -1 ? '' : trimmed.substring(comma + 1)).trim(); final count = countText.isEmpty ? 1 : _intOrNull(countText); if (count == null) return null; return [start, count]; } int? _intOrNull(String text) => RegExp(r'^[+-]?\d+$').hasMatch(text) ? int.tryParse(text) : null; /// The widest line number in a hunk, so its gutter lines up across every row. int gutterWidth(PatchHunk hunk) { var widest = 0; for (final line in hunk.lines) { if (line.oldLine > widest) widest = line.oldLine; if (line.newLine > widest) widest = line.newLine; } return '${widest < 1 ? 1 : widest}'.length; } /// One gutter number padded to [width]; 0 means no number on that side. String gutterText(int value, int width) => value > 0 ? '$value'.padLeft(width) : ' ' * width; /// The gutter symbol one line kind is drawn with (`+`/`-`/space). String diffLineSymbol(ChangeLineKind kind) => switch (kind) { ChangeLineKind.add => '+', ChangeLineKind.remove => '-', ChangeLineKind.context => ' ', }; /// The `+N -M` stat, or "" when both counts are zero. Shared by every renderer. String changeStat(int added, int removed) { var out = ''; if (added > 0) out += '+$added'; if (removed > 0) { if (out.isNotEmpty) out += ' '; out += '-$removed'; } return out; }