continuity-engine 0.2.36

Synchronous, storage-neutral Continuity editor engine
Documentation
//! Unit tests for the storage-neutral selection-edit dispatcher and the
//! [`apply_plan`] coalescing path. Split out of `selection_edit.rs` to keep
//! that file under the 600-line cap.

use continuity_buffer::Buffer;
use continuity_text::{Position, Selection, SelectionKind};

use super::*;

#[test]
fn insert_plans_one_op_per_caret_descending() {
    let mut buffer = Buffer::from_text("abcd");
    buffer.set_selections(vec![
        Selection::caret_at(Position::new(0, 1)),
        Selection::caret_at(Position::new(0, 3)),
    ]);
    let plan = plan(&buffer, &SelectionEdit::InsertText("x".into()))
        .expect("plan ok")
        .expect("plan some");
    assert_eq!(plan.ops.len(), 2);
    apply_plan(&mut buffer, &plan).expect("apply ok");
    assert_eq!(buffer.rope().to_string(), "axbcxd");
    assert_eq!(
        buffer
            .selections()
            .iter()
            .map(|selection| selection.head)
            .collect::<Vec<_>>(),
        vec![Position::new(0, 2), Position::new(0, 5)]
    );
}

#[test]
fn multiline_insert_accumulates_prior_cursor_edits() {
    let mut buffer = Buffer::from_text("aa\nbb\ncc");
    buffer.set_selections(vec![
        Selection::caret_at(Position::new(0, 2)),
        Selection::caret_at(Position::new(1, 2)),
        Selection::caret_at(Position::new(2, 2)),
    ]);
    let plan = plan(&buffer, &SelectionEdit::InsertText("\n".into()))
        .expect("plan ok")
        .expect("plan some");
    apply_plan(&mut buffer, &plan).expect("apply ok");
    assert_eq!(buffer.rope().to_string(), "aa\n\nbb\n\ncc\n");
    assert_eq!(
        buffer
            .selections()
            .iter()
            .map(|selection| selection.head)
            .collect::<Vec<_>>(),
        vec![
            Position::new(1, 0),
            Position::new(3, 0),
            Position::new(5, 0)
        ]
    );
}

#[test]
fn block_delete_uses_each_touched_line() {
    let mut buffer = Buffer::from_text("abcd\nwxyz\n");
    buffer.set_selections(vec![Selection::new(
        Position::new(0, 1),
        Position::new(1, 3),
        SelectionKind::BlockWise,
    )]);
    let plan = plan(&buffer, &SelectionEdit::DeleteForward)
        .expect("plan ok")
        .expect("plan some");
    apply_plan(&mut buffer, &plan).expect("apply ok");
    assert_eq!(buffer.rope().to_string(), "ad\nwz\n");
}

#[test]
fn delete_back_at_doc_start_is_noop() {
    let buffer = Buffer::from_text("abc");
    let plan = plan(&buffer, &SelectionEdit::DeleteBack).expect("plan ok");
    assert!(plan.is_none());
}

#[test]
fn apply_plan_coalesces_colliding_carets() {
    // Two carets at distinct positions on the same line, both insert "x".
    // The two inserts at byte 1 and byte 2 produce post-edit carets at
    // bytes 2 and 4 — distinct, so no collision. To force a collision,
    // we wire a synthetic plan with duplicate selections_after.
    let mut buffer = Buffer::from_text("abcdef");
    buffer.set_selections(vec![Selection::caret_at(Position::new(0, 0))]);
    let plan = SelectionEditPlan {
        ops: Vec::new(),
        selections_before: vec![Selection::caret_at(Position::new(0, 0))],
        selections_after: vec![
            Selection::caret_at(Position::new(0, 3)),
            Selection::caret_at(Position::new(0, 3)),
        ],
    };
    apply_plan(&mut buffer, &plan).expect("apply ok");
    assert_eq!(buffer.selections().len(), 1);
    assert_eq!(buffer.selections()[0].head, Position::new(0, 3));
}