plannotator-tui 0.12.1

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! The mouse: the wheel, clicks on lines, files, notes and buttons, and a drag that selects
//! lines for a comment, in the diff and in the whole file. Every position is found in the
//! frame the screen drew, as a terminal's click would land on it.

use plannotator_tui_review::Side;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{KeyCode, MouseButton, MouseEvent, MouseEventKind};
use ratatui::style::Modifier;

use super::whole_flow::settle_whole;
use super::*;
use crate::review::diff::Lines;
use crate::review::{Draft, Focus, Mode};
use crate::ui::drag::STEP;

pub(super) const COLS: u16 = 120;
pub(super) const ROWS: u16 = 40;

pub(super) fn draw(screen: &mut Screen) -> Buffer {
    let mut terminal = Terminal::new(TestBackend::new(COLS, ROWS)).expect("terminal");
    let mut drawn = Ok(());
    terminal.draw(|f| drawn = screen.draw(f)).expect("draw");
    drawn.expect("viewer draw");
    screen.after_draw();
    terminal.backend().buffer().clone()
}

/// The cell where `needle` starts in a fresh frame: the leftmost match, or with `last` the
/// rightmost (the new side of a split diff).
pub(super) fn locate(screen: &mut Screen, needle: &str, last: bool) -> (u16, u16) {
    let buffer = draw(screen);
    let mut found = Vec::new();
    for y in 0..ROWS {
        let cells: Vec<String> = (0..COLS).map(|x| buffer[(x, y)].symbol().to_owned()).collect();
        let row = cells.concat();
        for (byte, _) in row.match_indices(needle) {
            let mut width = 0;
            let mut x = 0u16;
            while width < byte {
                width += cells[usize::from(x)].len();
                x += 1;
            }
            found.push((x, y));
        }
    }
    let at = if last { found.last() } else { found.first() };
    *at.unwrap_or_else(|| panic!("no {needle:?} on screen"))
}

pub(super) fn mouse(screen: &mut Screen, kind: MouseEventKind, (column, row): (u16, u16)) {
    let event = MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE };
    screen.handle_event(Event::Mouse(event)).expect("mouse");
}

/// Press at `from` and move, a row at a time, to `to`, without letting go.
pub(super) fn drag(screen: &mut Screen, from: (u16, u16), to: (u16, u16)) {
    mouse(screen, MouseEventKind::Down(MouseButton::Left), from);
    let rows: Vec<u16> =
        if to.1 >= from.1 { (from.1..=to.1).collect() } else { (to.1..=from.1).rev().collect() };
    for row in rows {
        mouse(screen, MouseEventKind::Drag(MouseButton::Left), (to.0, row));
    }
}

/// Hold a drag where it is for `steps` of the timer that scrolls past the pane's edge, with a
/// frame after each, as the event loop draws after a tick.
pub(super) fn hold(screen: &mut Screen, steps: u32) {
    let start = Instant::now();
    for step in 0..steps {
        screen.step_at_edge(start + STEP * step).expect("step");
        let _ = draw(screen);
    }
}

pub(super) fn release(screen: &mut Screen, at: (u16, u16)) {
    mouse(screen, MouseEventKind::Up(MouseButton::Left), at);
}

pub(super) fn lines(side: Side, first: u32, last: u32) -> Lines {
    Lines { path: "app.py".into(), side, first, last }
}

fn selection(screen: &Screen) -> Option<Lines> {
    screen.diff.selection()
}

#[test]
fn a_drag_selects_whole_lines_on_its_side_and_the_comment_lands_on_them() {
    let fixture = Fixture::new("mouse-drag");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    drag(&mut screen, (from.0 + 3, from.1), to);
    release(&mut screen, to);
    assert_eq!(selection(&screen), Some(lines(Side::New, 8, 10)));
    press(&mut screen, KeyCode::Char('c'));
    assert_eq!(screen.mode, Mode::Compose(Draft::Comment(lines(Side::New, 8, 10))));
    type_and_save(&mut screen, "split this up");
    let note = screen.store.annotations().first().expect("a note");
    assert_eq!((note.line_start, note.line_end, note.side), (8, 10, Side::New));
    assert_eq!(note.file_path, "app.py");
}

#[test]
fn a_drag_that_starts_on_the_old_side_stays_on_it() {
    let fixture = Fixture::new("mouse-old");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "def add(a, b):", false);
    let to = locate(&mut screen, "return a + b", false);
    // The pointer wanders to the new side; the lines stay old ones.
    drag(&mut screen, from, (COLS - 10, to.1));
    release(&mut screen, to);
    assert_eq!(selection(&screen), Some(lines(Side::Old, 5, 6)));
}

