continuity-engine 0.2.36

Synchronous, storage-neutral Continuity editor engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Tests for storage-neutral line-structure planning.

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() {
    // Phase B9 — three behaviours in one test.
    let mut b = build("- foo", vec![at(0, 5)]);
    run(&mut b, SelectionEdit::InsertNewlineSmart);
    assert_eq!(b.rope().to_string(), "- foo\n- ");
    // Item 15 — smart-newline now renumbers the ordered run starting at 1
    // (see `enter_renumbers_mis_numbered_ordered_run`), so a lone `3. foo`
    // is corrected to `1. foo` and the continuation becomes `2. `.
    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() {
    // Caret at column 3 of `hello` → after toggle, line becomes
    // `- hello` and caret sits at column 5 (same content character).
    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() {
    // Caret at column 5 of `- hello` (the 'l' before final 'o') →
    // after toggle, line becomes `hello` and caret sits at column 3.
    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() {
    // Caret inside the leading whitespace itself shouldn't shift.
    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() {
    // Selection spans three plain lines → all three get `- `.
    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() {
    // Blank line and whitespace-only line inside the selection must
    // not receive `- ` markers.
    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() {
    // Every non-blank line is bulleted; the blank gap must not force
    // the toggle into add mode — it should strip.
    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() {
    // A caret on one empty line keeps the old behavior: start a 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() {
    // Mixed state: lines 0 and 2 lack bullets, line 1 already has one.
    // Result: bullets added to 0 and 2; line 1 left untouched.
    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() {
    // Every line already bulleted → toggle strips them 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() {
    // A blank-line separator loses exactly one newline per press;
    // the sections stay on their own lines until pressed again.
    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");
    // The selection now covers the rebuilt block, so pressing again
    // finishes the join.
    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() {
    // `-no-space` and `--flag` are ordinary text, not list markers.
    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");
}

// --- Item 22: Ctrl+R on an ordered line converts it to a bullet ---

#[test]
fn toggle_bullet_on_ordered_line_converts_to_dash() {
    // First press: `1. foo` → `- foo` (ordered → bullet, not `- 1. foo`).
    let mut b = build("1. foo", vec![at(0, 3)]);
    run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
    assert_eq!(b.rope().to_string(), "- foo");
    // Second press: `- foo` → `foo` (bullet → plain).
    run(&mut b, SelectionEdit::ToggleBulletAtLineStart);
    assert_eq!(b.rope().to_string(), "foo");
}

#[test]
fn toggle_bullet_ordered_paren_marker_converts_to_dash() {
    // `2) foo` (paren-style ordered marker) also converts to `- foo`.
    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() {
    // `12. foo`: caret at col 4 (the 'f'). After ordered→bullet the prefix
    // shrinks from 4 to 2, so the caret column drops by 2 to col 2.
    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() {
    // All-ordered run: first press converts each to a dash bullet (not
    // stripped), so a second press strips them — ordered→bullet→plain.
    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() {
    // Not all dash-bulleted → make-bullets: ordered converts, plain gains.
    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");
}

// --- Item 23: Ctrl+Shift+R toggle bullet + continuation indent ---

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() {
    // Round-trip: applying it twice returns to the original text.
    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");
    // Reselect the now-bulleted block (lines 0..2) and toggle off.
    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() {
    // A single covered line never indents — identical to Ctrl+R.
    let mut b = build("hello", vec![at(0, 0)]);
    run(&mut b, continuation_edit());
    assert_eq!(b.rope().to_string(), "- hello");
}

// --- Item 14: Enter on a task line continues with an unchecked box ---

#[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(), "");
}

// --- Item 15: auto-renumber on Enter inside an ordered list ---

#[test]
fn enter_inside_ordered_list_renumbers_following_items() {
    // Caret at end of `2.` in `1.\n2.\n3.` → new item is `3.`, the old
    // `3.` becomes `4.`.
    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");
    // Caret lands on the new (third) line after the `3. ` marker.
    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() {
    // A mis-numbered run `1.\n5.` → Enter after `5.` produces a clean
    // `1.\n2.\n3.` because the whole run is renumbered in one group.
    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. ");
}