diff options
| -rw-r--r-- | src/pardes.zig | 155 | ||||
| -rw-r--r-- | test/behaviour/modes.zig | 219 |
2 files changed, 228 insertions, 146 deletions
diff --git a/src/pardes.zig b/src/pardes.zig index ba09b3b6..2b4fef70 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -8517,56 +8517,6 @@ test "a Rename the language server finds nothing for says so, and changes nothin try std.testing.expectEqualStrings("const value=1;\n", pane.file.?.content); } -test "hopping between two panes does not grow the jump stack" { - if (platform == .web) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); - p.settings.placement = .pardes; // two panes of room, not acme's fit to the doc's two lines - defer p.deinit(); - p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc - const doc = 0; - const shell = p.active; - try std.testing.expect(shell != doc); - const depth = p.njumps; - - // Esc is Last — the everyday two-pane hop. Each press used to - // append, so sixty-four of them left the stack remembering nothing else. - for (0..8) |_| { - const was = p.active; - p.update(.{ .key = .{ .cp = Key.escape } }); - try std.testing.expect(p.active != was); - } - try std.testing.expectEqual(depth, p.njumps); - - // ...and so are the other two ways to switch panes. - for (0..8) |_| { - const want = if (p.active == doc) shell else doc; - const dir: u21 = if (p.active == doc) 'j' else 'k'; - p.update(.{ .key = .{ .cp = ' ' } }); - p.update(.{ .key = .{ .cp = 'w' } }); - p.update(.{ .key = .{ .cp = dir } }); - p.update(.{ .key = .{ .cp = Key.enter } }); - try std.testing.expectEqual(want, p.active); - } - try std.testing.expectEqual(depth, p.njumps); - - for (0..8) |_| { - const other = if (p.active == doc) shell else doc; - const r = p.rects[other]; - p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + config.GUTTER + 1, .row = r.y + BOX_H + 1 } }); - p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + config.GUTTER + 1, .row = r.y + BOX_H + 1 } }); - try std.testing.expectEqual(other, p.active); - } - try std.testing.expectEqual(depth, p.njumps); - - const before = p.active; - exec.runBuiltin(p, .Back, p.active, "", null); - p.sync(); - try std.testing.expect(p.active != before); - try std.testing.expectEqual(depth, p.njumps); -} - /// Every `.write` a keystroke produced, joined — what the pane's program would /// actually have read off its pty. fn drainWrites(p: *Pardes, buf: []u8) []const u8 { @@ -8930,35 +8880,15 @@ test "board heap: every allocation failure during boot is a clean OutOfMemory" { ); } -test "jump history clears selections and lands at the recorded cursor" { +test "jump history skips an entry whose pane slot or serial is gone, leaving the selection" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n"); p.njumps = 2; p.jcur = 1; - p.jumps[0] = .{ .pane = 0, .serial = pane.serial, .line = 3, .col = 8 }; p.jumps[1] = .{ .pane = 0, .serial = pane.serial, .line = 1, .col = 1 }; - for ([_]u21{ 'o', 'i' }, 0..) |key, index| { - pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); - pane.sel[0] = .{ .state = .done, .c0 = 2, .c1 = 5, .r0 = 1, .r1 = 1 }; - try edit.capturePointerSelection(p, pane, 0); - pane.body.select = true; - p.update(.{ .key = .{ .cp = key, .ctrl = true } }); - try std.testing.expectEqual(index, p.jcur); - try std.testing.expectEqual(@as(usize, 2), p.njumps); - try std.testing.expectEqual(@as(u32, 3), p.jumps[0].line); - try std.testing.expect(!pane.body.vsel.active and !pane.body.msel.active and !pane.body.select); - try std.testing.expectEqual(@as(u16, 0), pane.body.nsel); - try std.testing.expectEqual(.none, pane.sel[0].state); - try std.testing.expect(pane.pointer_selections[0] == null); - try std.testing.expectEqual(@as(i32, if (index == 0) 2 else 0), pane.body.cur_row); - try std.testing.expectEqual(@as(i32, if (index == 0) 7 else 0), pane.body.cur_col); - } pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true); const selected = pane.body.primaryRange(pane.file.?.content, 0); - p.jumpBy(1); // No forward entry: selection stays untouched. - try std.testing.expectEqualDeep(selected, pane.body.primaryRange(pane.file.?.content, 0)); - try std.testing.expect(std.mem.indexOf(u8, p.panes[p.active].?.msg[0..p.panes[p.active].?.msg_len], "nothing further on") != null); for ([_]Loc{ .{ .pane = 15, .serial = pane.serial, .line = 1, .col = 1 }, .{ .pane = 0, .serial = pane.serial + 1, .line = 1, .col = 1 }, @@ -8970,7 +8900,7 @@ test "jump history clears selections and lands at the recorded cursor" { } } -/// A session for the JumpScope tests: four panes of 100 lines in one +/// A session for the side-button jump test: four panes of 100 lines in one /// column, A (pane 0) and B each on a file of its own, C on A's file, and O /// an output buffer, no file behind it. `order` is the jumplist, a letter a /// place, each letter's n-th place its line 10 + 40n: "ABABA" is A10 B10 A50 @@ -8980,7 +8910,7 @@ const JumpsTest = struct { ids: [4]usize = .{ 0, 0, 0, 0 }, const letters = "ABCO"; - const Way = enum { call, key, builtin, mouse }; + const Way = enum { call, mouse }; fn init(order: []const u8) !JumpsTest { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 80 }); @@ -9021,14 +8951,12 @@ const JumpsTest = struct { return t; } - /// One step `delta` through `way`: the function, Ctrl-o or Ctrl-i, the - /// mouse's side button pressed and let go, or the builtin's word. + /// One step `delta` through `way`: the function, or the mouse's side + /// button pressed and let go. fn step(t: JumpsTest, way: Way, delta: i32) void { const back = delta < 0; switch (way) { .call => t.p.jumpBy(delta), - .key => t.p.update(.{ .key = .{ .cp = if (back) 'o' else 'i', .ctrl = true } }), - .builtin => _ = t.p.executeBuiltinLine(t.p.active, if (back) "Back" else "Forward"), .mouse => for ([_]Mouse.Kind{ .press, .release }) |kind| t.p.update(.{ .mouse = .{ .button = if (back) .back else .forward, .kind = kind, .col = 0, .row = 0 } }), } @@ -9054,75 +8982,10 @@ const JumpsTest = struct { } }; -test "JumpScope file: Back keeps to the focused file, A50 then A10, and leaves it only once it runs out that way" { - const t = try JumpsTest.init("BABABA"); - defer t.p.deinit(); - try std.testing.expectEqual(config.Runtime.JumpScope.file, t.p.settings.jump_scope); - var buf: [128]u8 = undefined; - // B90, B50 passed over, kept in their places: A has nothing older than - // A10, so the step after it is the nearest place of any file, B10. - try std.testing.expectEqualStrings("A50 A10 B10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqual(@as(usize, 6), t.p.njumps); - // Forward is the mirror, from B10 in B: B's own places first, then A90, - // the newest, where the Backs started. - try std.testing.expectEqualStrings("B50 B90 A90", try t.walk(.call, 1, &buf)); - try std.testing.expectEqual(@as(usize, 5), t.p.jcur); -} - -test "JumpScope file: Forward retraces a run of Backs inside one file step for step, back to where it began" { - const t = try JumpsTest.init("ABABA"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // A1 B1 A2 B2 A3 from A3: nothing older than A1 at all, so Back ends there. - try std.testing.expectEqualStrings("A50 A10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqual(@as(usize, 0), t.p.jcur); - try std.testing.expectEqualStrings("A50 A90", try t.walk(.call, 1, &buf)); - try std.testing.expectEqual(@as(usize, 4), t.p.jcur); - // One Back, one Forward: the same entry, whichever were passed over. - t.step(.call, -1); - try std.testing.expectEqual(@as(usize, 2), t.p.jcur); - t.step(.call, 1); - try std.testing.expectEqual(@as(usize, 4), t.p.jcur); - try std.testing.expectEqual(@as(usize, 5), t.p.njumps); -} - -test "JumpScope all: Back and Forward take every place in the order it was jumped to" { - const t = try JumpsTest.init("ABABA"); - defer t.p.deinit(); - try std.testing.expect(t.p.executeBuiltinLine(t.p.active, "JumpScope all")); - try std.testing.expectEqual(config.Runtime.JumpScope.all, t.p.settings.jump_scope); - var buf: [128]u8 = undefined; - try std.testing.expectEqualStrings("B50 A50 B10 A10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqualStrings("B10 A50 B50 A90", try t.walk(.call, 1, &buf)); - // Bare, the word steps back to file. - try std.testing.expect(t.p.executeBuiltinLine(t.p.active, "JumpScope")); - try std.testing.expectEqual(config.Runtime.JumpScope.file, t.p.settings.jump_scope); -} - -test "JumpScope file: from a pane with no file, Back and Forward are global" { - const t = try JumpsTest.init("OAOBO"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // O90 keeps to nothing: B10, the next older of all. From B, nothing of - // B is older, so O50; from O, A10; from A, O10. - try std.testing.expectEqualStrings("B10 O50 A10 O10", try t.walk(.call, -1, &buf)); - try std.testing.expectEqualStrings("A10 O50 B10 O90", try t.walk(.call, 1, &buf)); -} - -test "JumpScope file: a file in two panes keeps to the pane first, then the file in the other pane" { - // C is A's file in a second pane: C10 B10 A10 C50 A50. - const t = try JumpsTest.init("CBACA"); - defer t.p.deinit(); - var buf: [128]u8 = undefined; - // From A50: A10 in this pane, over the nearer C50; then none of A's - // own is older, so the file's in C, C10, over B10. - try std.testing.expectEqualStrings("A10 C10", try t.walk(.call, -1, &buf)); - // From C10 in C: C50, its own, then the file's newest, A50. - try std.testing.expectEqualStrings("C50 A50", try t.walk(.call, 1, &buf)); -} - -test "Back and Forward take the same steps from Ctrl-o and Ctrl-i, the mouse's side buttons, the builtins and jumpBy" { - for ([_]JumpsTest.Way{ .call, .key, .builtin, .mouse }) |way| { +// The keys and builtins are test/behaviour/modes.zig's; a mouse's side +// buttons have no /mousein bits. +test "Back and Forward take the same steps from the mouse's side buttons and jumpBy" { + for ([_]JumpsTest.Way{ .call, .mouse }) |way| { const t = try JumpsTest.init("BABABA"); defer t.p.deinit(); var buf: [128]u8 = undefined; diff --git a/test/behaviour/modes.zig b/test/behaviour/modes.zig index f29a4b56..b1641c6f 100644 --- a/test/behaviour/modes.zig +++ b/test/behaviour/modes.zig @@ -259,3 +259,222 @@ test "modes: window keys walk from a pane up through its column's tag and the wo try expectEqual((try s.where("body"))[1], (try s.cursor()).?[1]); try s.expect(s.at("/pane/{d}/body", .{a}), "body\n"); } + +/// The jump tests' session: a pane a letter of `order`, A and B each a file +/// of 100 lines, C a Zerox of A, O the +Help output (no file behind it), +/// and the jumplist `order`, a letter a place, each letter's n-th place its +/// line 1 + 40n ("ABABA" is A1 B1 A41 B41 A81): each made by moving its +/// pane's dot there and giving it the keyboard, as a jump does (a new +/// pane's first, where it opens). The shell the session started with is +/// closed after, its places with it. +const Jumps = struct { + s: Session, + ids: [4]u32 = @splat(0), + + const letters = "ABCO"; + + fn init(order: []const u8) !Jumps { + var t: Jumps = .{ .s = try Session.initTerm(std.testing.allocator) }; + errdefer t.s.deinit(); + const s = &t.s; + const shell = try s.focused(); + var seen: [4]u32 = @splat(0); + for (order) |letter| { + const which = std.mem.indexOfScalar(u8, letters, letter).?; + if (t.ids[which] == 0) t.ids[which] = switch (letter) { + 'O' => o: { + try s.write(s.at("/pane/{d}/exec", .{shell}), "Help\n"); + break :o try s.focused(); + }, + else => made: { + // C is A's twin, and A C's when C came first. + const twin = t.ids[if (letter == 'A') 2 else if (letter == 'C') 0 else 1]; + if (letter != 'B' and twin != 0) { + try s.write(s.at("/pane/{d}/exec", .{twin}), "Zerox\n"); + break :made try s.focused(); + } + const n = try s.newPane(); + try s.write(s.at("/pane/{d}/name", .{n}), if (letter == 'B') "/b.txt\n" else "/a.txt\n"); + try s.write(s.at("/pane/{d}/body", .{n}), std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + break :made n; + }, + }; + const n = t.ids[which]; + try s.write(s.at("/pane/{d}/addr", .{n}), s.at("{d}\n", .{1 + 40 * seen[which]})); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.write("/focus", s.at("{d}\n", .{n})); + seen[which] += 1; + } + try s.write(s.at("/pane/{d}/ctl", .{shell}), "delete\n"); + return t; + } + + /// Where the keyboard is: its pane's letter and the dot's line, `A50`. + fn here(t: *Jumps) ![]const u8 { + const s = &t.s; + const n = try s.focused(); + const at, _ = try s.range(s.at("/pane/{d}/dot", .{n})); + const body = try s.read(s.at("/pane/{d}/body", .{n})); + const which = std.mem.indexOfScalar(u32, &t.ids, n).?; + return s.at("{c}{d}", .{ letters[which], std.mem.count(u8, body[0..at], "\n") + 1 }); + } + + const Way = enum { key, leader, builtin }; + + /// Steps back or on until the list ends that way, as it says; the + /// places landed on, "A50 A10 B10". + fn walk(t: *Jumps, way: Way, back: bool) ![]const u8 { + const s = &t.s; + var out: []const u8 = ""; + for (0..64) |_| { + const before = try t.here(); + switch (way) { + .key => try s.keys(if (back) "\x0f" else "\u{F017}i"), + .leader => try s.keys(if (back) " jo" else " ji"), + .builtin => try s.write("/exec", if (back) "Back\n" else "Forward\n"), + } + const now = try t.here(); + if (std.mem.eql(u8, before, now)) break; + out = if (out.len == 0) now else s.at("{s} {s}", .{ out, now }); + } else return error.NoEnd; + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), if (back) "nothing further back" else "nothing further on") != null); + return out; + } +}; + +test "modes: Ctrl-o keeps to the focused file (JumpScope file), A41 then A1, and leaves it only once it runs out that way; Ctrl-i mirrors it" { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); + // B81, B41 passed over, kept in their places: A has nothing older than + // A1, so the step after it is the nearest place of any file, B1. + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(.key, true)); + // Forward is the mirror, from B1 in B: B's own places first, then A81, + // the newest, where the Backs started. + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(.key, false)); +} + +test "modes: Ctrl-i retraces a run of Ctrl-o inside one file step for step, back to where it began" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + // A1 B1 A41 B41 A81 from A81: nothing older than A1 at all. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A41 A81", try t.walk(.key, false)); + // One back, one on: the same place, whichever were passed over. + try t.s.keys("\x0f"); + try std.testing.expectEqualStrings("A41", try t.here()); + try t.s.keys("\u{F017}i"); + try std.testing.expectEqualStrings("A81", try t.here()); + // And the steps made no places of their own. + try std.testing.expectEqualStrings("A41 A1", try t.walk(.key, true)); +} + +test "modes: with JumpScope all, Ctrl-o and Ctrl-i take every place in the order it was jumped to, and bare JumpScope is file again" { + var t = try Jumps.init("ABABA"); + defer t.s.deinit(); + try t.s.write("/ctl", "JumpScope all\n"); + try std.testing.expectEqualStrings("B41 A41 B1 A1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("B1 A41 B41 A81", try t.walk(.key, false)); + try t.s.write("/ctl", "JumpScope\n"); + try std.testing.expect(std.mem.indexOf(u8, try t.s.read("/ctl"), "\nJumpScope file\n") != null); +} + +test "modes: from a pane with no file, Ctrl-o and Ctrl-i step through every place" { + var t = try Jumps.init("OAOBO"); + defer t.s.deinit(); + // O81 keeps to nothing: B1, the next older of all. From B, nothing of + // B is older, so O41; from O, A1; from A, O1. + try std.testing.expectEqualStrings("B1 O41 A1 O1", try t.walk(.key, true)); + try std.testing.expectEqualStrings("A1 O41 B1 O81", try t.walk(.key, false)); +} + +test "modes: Ctrl-o in a file shown in two panes keeps to its own pane first, then the file in the other" { + // C is A's file in a second pane: C1 B1 A1 C41 A41. + var t = try Jumps.init("CBACA"); + defer t.s.deinit(); + // From A41: A1 in this pane, over the nearer C41; then none of A's own + // is older, so the file's in C, C1, over B1. + try std.testing.expectEqualStrings("A1 C1", try t.walk(.key, true)); + // From C1 in C: C41, its own, then the file's newest, A41. + try std.testing.expectEqualStrings("C41 A41", try t.walk(.key, false)); +} + +test "modes: Back and Forward take the same steps from Ctrl-o and Ctrl-i, SPC j o and SPC j i, and the builtins" { + for ([_]Jumps.Way{ .key, .leader, .builtin }) |way| { + var t = try Jumps.init("BABABA"); + defer t.s.deinit(); + try std.testing.expectEqualStrings("A41 A1 B1", try t.walk(way, true)); + try std.testing.expectEqualStrings("B41 B81 A81", try t.walk(way, false)); + } +} + +test "modes: Ctrl-o and Ctrl-i drop the selections, select mode and the extra cursors, and land where the place was left" { + const rows = std.mem.asBytes(&@as([97][4]u8, @splat("row\n".*))); + var s = try Session.initFile(std.testing.allocator, "alpha beta\ngamma delta\nepsilon zeta\n" ++ rows); + defer s.deinit(); + const n = try s.focused(); + const dot = s.at("/pane/{d}/dot", .{n}); + // A place on line 3, at its eighth column (byte 30), then line 60. + try s.write(s.at("/pane/{d}/addr", .{n}), "#30\n"); + try s.write(s.at("/pane/{d}/ctl", .{n}), "dot=addr\n"); + try s.keys("60G"); + // Each step lands on the cursor the place was left with. + var back_to: u32 = 30; + for ([_][]const u8{ "\x0f", "\u{F017}i" }) |key| { + // A line selected, a second cursor under it, select mode on. + try s.keys("xCv"); + try std.testing.expectEqualStrings("select", try mode(&s, n)); + const leaving = (try s.range(dot))[1] - 1; // the cursor: its last character + try s.keys(key); + try std.testing.expectEqualStrings("normal", try mode(&s, n)); + try std.testing.expectEqual(back_to, (try s.range(dot))[0]); + back_to = leaving; + try s.expect(s.at("/pane/{d}/sel", .{n}), ""); + // One cursor: a Z typed goes in once. + try s.keys("iZ\x1b"); + try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read(s.at("/pane/{d}/body", .{n})), "Z")); + try s.keys("u"); + } + // No place further on: said, and the selection left as it was. + try s.keys("x"); + const sel = try s.read(s.at("/pane/{d}/sel", .{n})); + try s.keys("\u{F017}i"); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further on") != null); + try s.expect(s.at("/pane/{d}/sel", .{n}), sel); +} + +test "modes: hopping between two panes by Esc, window mode or a click makes no places, so one Ctrl-o goes back across and the next has nowhere to go" { + var s = try Session.initFile(std.testing.allocator, std.mem.asBytes(&@as([100][4]u8, @splat("row\n".*)))); + defer s.deinit(); + // Two panes of room, not acme's fit to the doc. + try s.write("/ctl", "Placement pardes\n"); + const doc = try s.focused(); + try s.keys("\u{F015}n"); // a shell under the doc + const shell = try s.focused(); + if (std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{shell})), "mode:tty") != null) try s.keys("\x02"); + for (0..8) |_| { + const was = try s.focused(); + try s.keys("\x1b"); + try expectEqual(if (was == doc) shell else doc, try s.focused()); + } + for (0..8) |_| { + const want = if (try s.focused() == doc) shell else doc; + try s.keys(if (want == shell) " wj\n" else " wk\n"); + try expectEqual(want, try s.focused()); + } + const doc_row = (try s.where("row"))[1]; + const shell_row = (try s.where("Tty Save Mode"))[1] + 1; + for (0..8) |_| { + const other = if (try s.focused() == doc) shell else doc; + const row = if (other == doc) doc_row else shell_row; + try s.write("/mousein", s.at("m 10 {d} 1\nm 10 {d} 0\n", .{ row, row })); + try expectEqual(other, try s.focused()); + } + const before = try s.focused(); + try s.keys("\x0f"); + try std.testing.expect(before != try s.focused()); + const after = try s.focused(); + try s.keys("\x0f"); + try expectEqual(after, try s.focused()); + try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "nothing further back") != null); +} |
