Skip to main content

reconcile_cursor_position

Function reconcile_cursor_position 

Source
pub fn reconcile_cursor_position(
    old_text: &str,
    new_text: &str,
    old_cursor_byte: usize,
) -> usize
Expand description

Reconcile cursor byte position when text content changes.

This function maps a cursor position from old text to new text, preserving the cursor’s logical position as much as possible:

  1. If cursor is in unchanged prefix → stays at same byte offset
  2. If cursor is in unchanged suffix → adjusts by length difference
  3. If cursor is in changed region → places at end of new content

§Arguments

  • old_text - The previous text content
  • new_text - The new text content
  • old_cursor_byte - Cursor byte offset in old text

§Returns

The reconciled cursor byte offset in new text

§Example

let old_text = "Hello";
let new_text = "Hello World";
let old_cursor = 5; // cursor at end of "Hello"
let new_cursor = reconcile_cursor_position(old_text, new_text, old_cursor);
assert_eq!(new_cursor, 5); // cursor stays at same position (prefix unchanged)