reversePatch method

List<DiffHunk> reversePatch(
  1. List<DiffHunk> hunks
)

Reverse a patch so that applying the reversed hunks undoes the original.

Implementation

List<DiffHunk> reversePatch(List<DiffHunk> hunks) {
  return hunks.map((hunk) {
    final reversedLines = hunk.lines.map((line) {
      final newType = switch (line.type) {
        DiffLineType.add => DiffLineType.remove,
        DiffLineType.remove => DiffLineType.add,
        DiffLineType.context => DiffLineType.context,
      };
      return DiffLine(
        type: newType,
        content: line.content,
        oldLineNumber: line.newLineNumber,
        newLineNumber: line.oldLineNumber,
      );
    }).toList();

    return DiffHunk(
      oldStart: hunk.newStart,
      oldCount: hunk.newCount,
      newStart: hunk.oldStart,
      newCount: hunk.oldCount,
      lines: reversedLines,
    );
  }).toList();
}