#[test]
fn dragging_up_selects_from_the_pointer_down_to_the_pressed_line() {
    let fixture = Fixture::new("mouse-up");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "return total", true);
    let to = locate(&mut screen, "def add(a, b):", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    assert_eq!(selection(&screen), Some(lines(Side::New, 8, 10)));
}

#[test]
fn a_click_moves_the_cursor_and_picks_the_side_without_selecting() {
    let fixture = Fixture::new("mouse-click");
    let mut screen = fixture.open();
    let new = locate(&mut screen, "return total", true);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), new);
    release(&mut screen, new);
    assert_eq!(screen.diff.cursor().expect("cursor"), Some(lines(Side::New, 10, 10)));
    assert_eq!(selection(&screen), None);
    let old = locate(&mut screen, "return a + b", false);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), old);
    release(&mut screen, old);
    assert_eq!(screen.diff.cursor().expect("cursor"), Some(lines(Side::Old, 6, 6)));
}

#[test]
fn a_new_press_clears_the_last_selection() {
    let fixture = Fixture::new("mouse-reselect");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), from);
    release(&mut screen, from);
    assert_eq!(selection(&screen), None);
}

/// The new-side pane of the row holding `needle`: from the cell after the split's middle
/// line to the frame's edge.
fn new_pane(buffer: &Buffer, (x, y): (u16, u16)) -> std::ops::Range<u16> {
    let middle = (0..x).rev().find(|&c| buffer[(c, y)].symbol() == "\u{2502}").expect("a split row");
    middle + 1..COLS
}

/// Each of `rows` is one block of the selection colour on its new side, edge to edge: no
/// bar in the gutter, no underline, and the old side and the middle line left alone.
fn assert_selected_block(buffer: &Buffer, rows: &[(u16, u16)]) {
    let tint = crate::review::mouse::selection_bg();
    for &at in rows {
        let y = at.1;
        let pane = new_pane(buffer, at);
        assert_eq!(buffer[(pane.start, y)].symbol(), " ", "no bar on row {y}");
        for x in pane.clone() {
            let cell = &buffer[(x, y)];
            assert_eq!(cell.bg, tint, "row {y} column {x} is filled");
            assert!(!cell.modifier.contains(Modifier::UNDERLINED), "row {y} column {x} has no underline");
        }
        assert_ne!(buffer[(pane.start - 1, y)].bg, tint, "the middle line of row {y}");
        assert_ne!(buffer[(pane.start - 2, y)].bg, tint, "the old side of row {y}");
    }
}

#[test]
fn a_selection_is_one_block_of_colour_while_dragging_after_the_release_and_with_v() {
    let fixture = Fixture::new("mouse-tint");
    let mut screen = fixture.open();
    let rows = ["def add(a, b):", "total = a + b", "return total"];
    let marks: Vec<(u16, u16)> = rows.iter().map(|r| locate(&mut screen, r, true)).collect();
    let outside = locate(&mut screen, "def main():", true);
    let tint = crate::review::mouse::selection_bg();

    drag(&mut screen, marks[0], marks[2]);
    let buffer = draw(&mut screen);
    assert_selected_block(&buffer, &marks);
    assert_ne!(buffer[(outside.0, outside.1)].bg, tint);

    release(&mut screen, marks[2]);
    assert_selected_block(&draw(&mut screen), &marks);

    // The keyboard's selection looks the same, the cursor's row included.
    press(&mut screen, KeyCode::Esc);
    assert_eq!(selection(&screen), None);
    at(&mut screen, 8);
    keys(&mut screen, "vjj");
    assert_eq!(selection(&screen), Some(lines(Side::New, 8, 10)));
    let buffer = draw(&mut screen);
    assert_selected_block(&buffer, &marks);
    assert_ne!(buffer[(outside.0, outside.1)].bg, tint);
}

