MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / classify_insert

Function classify_insert

atomic-core/src/record/workflow/globalize/hunk.rs:645–716  ·  view source on GitHub ↗

Determine whether an Insert hunk is a Prepend, Append, or Middle insert. For multi-vertex files (files that have been recorded at least once with per-line vertex granularity), a middle insert is resolved to the vertex boundary at `old_start`. For single-vertex files there are no internal boundaries, so we return an error and let the caller fall back to Replace.

(
    vertices: &[GraphNode<NodeId>],
    inode_pos: Position<NodeId>,
    old_start: usize,
    old_line_count: Option<usize>,
)

Source from the content-addressed store, hash-verified

643/// boundary at `old_start`. For single-vertex files there are no internal
644/// boundaries, so we return an error and let the caller fall back to Replace.
645fn classify_insert(
646 vertices: &[GraphNode<NodeId>],
647 inode_pos: Position<NodeId>,
648 old_start: usize,
649 old_line_count: Option<usize>,
650) -> GlobalizeResult<InsertPosition> {
651 log::debug!(
652 "classify_insert: old_start={} old_line_count={:?} vertices={}",
653 old_start,
654 old_line_count,
655 vertices.len(),
656 );
657
658 // Empty file → append (predecessor is the inode itself).
659 if vertices.is_empty() {
660 return Ok(InsertPosition::Append {
661 last_content_end: inode_pos,
662 });
663 }
664
665 // Prepend: insert before the very first line.
666 if old_start == 0 {
667 let first_start = Position::new(vertices[0].change, vertices[0].start);
668 return Ok(InsertPosition::Prepend {
669 first_content: first_start,
670 });
671 }
672
673 // Append: insert after the very last line.
674 let total_old_lines = old_line_count.unwrap_or(vertices.len());
675 if old_start >= total_old_lines {
676 let last = vertices.last().unwrap();
677 let last_end = Position::new(last.change, last.end);
678 return Ok(InsertPosition::Append {
679 last_content_end: last_end,
680 });
681 }
682
683 // ── Middle insertion ──
684 //
685 // For single-vertex files we cannot split at a line boundary, so we
686 // return an error. The caller (`globalize_insert`) catches this and
687 // falls back to a whole-file Replace.
688 if vertices.len() == 1 {
689 return Err(GlobalizeError::MissingContext {
690 path: "(middle insertion into single-vertex file — needs consolidation)".to_string(),
691 line: old_start as u64,
692 });
693 }
694
695 // Multi-vertex file: walk vertices to find the boundary.
696 // After per-line vertex creation, each vertex corresponds roughly
697 // to one line, so vertex[old_start-1] / vertex[old_start] gives
698 // the correct boundary.
699 if old_start < vertices.len() {
700 let pred = &vertices[old_start - 1];
701 let succ = &vertices[old_start];
702 return Ok(InsertPosition::Middle {

Callers 2

globalize_insertFunction · 0.85
globalize_replaceFunction · 0.85

Calls 4

lastMethod · 0.80
is_emptyMethod · 0.45
lenMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected