croft-software 0.2.232

VSCode-style TUI written in Rust
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! What each right-click menu holds (#2739), saved as text in
//! `golden/right-click-<spot>.txt`. Before the menus are opened from the
//! keyboard too, the code that finds the item under the pointer and builds
//! its menu is split in two; this ledger is what holds that split to
//! changing nothing a right-click shows or does.
//!
//! Each spot is right-clicked in an app drawn to show it, and the menu is
//! written down: where it opens, how it is drawn, the folder New File would
//! use, which pane then has the keys, and every row with what it runs.
//! Spots whose right-click also picks the item (an Explorer row, a capture,
//! a terminal pane) write what is picked after it. The workspace reads
//! `<ws>` and a commit id `<id>`, the two things that change from run to
//! run. `CROFT_BLESS=1` rewrites the files after a change meant to move a
//! menu.
//!
//! Linux only, like the screen goldens: on macOS the tab and Explorer
//! menus offer Reveal in Finder too, so its files would be others.
//!
//! The keyboard opens the same menus (Show Context Menu: Shift+F10, the
//! Menu key). Its tests below right-click an item, then press the key on
//! the same item, and want the same menu, opened just below the item.

use super::*;
use crossterm::event::{MouseButton, MouseEventKind};
use std::fmt::Write as _;

/// Every file the tests here save, for the check that no golden is orphaned.
pub(super) const RIGHT_CLICK_GOLDENS: [&str; 11] = [
    "right-click-minimap",
    "right-click-commit",
    "right-click-problem",
    "right-click-debug-row",
    "right-click-outline",
    "right-click-capture",
    "right-click-tab",
    "right-click-terminal",
    "right-click-explorer",
    "right-click-gutter",
    "right-click-editor",
];

const W: u16 = 120;
const H: u16 = 40;

fn right_click(app: &mut App, col: u16, row: u16) {
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Right), col, row));
}

fn pane_name(pane: Pane) -> &'static str {
    match pane {
        Pane::Tree => "Tree",
        Pane::Editor => "Editor",
        Pane::Terminal => "Terminal",
    }
}

fn rows(items: &[MenuEntry], depth: usize, out: &mut String) {
    let pad = "  ".repeat(depth);
    for entry in items {
        match entry {
            MenuEntry::Item { label, action } => {
                writeln!(out, "{pad}{label}  =>  {action:?}").unwrap();
            }
            MenuEntry::Submenu { label, items } => {
                writeln!(out, "{pad}{label} >").unwrap();
                rows(items, depth + 1, out);
            }
            MenuEntry::Separator => writeln!(out, "{pad}---").unwrap(),
            MenuEntry::Header(label) => writeln!(out, "{pad}[{label}]").unwrap(),
        }
    }
}

thread_local! {
    /// The menus [`written`] wrote on this thread, by spot, for the
    /// keyboard-route guard (`keyboard_routes.rs`) to read the actions of.
    static SEEN: std::cell::RefCell<Vec<(String, Vec<MenuEntry>)>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

/// The menus the scene `scene` opens, by spot, as it writes them.
pub(super) fn menus_seen_in(scene: fn()) -> Vec<(String, Vec<MenuEntry>)> {
    SEEN.with(|seen| seen.borrow_mut().clear());
    scene();
    SEEN.with(|seen| std::mem::take(&mut *seen.borrow_mut()))
}

/// The open menu (or that none opened) and where the keys went, as the
/// ledger writes them under the heading `spot`.
fn written(app: &App, spot: &str) -> String {
    let mut out = format!("== {spot}\n");
    match app.context_menu.as_ref() {
        None => out.push_str("no menu\n"),
        Some(menu) => {
            SEEN.with(|seen| {
                seen.borrow_mut()
                    .push((spot.to_string(), menu.items.clone()))
            });
            writeln!(
                out,
                "opens at {:?}, {:?}{}, row {} lit",
                menu.origin,
                menu.look,
                if menu.keyed { ", keyed" } else { "" },
                menu.selected,
            )
            .unwrap();
            writeln!(out, "New File goes in {}", menu.target_dir.display()).unwrap();
            rows(&menu.items, 1, &mut out);
        }
    }
    writeln!(out, "keys: {}", pane_name(app.focus)).unwrap();
    out
}

/// `text` as the ledger saves it: the workspace as `<ws>` (both spellings
/// of a temp folder behind a symlink) and every commit id as `<id>`.
fn saved(text: &str, root: &Path) -> String {
    let mut text = text.to_string();
    let canonical = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
    for r in [canonical.as_path(), root] {
        text = text.replace(&r.display().to_string(), "<ws>");
    }
    no_commit_ids(&text)
}

fn check(name: &str, text: &str, root: &Path) {
    super::golden::check_text(name, &saved(text, root));
}

/// The first row in `rows` of `area` that `hit` names, for aiming a click.
fn row_where(area: Rect, mut hit: impl FnMut(u16) -> bool) -> u16 {
    (area.y..area.y + area.height)
        .find(|&y| hit(y))
        .unwrap_or_else(|| panic!("no row in {area:?} answers"))
}

#[test]
pub(super) fn the_minimap_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("long.rs");
    let body: String = (0..300)
        .map(|i| format!("fn f{i}() {{ let value = {i}; }}\n"))
        .collect();
    std::fs::write(&path, body).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.inline_protocol = crate::iterm2_inline::InlineImageProtocol::None;
    app.cell_pixel = None;
    app.editor.open(&path).unwrap();
    draw(&mut app, 160, H);
    let strip = app.minimap.img_rect;
    assert!(strip.width > 0, "the minimap is drawn");
    right_click(&mut app, strip.x + 1, strip.y + 2);
    check(
        "right-click-minimap",
        &written(&app, "the minimap strip"),
        tmp.path(),
    );
}

#[test]
fn the_commit_menu() {
    let tmp = make_committed_repo();
    let p = tmp.path();
    std::fs::write(p.join("b.txt"), "b\n").unwrap();
    assert!(git_succeeds(p, &["add", "."]) && git_succeeds(p, &["commit", "-qm", "add b"]));
    let mut app = App::new(p.to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::SourceControl;
    app.show_tree = true;
    app.source_control.status.in_repo = true;
    app.commit_graph
        .set_rows(crate::git::layout_graph(crate::git::commit_graph(p, 100)));
    draw(&mut app, W, H);
    let area = app.commit_graph.last_area;
    let y = row_where(area, |y| {
        app.commit_graph
            .commit_at(y)
            .is_some_and(|c| c.summary == "add b")
    });
    let commit = app.commit_graph.commit_at(y).cloned().unwrap();
    right_click(&mut app, area.x + 4, y);
    // The commit's own ids, by value: `core.abbrev` can make the short one
    // as few as 4 digits, which `no_commit_ids` leaves alone.
    let text = written(&app, "a COMMITS row")
        .replace(&commit.hash, "<id>")
        .replace(&commit.short_hash, "<id>");
    check("right-click-commit", &text, p);
}

#[test]
fn the_problem_menu() {
    use crate::lsp::manager::DiagnosticSeverity;
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("hello.rs");
    std::fs::write(&file, "fn f0() {}\nfn f1() {}\nfn f2() {}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&file).unwrap();
    let mut by_server = std::collections::HashMap::new();
    by_server.insert(
        String::from("rustc"),
        vec![crate::lsp::manager::Diagnostic {
            start_line: 2,
            start_char: 3,
            end_line: 2,
            end_char: 5,
            severity: DiagnosticSeverity::Warning,
            message: String::from("function `f2` is never used"),
            tags: Default::default(),
            code: None,
            origin: None,
        }],
    );
    app.lsp.diagnostics.insert(file.clone(), by_server);
    app.rebuild_problems();
    app.bottom_panel_tab = BottomPanelTab::Problems;
    app.focus_pane(Pane::Editor);
    draw(&mut app, W, H);
    let list = app.problems.last_area;
    let y = row_where(list, |y| app.problems.diagnostic_at(y).is_some());
    right_click(&mut app, list.x + 30, y);
    check(
        "right-click-problem",
        &written(&app, "a PROBLEMS row"),
        tmp.path(),
    );
}

#[test]
fn the_debug_row_menus() {
    use crate::widgets::run_debug::{DebugRow, DebugRowKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::RunDebug;
    app.show_tree = true;
    let row = |kind| DebugRow { indent: 0, kind };
    let variable = |name: &str, evaluate: Option<&str>| DebugRowKind::Variable {
        reference: 0,
        expandable: false,
        expanded: false,
        name: name.into(),
        value: "42".into(),
        type_name: "int".into(),
        evaluate_name: evaluate.map(String::from),
    };
    let rows_shown = [
        (
            "the CALL STACK header",
            DebugRowKind::Header {
                title: "CALL STACK".into(),
            },
        ),
        (
            "a frame",
            DebugRowKind::Frame {
                id: 11,
                selected: true,
                name: "main".into(),
                location: "s.py:6".into(),
            },
        ),
        (
            "a scope",
            DebugRowKind::Scope {
                name: "Locals".into(),
                index: 0,
                open: None,
            },
        ),
        (
            "a variable with an expression",
            variable("count", Some("self.count")),
        ),
        ("a variable without one", variable("tmp", None)),
        (
            "a watch",
            DebugRowKind::Watch {
                index: 0,
                expression: "len(items)".into(),
                value: "3".into(),
                available: true,
                changed: false,
                pending: false,
            },
        ),
    ];
    app.run_debug.debug_active = true;
    app.run_debug.debug_rows = rows_shown.iter().map(|(_, k)| row(k.clone())).collect();
    let mut text = String::new();
    for (i, (spot, _)) in rows_shown.iter().enumerate() {
        app.context_menu = None;
        draw(&mut app, W, H);
        let area = app.run_debug.last_area;
        let y = row_where(area, |y| app.run_debug.debug_row_at(y) == Some(i));
        right_click(&mut app, area.x + 3, y);
        text.push_str(&written(&app, &format!("Run and Debug: {spot}")));
    }
    check("right-click-debug-row", &text, tmp.path());
}

#[test]
pub(super) fn the_outline_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("lib.rs");
    std::fs::write(&file, "fn a() {\n    1\n}\nfn b() {\n    2\n}").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&file).unwrap();
    if !app.explorer_views.is_visible(ExplorerView::Outline) {
        app.toggle_explorer_view(ExplorerView::Outline);
    }
    app.outline.collapsed = false;
    let symbols = app.compute_syntax_outline(&file);
    app.outline.set_symbols(file.clone(), symbols);
    draw(&mut app, W, H);
    let area = app.outline.last_area;
    let y = row_where(area, |y| {
        app.outline
            .row_at(y)
            .is_some_and(|i| app.outline.symbols()[i].name == "b")
    });
    right_click(&mut app, area.x + 2, y);
    check(
        "right-click-outline",
        &written(&app, "an OUTLINE row"),
        tmp.path(),
    );
}

#[test]
fn the_capture_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    for message in ["Build failed", "Test failed"] {
        app.captures.push(crate::widgets::captures::CapturedLine {
            pane: "zsh".into(),
            shell_pid: None,
            message: message.into(),
            line: format!("error: {message}"),
            anchor: None,
        });
    }
    app.bottom_panel_tab = BottomPanelTab::Captures;
    app.focus_pane(Pane::Editor);
    draw(&mut app, W, H);
    let area = app.captures.last_area;
    let (x, y) = (area.y..area.y + area.height)
        .flat_map(|y| (area.x..area.x + area.width).map(move |x| (x, y)))
        .find(|&(x, y)| app.captures.row_at(x, y) == Some(1))
        .expect("the second capture is drawn");
    right_click(&mut app, x, y);
    let mut text = written(&app, "the second CAPTURES row");
    let picked = app.captures.selected_entry().map(|c| c.message.clone());
    writeln!(text, "picked capture {picked:?}").unwrap();
    check("right-click-capture", &text, tmp.path());
}

#[test]
pub(super) fn the_tab_menus() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    for name in ["a.rs", "b.rs", "c.rs"] {
        let path = tmp.path().join(name);
        std::fs::write(&path, "fn main() {}\n").unwrap();
        app.editor.open_pinned(&path).unwrap();
    }
    let mut text = String::new();
    for (i, spot) in ["the first tab", "the middle tab", "the last tab"]
        .iter()
        .enumerate()
    {
        app.context_menu = None;
        app.focus_pane(Pane::Tree);
        draw(&mut app, W, H);
        let full = app.editor.last_full_area;
        let (x, y) = (full.y..full.y + 2)
            .flat_map(|y| (full.x..full.x + full.width).map(move |x| (x, y)))
            .find(|&(x, y)| app.editor.tab_at(x, y) == Some(i))
            .unwrap_or_else(|| panic!("{spot} is drawn"));
        right_click(&mut app, x, y);
        text.push_str(&written(&app, spot));
    }
    check("right-click-tab", &text, tmp.path());
}

#[test]
fn the_terminal_menus() {
    let (tmp, mut app, _term) = app_with_terminal_panes(1);
    let mut text = String::new();
    let aim = |app: &mut App, idx: usize, spot: &str, text: &mut String| {
        app.context_menu = None;
        app.focus_pane(Pane::Editor);
        draw(app, 160, 30);
        let area = app.terminals[idx].last_area;
        right_click(app, area.x + area.width / 2, area.y + area.height / 2);
        text.push_str(&written(app, spot));
        writeln!(text, "active pane {}", app.active_terminal).unwrap();
    };
    aim(&mut app, 0, "a lone terminal", &mut text);
    app.split_terminal().unwrap();
    app.split_terminal().unwrap();
    aim(&mut app, 1, "the middle of three terminals", &mut text);
    app.broadcast_input = true;
    aim(
        &mut app,
        1,
        "the middle of three terminals, broadcasting",
        &mut text,
    );
    app.broadcast_input = false;
    app.toggle_terminal_collapse(0);
    app.context_menu = None;
    draw(&mut app, 160, 30);
    let strip = app.panel_hits.collapsed_strips[0];
    right_click(&mut app, strip.x, strip.y + 1);
    text.push_str(&written(&app, "a folded terminal's strip"));
    writeln!(text, "active pane {}", app.active_terminal).unwrap();
    check("right-click-terminal", &text, tmp.path());
}

