use continuity_buffer::Buffer;
use continuity_text::{Position, Selection, SelectionKind};
use super::*;
use crate::selection_edit::{apply_plan, plan, SelectionEdit};
fn caret(line: u32, col: u32) -> Selection {
Selection::caret_at(Position::new(line, col))
}
fn span(start: (u32, u32), end: (u32, u32)) -> Selection {
Selection::new(
Position::new(start.0, start.1),
Position::new(end.0, end.1),
SelectionKind::Caret,
)
}
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 toggle_bold_wraps_selection() {
let mut b = Buffer::from_text("text");
b.set_selections(vec![span((0, 0), (0, 4))]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "**text**");
}
#[test]
fn toggle_bold_multiline_wraps_each_line() {
let mut b = Buffer::from_text("foo\nbar");
b.set_selections(vec![span((0, 0), (1, 3))]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "**foo**\n**bar**");
}
#[test]
fn toggle_bold_multiline_preserves_indent_and_blank_lines() {
let mut b = Buffer::from_text(" foo\n\n bar");
b.set_selections(vec![span((0, 0), (2, 5))]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), " **foo**\n\n **bar**");
}
#[test]
fn toggle_bold_strips_existing_wrap() {
let mut b = Buffer::from_text("**text**");
b.set_selections(vec![span((0, 0), (0, 8))]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "text");
}
#[test]
fn toggle_bold_caret_inside_span_strips_markers() {
let mut b = Buffer::from_text("**bold**");
b.set_selections(vec![caret(0, 4)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "bold");
}
#[test]
fn toggle_bold_caret_at_inner_edge_strips_markers() {
let mut b = Buffer::from_text("**bold**");
b.set_selections(vec![caret(0, 2)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "bold");
}
#[test]
fn toggle_bold_caret_in_plain_text_inserts_empty_pair() {
let mut b = Buffer::from_text("plain");
b.set_selections(vec![caret(0, 2)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Bold),
);
assert_eq!(b.rope().to_string(), "pl****ain");
}
#[test]
fn toggle_italic_caret_inside_span_strips_single_asterisks() {
let mut b = Buffer::from_text("*em*");
b.set_selections(vec![caret(0, 2)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Italic),
);
assert_eq!(b.rope().to_string(), "em");
}
#[test]
fn toggle_italic_caret_inside_bold_inserts_pair_not_strip() {
let mut b = Buffer::from_text("**bold**");
b.set_selections(vec![caret(0, 4)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Italic),
);
assert_eq!(b.rope().to_string(), "**bo**ld**");
}
#[test]
fn toggle_strikethrough_caret_inside_span_strips_markers() {
let mut b = Buffer::from_text("~~gone~~");
b.set_selections(vec![caret(0, 4)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::Strikethrough),
);
assert_eq!(b.rope().to_string(), "gone");
}
#[test]
fn toggle_inline_code_caret_inside_span_strips_backticks() {
let mut b = Buffer::from_text("`code`");
b.set_selections(vec![caret(0, 3)]);
run(
&mut b,
SelectionEdit::MarkdownToggleEmphasis(EmphasisKind::InlineCode),
);
assert_eq!(b.rope().to_string(), "code");
}
#[test]
fn toggle_bullet_adds_then_removes() {
let mut b = Buffer::from_text("a");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- a");
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "a");
}
#[test]
fn toggle_bullet_multiline_skips_blank_lines() {
let mut b = Buffer::from_text("a\n\n \nb");
b.set_selections(vec![span((0, 0), (3, 1))]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- a\n\n \n- b");
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "a\n\n \nb");
}
#[test]
fn toggle_bullet_preserves_caret_on_the_same_content_character() {
let mut b = Buffer::from_text("alpha");
b.set_selections(vec![caret(0, 2)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- alpha");
assert_eq!(b.selections(), &[caret(0, 4)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "alpha");
assert_eq!(b.selections(), &[caret(0, 2)]);
}
#[test]
fn toggle_bullet_on_empty_line_places_caret_after_marker() {
let mut b = Buffer::from_text("");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- ");
assert_eq!(b.selections(), &[caret(0, 2)]);
}
#[test]
fn toggle_bullet_respects_leading_whitespace() {
let mut b = Buffer::from_text(" alpha");
b.set_selections(vec![caret(0, 4)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), " - alpha");
assert_eq!(b.selections(), &[caret(0, 6)]);
}
#[test]
fn toggle_bullet_shifts_both_ends_of_a_backward_range() {
let mut b = Buffer::from_text("alpha");
b.set_selections(vec![span((0, 3), (0, 0))]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- alpha");
assert_eq!(b.selections(), &[span((0, 5), (0, 2))]);
}
#[test]
fn toggle_bullet_shifts_both_ends_of_a_forward_multiline_range() {
let mut b = Buffer::from_text("alpha\nbravo");
b.set_selections(vec![span((0, 1), (1, 3))]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- alpha\n- bravo");
assert_eq!(b.selections(), &[span((0, 3), (1, 5))]);
}
#[test]
fn toggle_bullet_removes_marker_and_walks_caret_back() {
let mut b = Buffer::from_text("- alpha");
b.set_selections(vec![caret(0, 4)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "alpha");
assert_eq!(b.selections(), &[caret(0, 2)]);
}
#[test]
fn toggle_bullet_converts_ordered_marker_to_dash() {
let mut b = Buffer::from_text("1. alpha");
b.set_selections(vec![caret(0, 5)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- alpha");
assert_eq!(b.selections(), &[caret(0, 4)]);
}
#[test]
fn toggle_bullet_preserves_caret_across_multibyte_content() {
let mut b = Buffer::from_text("café");
b.set_selections(vec![caret(0, 5)]);
run(&mut b, SelectionEdit::MarkdownToggleBullet);
assert_eq!(b.rope().to_string(), "- café");
assert_eq!(b.selections(), &[caret(0, 7)]);
}
#[test]
fn toggle_task_multiline_skips_blank_lines() {
let mut b = Buffer::from_text("a\n\nb");
b.set_selections(vec![span((0, 0), (2, 1))]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] a\n\n- [ ] b");
}
#[test]
fn toggle_numbered_multiline_skips_blank_lines_and_keeps_sequence() {
let mut b = Buffer::from_text("a\n\nb");
b.set_selections(vec![span((0, 0), (2, 1))]);
run(&mut b, SelectionEdit::MarkdownToggleNumbered);
assert_eq!(b.rope().to_string(), "1. a\n\n2. b");
}
#[test]
fn toggle_checkbox_multiline_skips_blank_lines() {
let mut b = Buffer::from_text("a\n\nb");
b.set_selections(vec![span((0, 0), (2, 1))]);
run(&mut b, SelectionEdit::MarkdownToggleCheckbox);
assert_eq!(b.rope().to_string(), "[ ] a\n\n[ ] b");
}
#[test]
fn toggle_numbered_inserts_one_dot_space() {
let mut b = Buffer::from_text("a\nb");
b.set_selections(vec![span((0, 0), (1, 1))]);
run(&mut b, SelectionEdit::MarkdownToggleNumbered);
assert_eq!(b.rope().to_string(), "1. a\n2. b");
}
#[test]
fn toggle_checkbox_cycles() {
let mut b = Buffer::from_text("a");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleCheckbox);
assert_eq!(b.rope().to_string(), "[ ] a");
run(&mut b, SelectionEdit::MarkdownToggleCheckbox);
assert_eq!(b.rope().to_string(), "[x] a");
}
#[test]
fn toggle_checkbox_flips_bulleted_task_without_duplicating() {
let mut b = Buffer::from_text("- [ ] task");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleCheckbox);
assert_eq!(b.rope().to_string(), "- [x] task");
run(&mut b, SelectionEdit::MarkdownToggleCheckbox);
assert_eq!(b.rope().to_string(), "- [ ] task");
}
#[test]
fn toggle_task_creates_then_removes() {
let mut b = Buffer::from_text("buy milk");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] buy milk");
assert_eq!(b.selections(), &[caret(0, 6)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "buy milk");
assert_eq!(b.selections(), &[caret(0, 0)]);
}
#[test]
fn toggle_task_preserves_caret_on_the_same_content_character() {
let mut b = Buffer::from_text("buy milk");
b.set_selections(vec![caret(0, 4)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] buy milk");
assert_eq!(b.selections(), &[caret(0, 10)]);
}
#[test]
fn toggle_task_on_empty_line_places_caret_after_fresh_marker() {
let mut b = Buffer::from_text("");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] ");
assert_eq!(b.selections(), &[caret(0, 6)]);
}
#[test]
fn toggle_task_on_plain_bullet_preserves_content_relative_caret() {
let mut b = Buffer::from_text("- buy milk");
b.set_selections(vec![caret(0, 6)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] buy milk");
assert_eq!(b.selections(), &[caret(0, 10)]);
}
#[test]
fn toggle_task_on_plain_bullet_keeps_marker() {
let mut b = Buffer::from_text("- buy milk");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownToggleTask);
assert_eq!(b.rope().to_string(), "- [ ] buy milk");
assert_eq!(b.selections(), &[caret(0, 0)]);
}
#[test]
fn cycle_list_marker_advances() {
let mut b = Buffer::from_text("- a");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownCycleListMarker);
assert_eq!(b.rope().to_string(), "* a");
}
#[test]
fn wrap_in_blockquote_prefixes() {
let mut b = Buffer::from_text("a");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownWrapInBlockquote);
assert_eq!(b.rope().to_string(), "> a");
}
#[test]
fn insert_code_fence_wraps_selection() {
let mut b = Buffer::from_text("x");
b.set_selections(vec![span((0, 0), (0, 1))]);
run(&mut b, SelectionEdit::MarkdownInsertCodeFence);
assert_eq!(b.rope().to_string(), "```\nx\n```");
}
#[test]
fn insert_link_wraps_selection() {
let mut b = Buffer::from_text("hi");
b.set_selections(vec![span((0, 0), (0, 2))]);
run(&mut b, SelectionEdit::MarkdownInsertLink);
assert_eq!(b.rope().to_string(), "[hi]()");
}
#[test]
fn insert_link_with_url_selection_fills_target() {
let mut b = Buffer::from_text("www.google.com");
b.set_selections(vec![span((0, 0), (0, 14))]);
run(&mut b, SelectionEdit::MarkdownInsertLink);
assert_eq!(b.rope().to_string(), "[](www.google.com)");
}
#[test]
fn insert_link_with_no_selection_inserts_empty_link() {
let mut b = Buffer::from_text("");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownInsertLink);
assert_eq!(b.rope().to_string(), "[]()");
}
#[test]
fn insert_image_ref_at_caret() {
let mut b = Buffer::from_text("");
b.set_selections(vec![caret(0, 0)]);
run(&mut b, SelectionEdit::MarkdownInsertImageRef);
assert_eq!(b.rope().to_string(), "");
}