project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use note_model::write::Block; |
| 2 | |
| 3 | use super::commands::{self as c, Command, Cx}; |
| 4 | use super::doc::Kind; |
| 5 | use super::history::History; |
| 6 | use super::marks::{MarkType, Marks}; |
| 7 | use super::pos::Selection; |
| 8 | use super::testutil::*; |
| 9 | |
| 10 | const BACKSPACE: &[Command] = &[c::delete_selection, c::join_backward, c::select_node_backward]; |
| 11 | const ENTER: &[Command] = &[c::split_list_item, c::newline_in_code, c::create_paragraph_near, c::lift_empty_block, c::split_block]; |
| 12 | |
| 13 | fn run(m: &super::model::Model, sel: Selection, cmds: &[Command]) -> (bool, Selection) { |
| 14 | let mut cx = Cx::new(m, sel); |
| 15 | let ran = c::chain(&mut cx, cmds, true); |
| 16 | (ran, cx.sel) |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn insert_text_keeps_marks_exact() { |
| 21 | let m = build(vec![pi(vec![t("ab"), strong("cd")])]); |
| 22 | // At the end of a strong run with plain marks requested: the new text is plain. |
| 23 | m.insert(pos(&m, 0, 4), "x", &Marks::none()); |
| 24 | // Inside the plain run, but asking for strong. |
| 25 | m.insert(pos(&m, 0, 1), "y", &Marks { strong: true, ..Marks::none() }); |
| 26 | assert_eq!(dump(&m), "p(a{s:y}b{s:cd}x)"); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn delete_range_across_blocks_and_lists() { |
| 31 | let m = build(vec![p("one"), ul(vec![vec![p("two")], vec![p("three")]]), p("four")]); |
| 32 | c::delete_range(&m, pos(&m, 0, 2), pos(&m, 2, 2)); |
| 33 | assert_eq!(dump(&m), "p(onree) p(four)"); |
| 34 | |
| 35 | let m = build(vec![quote(vec![p("a"), p("b")]), p("c")]); |
| 36 | c::delete_range(&m, pos(&m, 1, 1), pos(&m, 2, 0)); |
| 37 | assert_eq!(dump(&m), "quote(p(a) p(bc))"); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn split_heading() { |
| 42 | let m = build(vec![h(2, "title")]); |
| 43 | run(&m, at(&m, 0, 5), ENTER); |
| 44 | run(&m, at(&m, 0, 2), ENTER); |
| 45 | assert_eq!(dump(&m), "h2(ti) h2(tle) p()"); |
| 46 | run(&m, at(&m, 0, 0), ENTER); |
| 47 | assert_eq!(dump(&m), "p() h2(ti) h2(tle) p()"); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn code_block_merges_lines_and_splits_back() { |
| 52 | let m = build(vec![p("let a"), pi(vec![t(" b"), br(), strong("c")]), p("d")]); |
| 53 | let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 0), head: pos(&m, 1, 1) }); |
| 54 | assert!(c::toggle_block(&mut cx, Kind::CodeBlock, true)); |
| 55 | assert_eq!(dump(&m), "code(let a\n b\nc) p(d)"); |
| 56 | let mut cx = Cx::new(&m, at(&m, 0, 2)); |
| 57 | assert!(c::toggle_block(&mut cx, Kind::CodeBlock, true)); |
| 58 | assert_eq!(dump(&m), "p(let a) p( b) p(c) p(d)"); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn backspace() { |
| 63 | let m = build(vec![p("a"), p("b")]); |
| 64 | assert!(!run(&m, at(&m, 0, 0), BACKSPACE).0); |
| 65 | |
| 66 | let m = build(vec![p("a"), hr(), p("b")]); |
| 67 | let (_, sel) = run(&m, at(&m, 1, 0), BACKSPACE); |
| 68 | assert_eq!(dump(&m), "p(a) p(b)"); |
| 69 | let (_, sel) = run(&m, sel, BACKSPACE); |
| 70 | assert_eq!(dump(&m), "p(ab)"); |
| 71 | assert_eq!(sel, at(&m, 0, 1)); |
| 72 | |
| 73 | let m = build(vec![ul(vec![vec![p("a")], vec![p("b")]])]); |
| 74 | run(&m, at(&m, 1, 0), BACKSPACE); |
| 75 | assert_eq!(dump(&m), "ul(li(p(a) p(b)))"); |
| 76 | let m = build(vec![ul(vec![vec![p("a")], vec![p("b")]])]); |
| 77 | run(&m, at(&m, 0, 0), BACKSPACE); |
| 78 | assert_eq!(dump(&m), "p(a) ul(li(p(b)))"); |
| 79 | |
| 80 | let m = build(vec![p("x"), quote(vec![p("a"), p("b")])]); |
| 81 | run(&m, at(&m, 1, 0), BACKSPACE); |
| 82 | assert_eq!(dump(&m), "p(x) p(a) quote(p(b))"); |
| 83 | |
| 84 | // After a list: the paragraph moves into its last item. |
| 85 | let m = build(vec![ul(vec![vec![p("a")]]), p("b")]); |
| 86 | run(&m, at(&m, 1, 0), BACKSPACE); |
| 87 | assert_eq!(dump(&m), "ul(li(p(a) p(b)))"); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn enter_in_lists() { |
| 92 | let m = build(vec![ul(vec![vec![p("ab")]])]); |
| 93 | let (_, sel) = run(&m, at(&m, 0, 1), ENTER); |
| 94 | assert_eq!(dump(&m), "ul(li(p(a)) li(p(b)))"); |
| 95 | assert_eq!(sel, at(&m, 1, 0)); |
| 96 | // Enter at the end makes an empty item; Enter again lifts it out. |
| 97 | let (_, sel) = run(&m, at(&m, 1, 1), ENTER); |
| 98 | assert_eq!(dump(&m), "ul(li(p(a)) li(p(b)) li(p()))"); |
| 99 | run(&m, sel, ENTER); |
| 100 | assert_eq!(dump(&m), "ul(li(p(a)) li(p(b))) p()"); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn list_items_lift_and_sink() { |
| 105 | let m = build(vec![ul(vec![vec![p("a"), ul(vec![vec![p("b")], vec![p("c")]])]])]); |
| 106 | let mut cx = Cx::new(&m, at(&m, 1, 1)); |
| 107 | assert!(c::lift_list_item(&mut cx, true)); |
| 108 | assert_eq!(dump(&m), "ul(li(p(a)) li(p(b) ul(li(p(c)))))"); |
| 109 | let mut cx = Cx::new(&m, at(&m, 2, 0)); |
| 110 | assert!(!c::sink_list_item(&mut cx, false), "c's item has no previous sibling"); |
| 111 | let mut cx = Cx::new(&m, at(&m, 1, 0)); |
| 112 | assert!(c::sink_list_item(&mut cx, true)); |
| 113 | assert_eq!(dump(&m), "ul(li(p(a) ul(li(p(b) ul(li(p(c)))))))"); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn toggle_list_quote_and_mark() { |
| 118 | let m = build(vec![p("one"), p("two")]); |
| 119 | let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 1), head: pos(&m, 1, 2) }); |
| 120 | assert!(c::toggle_list(&mut cx, Kind::BulletList, true)); |
| 121 | assert_eq!(dump(&m), "ul(li(p(one)) li(p(two)))"); |
| 122 | assert!(c::toggle_list(&mut cx, Kind::OrderedList, true)); |
| 123 | assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); |
| 124 | assert!(c::toggle_quote(&mut cx, true)); |
| 125 | assert_eq!(dump(&m), "quote(ol(li(p(one)) li(p(two))))"); |
| 126 | assert!(c::toggle_quote(&mut cx, true)); |
| 127 | assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); |
| 128 | let mut cx = Cx::new(&m, Selection::Text { anchor: pos(&m, 0, 0), head: pos(&m, 1, 3) }); |
| 129 | c::toggle_mark(&mut cx, MarkType::Strong, true); |
| 130 | assert_eq!(dump(&m), "ol(li(p({s:one})) li(p({s:two})))"); |
| 131 | c::toggle_mark(&mut cx, MarkType::Strong, true); |
| 132 | assert_eq!(dump(&m), "ol(li(p(one)) li(p(two)))"); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn paste_and_copy() { |
| 137 | use note_model::write::Inline; |
| 138 | let m = build(vec![p("abcd")]); |
| 139 | let mut cx = Cx::new(&m, at(&m, 0, 2)); |
| 140 | let blocks = [Block::Paragraph(vec![t("X")]), Block::Rule, Block::Paragraph(vec![t("Y")])]; |
| 141 | assert!(c::insert_blocks(&mut cx, &blocks)); |
| 142 | assert_eq!(dump(&m), "p(abX) hr p(Ycd)"); |
| 143 | assert_eq!(cx.sel, at(&m, 1, 1)); |
| 144 | |
| 145 | let src = build(vec![p("one"), ul(vec![vec![p("two")]]), p("three")]); |
| 146 | let blocks = c::selected_blocks(&src, &Selection::Text { anchor: pos(&src, 0, 1), head: pos(&src, 2, 2) }); |
| 147 | let dst = build(vec![p("[]")]); |
| 148 | let mut cx = Cx::new(&dst, at(&dst, 0, 1)); |
| 149 | c::insert_blocks(&mut cx, &blocks); |
| 150 | assert_eq!(dump(&dst), "p([ne) ul(li(p(two))) p(th])"); |
| 151 | let _ = Inline::HardBreak; |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn undo_redo() { |
| 156 | let m = build(vec![p("ab"), ul(vec![vec![pi(vec![strong("x")])], vec![p("y")]])]); |
| 157 | let before = dump(&m); |
| 158 | let mut h = History::default(); |
| 159 | let edit = |h: &mut History, sel: Selection, f: &dyn Fn(&mut Cx), now: f64| { |
| 160 | let mut cx = Cx::new(&m, sel.clone()); |
| 161 | f(&mut cx); |
| 162 | h.push(&m, &sel, &cx.sel, now); |
| 163 | }; |
| 164 | // Type, close the step, delete the typed char: undo restores it, then removes it. |
| 165 | edit(&mut h, at(&m, 0, 1), &|cx| assert!(c::insert_text(cx, "Q")), 0.0); |
| 166 | h.checkpoint(); |
| 167 | edit(&mut h, at(&m, 0, 2), &|cx| assert!(c::chain(cx, &[c::delete_selection, c::join_backward], true) || { cx.m.delete(cx.sel.cursor().unwrap() - 1, cx.sel.cursor().unwrap()); true }), 10.0); |
| 168 | assert_eq!(dump(&m), before); |
| 169 | h.undo(&m); |
| 170 | assert!(dump(&m).starts_with("p(aQb)")); |
| 171 | h.undo(&m); |
| 172 | assert_eq!(dump(&m), before); |
| 173 | h.redo(&m); |
| 174 | h.redo(&m); |
| 175 | assert_eq!(dump(&m), before); |
| 176 | // Select all, delete, undo: everything back with its marks and lists. |
| 177 | edit(&mut h, Selection::All, &|cx| assert!(c::delete_selection(cx, true)), 5000.0); |
| 178 | assert_eq!(dump(&m), "p()"); |
| 179 | assert_eq!(h.undo(&m), Some(Selection::All)); |
| 180 | assert_eq!(dump(&m), before); |
| 181 | } |
| 182 | |
| 183 | #[test] |
| 184 | fn anchor_survives_remote_insert_before() { |
| 185 | let m = build(vec![pi(vec![t("ab"), br(), strong("cd")])]); |
| 186 | let a = m.anchor(4); |
| 187 | let other = loro::LoroDoc::new(); |
| 188 | other.import(&m.doc.export(loro::ExportMode::Snapshot).unwrap()).unwrap(); |
| 189 | other.get_text("body").insert(0, "xy").unwrap(); |
| 190 | other.commit(); |
| 191 | m.import(&other.export(loro::ExportMode::updates(&m.doc.oplog_vv())).unwrap()).unwrap(); |
| 192 | assert_eq!(m.resolve(&a), 6); |
| 193 | assert_eq!(dump(&m), "p(xyab|{s:cd})"); |
| 194 | } |
| 195 | |
| 196 | /// A 5 MB paragraph: Enter and Backspace in its middle stay cheap. |
| 197 | #[test] |
| 198 | #[ignore] |
| 199 | fn huge_block_split_join() { |
| 200 | let big = "lorem ipsum ".repeat(450_000); |
| 201 | let m = build(vec![p(&big)]); |
| 202 | let mid = big.len() / 2; |
| 203 | let t0 = std::time::Instant::now(); |
| 204 | let (_, sel) = run(&m, Selection::caret(mid), ENTER); |
| 205 | let split = t0.elapsed(); |
| 206 | let t1 = std::time::Instant::now(); |
| 207 | run(&m, sel, BACKSPACE); |
| 208 | let join = t1.elapsed(); |
| 209 | eprintln!("split {split:?} join {join:?}"); |
| 210 | assert!(join.as_millis() < 50, "join {join:?}"); |
| 211 | assert_eq!(m.len(), 1); |
| 212 | } |