#[test]
fn in_the_unified_view_a_removed_line_inside_new_lines_is_shaded_but_not_selected() {
    let fixture = Fixture::new("mouse-unified");
    let mut screen = fixture.open();
    press(&mut screen, KeyCode::Char('u'));
    let tint = crate::review::mouse::selection_bg();
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    let removed = locate(&mut screen, "return a + b", true);
    assert!(from.1 < removed.1 && removed.1 < to.1, "the removed line sits among the new ones");
    let shaded = |buffer: &Buffer, (x, y): (u16, u16)| buffer[(x, y)].bg == tint;
    let pane = screen.geometry.diff;
    let check = |buffer: &Buffer| {
        for at in [from, removed, to] {
            assert!(shaded(buffer, at), "row {} is shaded", at.1);
        }
        // The whole row, from the pane's first column to its last.
        assert!(shaded(buffer, (pane.x, removed.1)) && shaded(buffer, (pane.right() - 1, removed.1)));
        // Nothing above the first selected row or below the last.
        assert!(!shaded(buffer, (from.0, from.1 - 1)) && !shaded(buffer, (to.0, to.1 + 1)));
    };

    drag(&mut screen, from, to);
    check(&draw(&mut screen));
    release(&mut screen, to);
    check(&draw(&mut screen));

    // The keyboard's selection looks the same.
    press(&mut screen, KeyCode::Esc);
    at(&mut screen, 8);
    // In the unified view `j` steps over the removed row too.
    keys(&mut screen, "vjjj");
    assert_eq!(selection(&screen), Some(lines(Side::New, 8, 10)));
    check(&draw(&mut screen));

    // Only the look: the note is on new lines 8-10 and quotes them alone.
    press(&mut screen, KeyCode::Char('c'));
    type_and_save(&mut screen, "split this up");
    let note = screen.store.annotations().first().expect("a note");
    assert_eq!((note.line_start, note.line_end, note.side), (8, 10, Side::New));
    assert_eq!(note.anchor_text.as_deref(), Some("def add(a, b):\n    total = a + b\n    return total"));
}

#[test]
fn the_toolbar_after_a_drag_comments_on_the_selection() {
    let fixture = Fixture::new("mouse-toolbar");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    let comment = locate(&mut screen, "comment (c)", false);
    assert_eq!(comment.1, to.1 + 1, "the toolbar sits under the release");
    // The chip colours, whose contrast `ui::chips` checks.
    let buffer = draw(&mut screen);
    let chips = crate::ui::chips::chips();
    let suggest = locate(&mut screen, "suggest (s)", false);
    for (at, fg) in [(comment, chips.review_comment()), (suggest, chips.review_suggest)] {
        let cell = &buffer[at];
        assert_eq!((cell.fg, cell.bg), (fg, crate::theme::palette().toolbar_bg), "{at:?}");
    }
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), comment);
    assert_eq!(screen.mode, Mode::Compose(Draft::Comment(lines(Side::New, 8, 10))));
}

#[test]
fn a_key_takes_the_toolbar_away_and_keeps_the_selection() {
    let fixture = Fixture::new("mouse-toolbar-key");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    assert!(frame(&mut screen, COLS, ROWS).iter().any(|r| r.contains("comment (c)")));
    press(&mut screen, KeyCode::Char('j'));
    assert!(!frame(&mut screen, COLS, ROWS).iter().any(|r| r.contains("comment (c)")));
    assert_eq!(selection(&screen), Some(lines(Side::New, 8, 10)));
}