#[test]
fn the_explorer_menus() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path();
    std::fs::create_dir(root.join("src")).unwrap();
    std::fs::write(root.join("src/main.rs"), "fn main() {}\n").unwrap();
    std::fs::write(root.join("README.md"), "# hi\n").unwrap();
    std::fs::write(root.join("notes.txt"), "n\n").unwrap();
    let mut app = App::new(root.to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::Explorer;
    app.show_tree = true;
    let mut text = String::new();
    let picked = |app: &App, text: &mut String| {
        let name = |p: &Path| p.strip_prefix(root).unwrap_or(p).display().to_string();
        let mut marked: Vec<String> = app.tree.marked.iter().map(|p| name(p)).collect();
        marked.sort();
        let selected = app
            .tree
            .nodes
            .get(app.tree.selected)
            .map(|n| name(&n.path))
            .unwrap_or_default();
        writeln!(text, "selected {selected:?}, marked {marked:?}").unwrap();
    };
    let row_of = |app: &App, name: &str| {
        let i = app
            .tree
            .nodes
            .iter()
            .position(|n| n.path == root.join(name))
            .unwrap_or_else(|| panic!("{name} is in the tree"));
        row_where(app.tree.last_area, |y| app.tree.node_at_y(y) == Some(i))
    };
    for (spot, name) in [("a folder row", "src"), ("a file row", "README.md")] {
        app.context_menu = None;
        app.focus_pane(Pane::Editor);
        draw(&mut app, W, H);
        let (x, y) = (app.tree.last_area.x + 3, row_of(&app, name));
        right_click(&mut app, x, y);
        text.push_str(&written(&app, spot));
        picked(&app, &mut text);
    }
    // Two marked rows: a right-click on one of them keeps both.
    app.context_menu = None;
    app.tree.marked.clear();
    app.tree.marked.insert(root.join("README.md"));
    app.tree.marked.insert(root.join("notes.txt"));
    draw(&mut app, W, H);
    let (x, y) = (app.tree.last_area.x + 3, row_of(&app, "notes.txt"));
    right_click(&mut app, x, y);
    text.push_str(&written(&app, "one of two marked rows"));
    picked(&app, &mut text);
    // Below the last row: the workspace's own menu.
    app.context_menu = None;
    draw(&mut app, W, H);
    let area = app.tree.last_area;
    let y = row_where(area, |y| {
        y > area.y && app.tree.node_at_y(y).is_none() && app.tree.sticky_row_at(y).is_none()
    });
    right_click(&mut app, area.x + 3, y);
    text.push_str(&written(&app, "the empty space below the rows"));
    picked(&app, &mut text);
    check("right-click-explorer", &text, root);
}

