use continuity_buffer::Buffer;
use continuity_text::{Position, Selection};
use crate::selection_edit::{apply_plan, plan, SelectionEdit};
fn at(line: u32, col: u32) -> Selection {
Selection::caret_at(Position::new(line, col))
}
fn build(text: &str, selections: Vec<Selection>) -> Buffer {
let mut buffer = Buffer::from_text(text);
buffer.set_selections(selections);
buffer
}
fn run(buffer: &mut Buffer, edit: SelectionEdit) {
let plan = plan(buffer, &edit).expect("plan ok").expect("plan some");
apply_plan(buffer, &plan).expect("apply ok");
}
#[test]
fn newline_above_inserts_blank_line() {
let mut b = build("hi", vec![at(0, 1)]);
run(&mut b, SelectionEdit::InsertNewlineAbove);
assert_eq!(b.rope().to_string(), "\nhi");
}
#[test]
fn newline_below_inserts_blank_line() {
let mut b = build("hi", vec![at(0, 0)]);
run(&mut b, SelectionEdit::InsertNewlineBelow);
assert_eq!(b.rope().to_string(), "hi\n");
}
#[test]
fn newline_smart_copies_indent() {
let mut b = build(" if x:", vec![at(0, 9)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), " if x:\n ");
}
#[test]
fn newline_smart_multicursor_positions_accumulate_prior_newlines() {
let mut b = build(" aa\n bb\n cc", vec![at(0, 4), at(1, 4), at(2, 4)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), " aa\n \n bb\n \n cc\n ");
assert_eq!(
b.selections()
.iter()
.map(|selection| selection.head)
.collect::<Vec<_>>(),
vec![
Position::new(1, 2),
Position::new(3, 2),
Position::new(5, 2)
]
);
}
#[test]
fn newline_smart_list_continue_increment_end() {
let mut b = build("- foo", vec![at(0, 5)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "- foo\n- ");
let mut b = build("3. foo", vec![at(0, 6)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "1. foo\n2. ");
let mut b = build("- ", vec![at(0, 2)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "");
}
#[test]
fn toggle_bullet_at_line_start_adds_prefix_and_shifts_caret() {
let mut b = build("hello", vec![at(0, 3)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- hello");
let head = b.selections()[0].head;
assert_eq!(head.line, 0);
assert_eq!(head.byte_in_line, 5);
}
#[test]
fn toggle_bullet_at_line_start_removes_prefix_and_shifts_caret_back() {
let mut b = build("- hello", vec![at(0, 5)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "hello");
let head = b.selections()[0].head;
assert_eq!(head.byte_in_line, 3);
}
#[test]
fn toggle_bullet_at_line_start_respects_leading_whitespace() {
let mut b = build(" foo", vec![at(0, 6)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), " - foo");
let head = b.selections()[0].head;
assert_eq!(head.byte_in_line, 8);
}
#[test]
fn toggle_bullet_at_line_start_strips_asterisk_too() {
let mut b = build("* item", vec![at(0, 4)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "item");
let head = b.selections()[0].head;
assert_eq!(head.byte_in_line, 2);
}
#[test]
fn toggle_bullet_at_line_start_caret_in_leading_whitespace_stays_put() {
let mut b = build(" foo", vec![at(0, 2)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), " - foo");
let head = b.selections()[0].head;
assert_eq!(head.byte_in_line, 2);
}
#[test]
fn toggle_bullet_multiline_none_bulleted_adds_to_all() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("foo\nbar\nbaz", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo\n- bar\n- baz");
}
#[test]
fn toggle_bullet_multiline_skips_blank_and_whitespace_lines() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(4, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("foo\n\n \t\nbar\nbaz", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo\n\n \t\n- bar\n- baz");
}
#[test]
fn toggle_bullet_multiline_all_bulleted_check_ignores_blank_lines() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 5),
continuity_text::SelectionKind::Caret,
);
let mut b = build("- foo\n\n- bar", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "foo\n\nbar");
}
#[test]
fn toggle_bullet_single_blank_line_still_starts_list() {
let mut b = build("", vec![at(0, 0)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- ");
}
#[test]
fn toggle_bullet_multiline_some_bulleted_adds_to_missing_only() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("foo\n- bar\nbaz", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo\n- bar\n- baz");
}
#[test]
fn toggle_bullet_multiline_all_bulleted_strips_all() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 5),
continuity_text::SelectionKind::Caret,
);
let mut b = build("- foo\n* bar\n+ baz", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "foo\nbar\nbaz");
}
#[test]
fn delete_to_line_start_keeps_other_lines() {
let mut b = build("ab\ncdef", vec![at(1, 3)]);
run(&mut b, SelectionEdit::DeleteToLineStart);
assert_eq!(b.rope().to_string(), "ab\nf");
}
#[test]
fn delete_to_line_end_drops_tail() {
let mut b = build("hello world", vec![at(0, 5)]);
run(&mut b, SelectionEdit::DeleteToLineEnd);
assert_eq!(b.rope().to_string(), "hello");
}
#[test]
fn duplicate_line_appends_copy() {
let mut b = build("first\nsecond", vec![at(0, 0)]);
run(&mut b, SelectionEdit::DuplicateLine);
assert_eq!(b.rope().to_string(), "first\nfirst\nsecond");
}
#[test]
fn duplicate_selection_repeats_span() {
let mut b = Buffer::from_text("ab");
b.set_selections(vec![Selection::new(
Position::new(0, 0),
Position::new(0, 2),
continuity_text::SelectionKind::Caret,
)]);
run(&mut b, SelectionEdit::DuplicateSelection);
assert_eq!(b.rope().to_string(), "abab");
}
#[test]
fn move_line_up_swaps_neighbors() {
let mut b = build("a\nb", vec![at(1, 0)]);
run(&mut b, SelectionEdit::MoveLineUp);
assert_eq!(b.rope().to_string(), "b\na");
}
#[test]
fn move_line_down_swaps_neighbors() {
let mut b = build("a\nb\nc", vec![at(0, 0)]);
run(&mut b, SelectionEdit::MoveLineDown);
assert_eq!(b.rope().to_string(), "b\na\nc");
}
#[test]
fn join_lines_compacts_whitespace() {
let mut b = build("hello\n world", vec![at(0, 5)]);
run(&mut b, SelectionEdit::JoinLines);
assert_eq!(b.rope().to_string(), "hello world");
}
fn block_selection(start: (u32, u32), end: (u32, u32)) -> Selection {
Selection::new(
Position::new(start.0, start.1),
Position::new(end.0, end.1),
continuity_text::SelectionKind::Caret,
)
}
#[test]
fn join_selected_adjacent_lines_use_single_space() {
let mut b = build("alpha\nbeta\ngamma", vec![block_selection((0, 0), (2, 5))]);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "alpha beta gamma");
}
#[test]
fn join_selected_keeps_sections_separated_by_blank_lines() {
let mut b = build(
"alpha\nbeta\n\ngamma",
vec![block_selection((0, 0), (3, 5))],
);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "alpha beta\ngamma");
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "alpha beta gamma");
}
#[test]
fn join_selected_reduces_multi_blank_runs_one_per_press() {
let mut b = build("a\n\n\nb", vec![block_selection((0, 0), (3, 1))]);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "a\n\nb");
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "a\nb");
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "a b");
}
#[test]
fn join_selected_strips_bullet_markers_from_continuations() {
let mut b = build(
"- alpha\n- beta\n- gamma",
vec![block_selection((0, 0), (2, 7))],
);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "- alpha beta gamma");
}
#[test]
fn join_selected_strips_numbered_and_task_markers() {
let mut b = build(
"1. alpha\n2. beta\n- [ ] gamma",
vec![block_selection((0, 0), (2, 11))],
);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "1. alpha beta gamma");
}
#[test]
fn join_selected_keeps_non_marker_dashes() {
let mut b = build(
"alpha\n-not-a-bullet",
vec![block_selection((0, 0), (1, 5))],
);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "alpha -not-a-bullet");
}
#[test]
fn join_selected_preserves_trailing_newline_after_block() {
let mut b = build("a\nb\nrest", vec![block_selection((0, 0), (1, 1))]);
run(&mut b, SelectionEdit::JoinSelectedLines);
assert_eq!(b.rope().to_string(), "a b\nrest");
}
#[test]
fn toggle_bullet_on_ordered_line_converts_to_dash() {
let mut b = build("1. foo", vec![at(0, 3)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo");
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "foo");
}
#[test]
fn toggle_bullet_ordered_paren_marker_converts_to_dash() {
let mut b = build("2) foo", vec![at(0, 3)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo");
}
#[test]
fn toggle_bullet_ordered_caret_column_shifts_for_prefix_change() {
let mut b = build("12. foo", vec![at(0, 4)]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- foo");
assert_eq!(b.selections()[0].head.byte_in_line, 2);
}
#[test]
fn toggle_bullet_multiline_ordered_run_counts_as_having_prefix() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 5),
continuity_text::SelectionKind::Caret,
);
let mut b = build("1. a\n2. b\n3. c", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- a\n- b\n- c");
}
#[test]
fn toggle_bullet_mixed_ordered_and_plain_converges_to_dash() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(1, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("1. a\nfoo", vec![sel]);
run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
assert_eq!(b.rope().to_string(), "- a\n- foo");
}
fn continuation_edit() -> SelectionEdit {
SelectionEdit::ToggleBulletWithContinuationIndent {
unit: crate::IndentUnit::Spaces(2),
}
}
#[test]
fn toggle_bullet_continuation_indents_lines_after_first() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("foo\nbar\nbaz", vec![sel]);
run(&mut b, continuation_edit());
assert_eq!(b.rope().to_string(), "- foo\n - bar\n - baz");
}
#[test]
fn toggle_bullet_continuation_strips_bullet_and_indent() {
let sel = Selection::new(
Position::new(0, 0),
Position::new(2, 3),
continuity_text::SelectionKind::Caret,
);
let mut b = build("foo\nbar\nbaz", vec![sel]);
run(&mut b, continuation_edit());
assert_eq!(b.rope().to_string(), "- foo\n - bar\n - baz");
let sel2 = Selection::new(
Position::new(0, 0),
Position::new(2, 7),
continuity_text::SelectionKind::Caret,
);
b.set_selections(vec![sel2]);
run(&mut b, continuation_edit());
assert_eq!(b.rope().to_string(), "foo\nbar\nbaz");
}
#[test]
fn toggle_bullet_continuation_single_line_behaves_like_plain() {
let mut b = build("hello", vec![at(0, 0)]);
run(&mut b, continuation_edit());
assert_eq!(b.rope().to_string(), "- hello");
}
#[test]
fn enter_on_task_line_continues_with_checkbox() {
let mut b = build("- [ ] a", vec![at(0, 7)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "- [ ] a\n- [ ] ");
}
#[test]
fn enter_on_checked_task_line_continues_unchecked() {
let mut b = build("- [x] done", vec![at(0, 10)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "- [x] done\n- [ ] ");
}
#[test]
fn enter_on_empty_task_stub_ends_the_list() {
let mut b = build("- [ ] ", vec![at(0, 6)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "");
}
#[test]
fn enter_inside_ordered_list_renumbers_following_items() {
let mut b = build("1. a\n2. b\n3. c", vec![at(1, 4)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "1. a\n2. b\n3. \n4. c");
let head = b.selections()[0].head;
assert_eq!((head.line, head.byte_in_line), (2, 3));
}
#[test]
fn enter_at_end_of_ordered_list_appends_next_number() {
let mut b = build("1. a\n2. b", vec![at(1, 4)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "1. a\n2. b\n3. ");
}
#[test]
fn enter_renumbers_mis_numbered_ordered_run() {
let mut b = build("1. a\n5. b", vec![at(1, 4)]);
run(&mut b, SelectionEdit::InsertNewlineSmart);
assert_eq!(b.rope().to_string(), "1. a\n2. b\n3. ");
}