#[test]
fn y_copies_the_selected_code_when_copy_on_select_is_off() {
    let fixture = Fixture::new("mouse-copy");
    let mut screen = fixture.open();
    screen.set_copy_on_select(false);
    let from = locate(&mut screen, "def add(a, b):", true);
    let to = locate(&mut screen, "return total", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    assert!(row_with(&frame(&mut screen, COLS, ROWS), "esc clear").contains("y copy"));
    let picked = selection(&screen).expect("selected");
    assert_eq!(
        screen.selected_code(&picked).as_deref(),
        Some("def add(a, b):\n    total = a + b\n    return total")
    );
    press(&mut screen, KeyCode::Char('y'));
    assert_eq!(screen.status.as_deref(), Some("copied 3 lines"));
}

/// The fixture with `app.py` long enough that its diff runs past one screen.
pub(super) fn long_fixture(tag: &str) -> Fixture {
    let fixture = Fixture::new(tag);
    let tail = (1..=60).map(|i| format!("v{i} = {i}")).collect::<Vec<_>>().join("\n");
    fixture.write("app.py", &format!("{AFTER}{tail}\n"));
    fixture
}

#[test]
fn dragging_past_the_bottom_scrolls_and_keeps_selecting() {
    let fixture = long_fixture("mouse-scroll");
    let mut screen = fixture.open();
    let from = locate(&mut screen, "import sys", true);
    let bottom = screen.geometry.diff.bottom();
    drag(&mut screen, from, (from.0, bottom - 1));
    let on_screen = screen.selected_lines().expect("dragging").last;
    mouse(&mut screen, MouseEventKind::Drag(MouseButton::Left), (from.0, bottom));
    hold(&mut screen, 10);
    release(&mut screen, (from.0, bottom));
    let picked = selection(&screen).expect("selected");
    assert_eq!(picked.first, 1);
    assert!(picked.last > on_screen + 5, "{} grew past {on_screen}", picked.last);
}

#[test]
fn a_drag_in_the_whole_file_selects_its_lines_for_a_comment() {
    let fixture = Fixture::new("mouse-whole");
    let mut screen = fixture.open();
    at(&mut screen, 1);
    press(&mut screen, KeyCode::Char('o'));
    settle_whole(&mut screen);
    let from = locate(&mut screen, "def greet(name):", true);
    let to = locate(&mut screen, "print(\"hello,\", name)", true);
    drag(&mut screen, from, to);
    release(&mut screen, to);
    // Drawn as in the diff: a block of colour with no bar and no underline.
    let buffer = draw(&mut screen);
    for (x, y) in [from, to] {
        let row: String = (0..COLS).map(|c| buffer[(c, y)].symbol()).collect();
        assert!(!row.contains(['\u{2503}', '\u{258e}']), "no bar: {row}");
        let cell = &buffer[(x, y)];
        assert_eq!(cell.bg, crate::review::mouse::selection_bg(), "{row}");
        assert!(!cell.modifier.contains(Modifier::UNDERLINED), "{row}");
    }
    let whole = screen.whole.as_ref().expect("still open");
    assert_eq!(whole.target().expect("target"), Some((4, 5)));
    press(&mut screen, KeyCode::Char('c'));
    assert_eq!(screen.mode, Mode::Compose(Draft::Comment(lines(Side::New, 4, 5))));
}

#[test]
fn the_wheel_scrolls_the_diff_under_the_pointer() {
    let fixture = long_fixture("mouse-wheel");
    let mut screen = fixture.open();
    let first = locate(&mut screen, "import sys", true);
    mouse(&mut screen, MouseEventKind::ScrollDown, first);
    assert!(!frame(&mut screen, COLS, ROWS).iter().any(|r| r.contains("import sys")));
    mouse(&mut screen, MouseEventKind::ScrollUp, first);
    assert!(frame(&mut screen, COLS, ROWS).iter().any(|r| r.contains("import sys")));
}

#[test]
fn a_click_in_the_file_list_opens_the_file_and_starts_no_selection() {
    let fixture = Fixture::new("mouse-files");
    fixture.write("notes.txt", "changed\n");
    let mut screen = fixture.open();
    draw(&mut screen);
    assert_eq!(screen.files.current.as_deref(), Some("app.py"));
    // The list's own row, not the diff's heading for the file.
    let y = screen.geometry.file_rows.iter().find(|(_, key)| key == "notes.txt").expect("a list row").0;
    let row = (screen.geometry.files.x + 2, y);
    drag(&mut screen, row, (row.0, row.1 + 3));
    release(&mut screen, (row.0, row.1 + 3));
    assert_eq!(selection(&screen), None);
    assert_eq!(screen.files.current.as_deref(), Some("notes.txt"));
    assert_eq!(screen.focus, Focus::Files);
}

#[test]
fn a_click_on_a_note_selects_it_and_shows_its_line() {
    let fixture = Fixture::new("mouse-note");
    let mut screen = fixture.open();
    at(&mut screen, 9);
    press(&mut screen, KeyCode::Char('c'));
    type_and_save(&mut screen, "first");
    at(&mut screen, 14);
    press(&mut screen, KeyCode::Char('c'));
    type_and_save(&mut screen, "second");
    open_notes(&mut screen);
    at(&mut screen, 1);
    let bubble = locate(&mut screen, "first", true);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), bubble);
    assert_eq!(screen.focus, Focus::Notes);
    assert_eq!(screen.selected_note().map(|n| n.line_start), Some(9));
    assert_eq!(screen.diff.cursor().expect("cursor"), Some(lines(Side::New, 9, 9)));
}

#[test]
fn the_header_buttons_send_and_open_the_review_menu() {
    let fixture = Fixture::new("mouse-buttons");
    let mut screen = fixture.open();
    at(&mut screen, 9);
    press(&mut screen, KeyCode::Char('c'));
    type_and_save(&mut screen, "a note");
    let review = locate(&mut screen, "Review", false);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), review);
    assert_eq!(screen.mode, Mode::Menu(0));
    press(&mut screen, KeyCode::Esc);
    let send = locate(&mut screen, "Send 1 new", false);
    mouse(&mut screen, MouseEventKind::Down(MouseButton::Left), send);
    assert_eq!(fixture.sent().len(), 1);
}