summaryrefslogtreecommitdiff
path: root/test/behaviour/mouse.zig
blob: 0abe15dc4f96886a1273aaf2d0271aee3ff97806 (plain) (blame)
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
//! docs/typ/guide.typ, The mouse <mouse>.
const std = @import("std");
const Session = @import("session").Session;

/// The mouse as `/mousein` lines say it, `m col row buttons` each.
fn mouse(s: *Session, comptime lines: []const u8, args: anytype) !void {
    try s.write("/mousein", s.at(lines, args));
}

test "mouse: a double-click selects a word, a line, a bracketed or quoted text, as acme's" {
    var s = try Session.initFile(std.testing.allocator, "call foo_bar(a, b)\nsay \"hi there\"\n");
    defer s.deinit();
    const n = try s.focused();
    const sel = s.at("/pane/{d}/sel", .{n});
    for ([_]struct { at: []const u8, into: usize, want: []const u8 }{
        .{ .at = "foo_bar", .into = 2, .want = "foo_bar" },
        .{ .at = "(a, b)", .into = 1, .want = "a, b" },
        .{ .at = "call", .into = 0, .want = "call foo_bar(a, b)\n" },
        .{ .at = "\"hi", .into = 1, .want = "hi there" },
    }) |w| {
        try s.click(w.at, w.into, 1);
        try s.click(w.at, w.into, 1);
        try s.expect(sel, w.want);
        // A third click is a plain click again: the caret, no selection.
        try s.click(w.at, w.into, 1);
        try s.expect(sel, "");
    }
    // In the tag too: what it selected is what a yank takes.
    try s.click("Collapse Del", "Collapse D".len, 1);
    try s.click("Collapse Del", "Collapse D".len, 1);
    try s.keys("\x1by:ggP");
    try s.expect(s.at("/pane/{d}/body", .{n}), "Delcall foo_bar(a, b)\nsay \"hi there\"\n");
}

test "mouse: everything a B1 chord does while B1 is down is one undo step" {
    var s = try Session.initFile(std.testing.allocator, "one two three\n");
    defer s.deinit();
    const n = try s.focused();
    const body = s.at("/pane/{d}/body", .{n});
    const x, const y = try s.where("one two");
    // B1-B2 then B1-B3: cut, then paste back: a copy, one step.
    try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 5\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y });
    try s.expect(body, "one two three\n");
    // Undone in one step, it is the text before the cut, not after it.
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
    try s.expect(body, "one two three\n");
    // A cut alone comes back with one Undo.
    try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 1\nm {d} {d} 0\n", .{ x + 4, y, x + 6, y, x + 6, y, x + 6, y, x + 6, y });
    try s.expect(body, "one  three\n");
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
    try s.expect(body, "one two three\n");
}

test "mouse: a click past a line's or the file's end puts the caret where insertion types" {
    for ([_]bool{ false, true }) |already_insert| {
        for ([_]bool{ false, true }) |bottom_tag| {
            for ([_]struct { text: []const u8, click_row: usize, dot: u32, col: usize, row: usize, result: []const u8 }{
                .{ .text = "abc\nxy", .click_row = 0, .dot = 3, .col = 3, .row = 0, .result = "abcZ\nxy" },
                .{ .text = "abc\nxy", .click_row = 5, .dot = 6, .col = 2, .row = 1, .result = "abc\nxyZ" },
                .{ .text = "abc\n", .click_row = 5, .dot = 4, .col = 0, .row = 1, .result = "abc\nZ" },
                .{ .text = "", .click_row = 5, .dot = 0, .col = 0, .row = 0, .result = "Z" },
            }) |case| {
                var s = try Session.initFile(std.testing.allocator, case.text);
                defer s.deinit();
                const n = try s.focused();
                if (bottom_tag) try s.write("/ctl", "Tagbottom on\n");
                if (already_insert) try s.keys("i");
                // The first line's number, then a blank, then its text.
                const x, const y = try s.where("1 ");
                try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 2 + 15, y + case.click_row, x + 2 + 15, y + case.click_row });
                try std.testing.expectEqual(.{ case.dot, case.dot }, try s.range(s.at("/pane/{d}/dot", .{n})));
                // The caret is drawn where it is, and insertion draws it there too.
                try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?);
                if (!already_insert) try s.keys("i");
                try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:insert "));
                try std.testing.expectEqual(.{ x + 2 + case.col, y + case.row }, (try s.cursor()).?);
                try s.keys("Z");
                try s.expect(s.at("/pane/{d}/body", .{n}), case.result);
            }
        }
    }
}

test "mouse: a click on a tab's or a wrapped wide glyph's cells puts the caret on it" {
    for ([_]bool{ false, true }) |already_insert| {
        for ([_]bool{ false, true }) |wrap| {
            const source = if (wrap) "012345678901234567890123456789界tail\n" else "\t界tail\n";
            var s = try Session.initFile(std.testing.allocator, source);
            defer s.deinit();
            s.detach();
            try s.write("/ctl", "size 24 12\n");
            const n = try s.focused();
            if (!wrap) try s.write("/ctl", "Wrap off\n");
            if (already_insert) try s.keys("i");
            const x, const y = try s.where("界");
            if (wrap) try std.testing.expect(y > (try s.where("1 "))[1]);
            // The glyph's second cell.
            try mouse(&s, "m {d} {d} 1\nm {d} {d} 0\n", .{ x + 1, y, x + 1, y });
            const at: u32 = @intCast(try std.unicode.utf8CountCodepoints(source[0..std.mem.indexOf(u8, source, "界").?]));
            try std.testing.expectEqual(.{ at, at }, try s.range(s.at("/pane/{d}/dot", .{n})));
            // Drawn on the glyph's first cell, in insert mode too.
            try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?);
            if (!already_insert) try s.keys("i");
            try std.testing.expectEqual(.{ x, y }, (try s.cursor()).?);
            try s.keys("Z");
            const byte = std.mem.indexOf(u8, source, "界").?;
            try s.expect(s.at("/pane/{d}/body", .{n}), s.at("{s}Z{s}", .{ source[0..byte], source[byte..] }));
        }
    }
}

test "mouse: a B1 sweep over several lines is a stream, as acme's, in a body and a tag" {
    var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\n");
    defer s.deinit();
    const n = try s.focused();
    const body = s.at("/pane/{d}/body", .{n});
    const x, const y = try s.where("alpha");
    // From the press to the end of its line, the line between whole, the
    // last from its start: no line numbers, no rectangle. B2 cuts it.
    try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 2, y, x + 1, y + 2, x + 1, y + 2, x + 1, y + 2 });
    try s.expect(body, "alarlie\n");
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
    // Upward, the same text.
    try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ x + 1, y + 2, x + 2, y, x + 2, y, x + 2, y });
    try s.expect(body, "alarlie\n");
    // A tag of several rows sweeps the same way.
    const tag = s.at("/pane/{d}/tag", .{n});
    try s.write(tag, "\nLook Edit");
    const tx, const ty = try s.where("Collapse");
    const lx, const ly = try s.where("Look Edit");
    try mouse(&s, "m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 3\nm {d} {d} 0\n", .{ tx + 3, ty, lx + 1, ly, lx + 1, ly, lx + 1, ly });
    try s.expect(tag, "/test.txt Save Tty Colok Edit");
}