#[test]
pub(super) fn the_gutter_menus() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("prog.py");
    std::fs::write(&f, "l1\nl2\nl3\nl4\nl5\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    app.focus_pane(Pane::Tree);
    draw(&mut app, W, H);
    let col = app.editor.last_inner.x;
    let row = app.editor.last_inner.y + 2;
    right_click(&mut app, col, row);
    let mut text = written(&app, "the gutter of line 3");
    app.context_menu = None;
    app.editor
        .breakpoints
        .entry(f.clone())
        .or_default()
        .insert(3);
    app.editor
        .breakpoint_conditions
        .entry(f.clone())
        .or_default()
        .insert(3, "x > 1".into());
    app.editor
        .breakpoint_logs
        .entry(f.clone())
        .or_default()
        .insert(3, "x is {x}".into());
    right_click(&mut app, col, row);
    text.push_str(&written(
        &app,
        "the gutter of line 3, with a conditional logpoint",
    ));
    check("right-click-gutter", &text, tmp.path());
}

#[test]
fn the_editor_menus() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("main.rs");
    std::fs::write(&f, "fn main() {\n    let total = 1;\n}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    app.focus_pane(Pane::Tree);
    draw(&mut app, W, H);
    let x = app.editor.last_inner.x + app.editor.last_gutter_width + 6;
    let y = app.editor.last_inner.y + 1;
    right_click(&mut app, x, y);
    let mut text = written(&app, "the text, nothing selected");
    app.context_menu = None;
    app.editor.selection = Some(crate::widgets::editor::EditorSelection {
        anchor: (1, 4),
        head: (1, 9),
    });
    right_click(&mut app, x, y);
    text.push_str(&written(&app, "the text, a word selected"));
    check("right-click-editor", &text, tmp.path());
}

