Recover before/after bytes for displaying a whole-file CRDT rewrite. Such rewrites tombstone old branch identities without embedding their text, then insert a fresh, numbered chain from the file-start sentinel. These are storage operations, not a human-readable diff. Deleted branch numbers may include historical tombstones, so callers must obtain the before-content from recorded history rather th
(
ops: &atomic_core::change::FileOps,
contents: &[u8],
before: impl FnOnce() -> Option<Vec<u8>>,
)
| 1581 | /// The resolver is called only after recognizing the rewrite's operation shape. |
| 1582 | /// Ordinary edits, file lifecycle operations, and incomplete chains return None. |
| 1583 | pub fn whole_file_replacement_text( |
| 1584 | ops: &atomic_core::change::FileOps, |
| 1585 | contents: &[u8], |
| 1586 | before: impl FnOnce() -> Option<Vec<u8>>, |
| 1587 | ) -> Option<(Vec<u8>, Vec<u8>)> { |
| 1588 | if ops.trunk_op().is_some() { |
| 1589 | return None; |
| 1590 | } |
| 1591 | let mut deletions = 0; |
| 1592 | let mut previous = None; |
| 1593 | let mut next_line = 1; |
| 1594 | let mut previous_end = None; |
| 1595 | let mut bytes = Vec::new(); |
| 1596 | for line in ops.line_ops() { |
| 1597 | match line.operation() { |
| 1598 | BranchOp::Delete { content, .. } if content.is_empty() && previous.is_none() => { |
| 1599 | deletions += 1; |
| 1600 | if line.old_line_num() != Some(deletions) { |
| 1601 | return None; |
| 1602 | } |
| 1603 | } |
| 1604 | BranchOp::Insert { after, .. } |
| 1605 | if *after == previous && line.new_line_num() == Some(next_line) => |
| 1606 | { |
| 1607 | // Leaf tokens omit line terminators. The canonical byte ranges |
| 1608 | // preserve blank lines, CRLF, and an unterminated final line. |
| 1609 | let (start, end) = line.content_range()?; |
| 1610 | let start = usize::try_from(start.get()).ok()?; |
| 1611 | let end = usize::try_from(end.get()).ok()?; |
| 1612 | if previous_end.is_some_and(|previous| previous != start) { |
| 1613 | return None; |
| 1614 | } |
| 1615 | bytes.extend_from_slice(contents.get(start..end)?); |
| 1616 | previous_end = Some(end); |
| 1617 | previous = Some(line.branch_id()); |
| 1618 | next_line += 1; |
| 1619 | } |
| 1620 | _ => return None, |
| 1621 | } |
| 1622 | } |
| 1623 | if deletions == 0 { |
| 1624 | return None; |
| 1625 | } |
| 1626 | let before = before()?; |
| 1627 | // A normal edit may delete a blank line with empty tokens. A rewrite |
| 1628 | // tombstones at least every old line (plus any observed historical IDs). |
| 1629 | if deletions < atomic_core::diff::Line::from_bytes(&before).len() { |
| 1630 | return None; |
| 1631 | } |
| 1632 | Some((before, bytes)) |
| 1633 | } |
| 1634 | |
| 1635 | /// The resolver-parameterized core of [`change_file_diffs`]: `before` |
| 1636 | /// supplies a file's pre-change content for context padding (a `None` |
no test coverage detected