Skip to main content

transform_position

Function transform_position 

Source
pub fn transform_position(pos: Position, edit: &AppliedEdit) -> Position
Expand description

Transform a position across an edit (§4.1 of owner-write-caret.md).

Pure, total function: every position has a well-defined result after any edit. Composes left-to-right across batches.

Where p is relative to edit.original_rangeResult
strictly before original_range.startunchanged
at or after original_range.endshifted by inserted_range.end - original_range.end
strictly inside original_rangeclamped to inserted_range.end
at original_range.start (non-empty range)stays at inserted_range.start

§Examples

use lattice_core::Buffer;
use lattice_core::buffer::transform_position;
use lattice_core::protocol::edit::Edit;
use lattice_core::protocol::position::Position;

let mut buf = Buffer::from_text("abc def");
// Insert "XY" at byte 1 of line 0.
let applied = buf.apply_edit(&Edit::insert(Position::new(0, 1), "XY"))?;

// A caret after the insert point shifts right by the inserted length…
assert_eq!(transform_position(Position::new(0, 4), &applied), Position::new(0, 6));
// …one before it does not move.
assert_eq!(transform_position(Position::new(0, 0), &applied), Position::new(0, 0));