/// `written` without where the menu opens, which a key and a click give
/// differently.
fn wherever(text: &str) -> String {
    text.lines()
        .map(|line| match line.strip_prefix("opens at ") {
            Some(rest) => format!("opens {}", rest.split_once("), ").map_or(rest, |(_, r)| r)),
            None => line.to_string(),
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn shift_f10(app: &mut App) {
    app.handle_key(key(KeyCode::F(10), KeyModifiers::SHIFT))
        .unwrap();
}

/// Right-click at `(x, y)`, then close the menu and press Shift+F10: the
/// two menus, as `written` gives them under `spot` without their spots.
fn clicked_then_keyed(app: &mut App, x: u16, y: u16, spot: &str) -> (String, String) {
    let focus = app.focus;
    right_click(app, x, y);
    let clicked = wherever(&written(app, spot));
    app.context_menu = None;
    app.focus_pane(focus);
    shift_f10(app);
    (clicked, wherever(&written(app, spot)))
}

fn opened_at(app: &App) -> (u16, u16) {
    app.context_menu.as_ref().expect("a menu is open").origin
}

fn text_app() -> (tempfile::TempDir, App) {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("main.rs");
    std::fs::write(&f, "fn main() {\n    let total = 1;\n}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    app.focus_pane(Pane::Editor);
    app.editor.cursor_row = 1;
    app.editor.cursor_col = 5;
    draw(&mut app, W, H);
    (tmp, app)
}

/// #2739: Shift+F10 in the editor opens the text's menu for the caret,
/// just below it: the menu a right-click on the caret's cell opens.
#[test]
fn shift_f10_in_the_editor_opens_the_text_menu_below_the_caret() {
    let (_tmp, mut app) = text_app();
    let (x, y) = app.editor.cursor_screen_pos().expect("the caret is drawn");
    let (clicked, keyed) = clicked_then_keyed(&mut app, x, y, "the caret");
    assert!(
        keyed.contains("GoToDefinitionAt { row: 1, col: 5 }"),
        "{keyed}"
    );
    assert_eq!(keyed, clicked);
    assert_eq!(opened_at(&app), (x, y + 1), "just below the caret");
}

/// #2739: the Menu key, as a terminal on the modern keyboard protocol
/// reports it, opens the same menu.
#[test]
fn the_menu_key_opens_the_same_menu() {
    let (_tmp, mut app) = text_app();
    shift_f10(&mut app);
    let by_shift_f10 = written(&app, "the caret");
    app.context_menu = None;
    app.handle_key(key(KeyCode::Menu, KeyModifiers::NONE))
        .unwrap();
    assert!(app.context_menu.is_some(), "the Menu key opened a menu");
    assert_eq!(written(&app, "the caret"), by_shift_f10);
}

/// #2739: Shift+F10 was Step Over. It opens the menu now and goes no
/// further; bare F10 still steps (`is_run_to_cursor_key` and the step-over
/// arm come after it).
#[test]
fn shift_f10_no_longer_steps_over() {
    let (_tmp, mut app) = text_app();
    assert!(!is_show_context_menu_key(key(
        KeyCode::F(10),
        KeyModifiers::NONE
    )));
    assert!(!is_show_context_menu_key(key(
        KeyCode::F(10),
        KeyModifiers::CONTROL
    )));
    shift_f10(&mut app);
    assert!(app.context_menu.is_some(), "Shift+F10 opened the menu");
}

/// #2739: in the Explorer, the selected row's menu.
#[test]
fn shift_f10_in_the_explorer_opens_the_selected_rows_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path();
    std::fs::create_dir(root.join("src")).unwrap();
    std::fs::write(root.join("README.md"), "# hi\n").unwrap();
    let mut app = App::new(root.to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::Explorer;
    app.show_tree = true;
    app.focus_pane(Pane::Tree);
    let idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path == root.join("README.md"))
        .unwrap();
    app.tree.select_replace(idx);
    draw(&mut app, W, H);
    let area = app.tree.last_area;
    let y = row_where(area, |y| app.tree.node_at_y(y) == Some(idx));
    let (clicked, keyed) = clicked_then_keyed(&mut app, area.x + 3, y, "README.md");
    assert!(
        keyed.contains("Rename(\"") && keyed.contains("README.md"),
        "{keyed}"
    );
    assert_eq!(keyed, clicked);
    assert_eq!(opened_at(&app).1, y + 1, "just below the row");
}

/// #2739: with COMMITS holding the keys, the picked commit's menu.
#[test]
fn shift_f10_in_commits_opens_the_picked_commits_menu() {
    let tmp = make_committed_repo();
    let p = tmp.path();
    std::fs::write(p.join("b.txt"), "b\n").unwrap();
    assert!(git_succeeds(p, &["add", "."]) && git_succeeds(p, &["commit", "-qm", "add b"]));
    let mut app = App::new(p.to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::SourceControl;
    app.show_tree = true;
    app.source_control.status.in_repo = true;
    app.commit_graph
        .set_rows(crate::git::layout_graph(crate::git::commit_graph(p, 100)));
    app.focus_pane(Pane::Tree);
    app.source_control.keys = crate::widgets::source_control::KeyStop::Commits;
    app.commit_graph.select(1);
    draw(&mut app, W, H);
    let area = app.commit_graph.last_area;
    let y = row_where(area, |y| app.commit_graph.row_index_at(y) == Some(1));
    let (clicked, keyed) = clicked_then_keyed(&mut app, area.x + 4, y, "the picked commit");
    assert!(keyed.contains("Cherry-Pick"), "{keyed}");
    assert_eq!(keyed, clicked);
    assert_eq!(opened_at(&app).1, y + 1, "just below the row");
}

/// #2739: in a terminal, the active pane's menu, at its cursor.
#[test]
fn shift_f10_in_a_terminal_opens_the_panes_menu() {
    let (_tmp, mut app, _term) = app_with_terminal_panes(2);
    app.bottom_panel_tab = BottomPanelTab::Terminal;
    app.active_terminal = 1;
    app.focus_pane(Pane::Terminal);
    draw(&mut app, 160, 30);
    let area = app.terminals[1].last_area;
    let (clicked, keyed) = clicked_then_keyed(
        &mut app,
        area.x + area.width / 2,
        area.y + area.height / 2,
        "the active pane",
    );
    assert!(keyed.contains("RenameTerminal(1)"), "{keyed}");
    assert_eq!(keyed, clicked);
}

/// #2739: in CAPTURES, the selected capture's menu.
#[test]
fn shift_f10_in_captures_opens_the_selected_captures_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    for message in ["Build failed", "Test failed"] {
        app.captures.push(crate::widgets::captures::CapturedLine {
            pane: "zsh".into(),
            shell_pid: None,
            message: message.into(),
            line: format!("error: {message}"),
            anchor: None,
        });
    }
    app.bottom_panel_tab = BottomPanelTab::Captures;
    app.focus_pane(Pane::Terminal);
    app.captures.select_index(0);
    draw(&mut app, W, H);
    let r = app
        .captures
        .row_rect(0)
        .expect("the first capture is drawn");
    let (clicked, keyed) = clicked_then_keyed(&mut app, r.x + 2, r.y, "the selected capture");
    assert!(keyed.contains("AskNavigatorAboutCapture"), "{keyed}");
    assert_eq!(keyed, clicked);
    assert_eq!(
        app.captures.selected_entry().map(|c| c.message.as_str()),
        Some("Build failed")
    );
    assert_eq!(opened_at(&app).1, r.y + 1, "just below the row");
}

/// #2739: in PROBLEMS, which has no keyboard row yet, the first problem
/// shown.
#[test]
fn shift_f10_in_problems_opens_the_first_problems_menu() {
    use crate::lsp::manager::{Diagnostic, DiagnosticSeverity};
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("hello.rs");
    std::fs::write(&file, "fn f0() {}\nfn f1() {}\nfn f2() {}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let warning = |line: u32, message: &str| Diagnostic {
        start_line: line,
        start_char: 3,
        end_line: line,
        end_char: 5,
        severity: DiagnosticSeverity::Warning,
        message: message.into(),
        tags: Default::default(),
        code: None,
        origin: None,
    };
    let mut by_server = std::collections::HashMap::new();
    by_server.insert(
        String::from("rustc"),
        vec![
            warning(1, "`f1` is never used"),
            warning(2, "`f2` is never used"),
        ],
    );
    app.lsp.diagnostics.insert(file.clone(), by_server);
    app.rebuild_problems();
    app.bottom_panel_tab = BottomPanelTab::Problems;
    app.focus_pane(Pane::Terminal);
    draw(&mut app, W, H);
    let list = app.problems.last_area;
    let y = row_where(list, |y| app.problems.diagnostic_at(y).is_some());
    let (clicked, keyed) = clicked_then_keyed(&mut app, list.x + 30, y, "the first problem");
    assert!(keyed.contains("f1"), "{keyed}");
    assert_eq!(keyed, clicked);
}

/// #2739: where what has the keys has no right-click menu (the Search
/// view), Shift+F10 opens nothing.
#[test]
fn shift_f10_opens_nothing_where_nothing_has_a_menu() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::Search;
    app.show_tree = true;
    app.focus_pane(Pane::Tree);
    draw(&mut app, W, H);
    shift_f10(&mut app);
    assert!(app.context_menu.is_none());
}

/// #2748: with a Run and Debug row holding the keys (Tab, then Down past
/// the section header), Shift+F10 opens that row's menu just below it:
/// the menu a right-click on the row opens.
#[test]
fn shift_f10_on_a_run_and_debug_row_opens_its_menu() {
    use crate::widgets::run_debug::{DebugRow, DebugRowKind};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.sidebar_view = SidebarView::RunDebug;
    app.show_tree = true;
    app.focus_pane(Pane::Tree);
    let row = |kind| DebugRow { indent: 1, kind };
    app.run_debug.debug_active = true;
    app.run_debug.debug_rows = vec![
        row(DebugRowKind::Header {
            title: "CALL STACK".into(),
        }),
        row(DebugRowKind::Frame {
            id: 11,
            selected: true,
            name: "main".into(),
            location: "s.py:6".into(),
        }),
        row(DebugRowKind::Header {
            title: "VARIABLES".into(),
        }),
        row(DebugRowKind::Variable {
            reference: 0,
            expandable: false,
            expanded: false,
            name: "count".into(),
            value: "42".into(),
            type_name: "int".into(),
            evaluate_name: Some("self.count".into()),
        }),
    ];
    draw(&mut app, W, H);
    for code in [KeyCode::Tab, KeyCode::Down] {
        app.handle_key(KeyEvent::new(code, KeyModifiers::NONE))
            .unwrap();
    }
    assert_eq!(app.run_debug.row_cursor, Some(3), "the keys reach `count`");
    shift_f10(&mut app);
    let keyed = wherever(&written(&app, "count"));
    assert!(keyed.contains("AddDebugWatch(\"self.count\")"), "{keyed}");
    let area = app.run_debug.last_area;
    let y = row_where(area, |y| app.run_debug.debug_row_at(y) == Some(3));
    assert_eq!(opened_at(&app).1, y + 1, "just below the row");
    app.context_menu = None;
    right_click(&mut app, area.x + 3, y);
    assert_eq!(keyed, wherever(&written(&app, "count")));
}