Skip to main content

compute_damage_range

Function compute_damage_range 

Source
pub fn compute_damage_range(
    old: &[String],
    new: &[String],
    cursor_row: usize,
) -> Option<Range<usize>>
Expand description

Compute the row range that differs between old and new, with a cursor-row hint to accelerate the common single-character-edit case.

Contract: cursor_row must be the row that was actually edited (the editor’s cursor position after the keystroke). The fast path trusts this — if cursor_row does not identify the real edit point, the function may under-report the damaged range for an edit shape that single-keystroke editing cannot produce. Distant simultaneous edits are out of scope; they can only happen via programmatic buffer replacement, which goes through set_text and bumps text_revision such that the LCP/LCS slow path is taken naturally (the cursor row’s content will match between old and new, so the fast path declines and the slow path runs).

Returns None when the buffers are byte-identical (defensive guard — callers should already have gated on text_revision).

Fast path: same line count, the row at cursor_row differs, and no other line in ±CURSOR_HINT_WINDOW differs. Returns Some(cursor_row..cursor_row + 1). O(CURSOR_HINT_WINDOW).

Slow path: longest common prefix (LCP) and longest common suffix (LCS); damaged range is the middle slice. O(min(buffer_size, damage_size)).