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>,
)
| 738 | /// boundary at `old_start`. For single-vertex files there are no internal |
| 739 | /// boundaries, so we return an error and let the caller fall back to Replace. |
| 740 | fn classify_insert( |
| 741 | vertices: &[GraphNode<NodeId>], |
| 742 | inode_pos: Position<NodeId>, |
| 743 | old_start: usize, |
| 744 | old_line_count: Option<usize>, |
| 745 | ) -> GlobalizeResult<InsertPosition> { |
| 746 | log::debug!( |
| 747 | "classify_insert: old_start={} old_line_count={:?} vertices={}", |
| 748 | old_start, |
| 749 | old_line_count, |
| 750 | vertices.len(), |
| 751 | ); |
| 752 | |
| 753 | // Empty file → append (predecessor is the inode itself). |
| 754 | if vertices.is_empty() { |
| 755 | return Ok(InsertPosition::Append { |
| 756 | last_content_end: inode_pos, |
| 757 | }); |
| 758 | } |
| 759 | |
| 760 | // Prepend: insert before the very first line. |
| 761 | if old_start == 0 { |
| 762 | let first_start = Position::new(vertices[0].change, vertices[0].start); |
| 763 | return Ok(InsertPosition::Prepend { |
| 764 | first_content: first_start, |
| 765 | }); |
| 766 | } |
| 767 | |
| 768 | // Append: insert after the very last line. |
| 769 | let total_old_lines = old_line_count.unwrap_or(vertices.len()); |
| 770 | if old_start >= total_old_lines { |
| 771 | let last = vertices.last().unwrap(); |
| 772 | let last_end = Position::new(last.change, last.end); |
| 773 | return Ok(InsertPosition::Append { |
| 774 | last_content_end: last_end, |
| 775 | }); |
| 776 | } |
| 777 | |
| 778 | // ── Middle insertion ── |
| 779 | // |
| 780 | // For single-vertex files we cannot split at a line boundary, so we |
| 781 | // return an error. The caller (`globalize_insert`) catches this and |
| 782 | // falls back to a whole-file Replace. |
| 783 | if vertices.len() == 1 { |
| 784 | return Err(GlobalizeError::MissingContext { |
| 785 | path: "(middle insertion into single-vertex file — needs consolidation)".to_string(), |
| 786 | line: old_start as u64, |
| 787 | }); |
| 788 | } |
| 789 | |
| 790 | // Multi-vertex file: walk vertices to find the boundary. |
| 791 | // After per-line vertex creation, each vertex corresponds roughly |
| 792 | // to one line, so vertex[old_start-1] / vertex[old_start] gives |
| 793 | // the correct boundary. |
| 794 | if old_start < vertices.len() { |
| 795 | let pred = &vertices[old_start - 1]; |
| 796 | let succ = &vertices[old_start]; |
| 797 | return Ok(InsertPosition::Middle { |
no test coverage detected