summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/File.zig48
-rw-r--r--test/behaviour/flags.zig83
2 files changed, 131 insertions, 0 deletions
diff --git a/src/File.zig b/src/File.zig
index 92e5b210..8f3d4bb9 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -2328,6 +2328,54 @@ pub fn paintWordSelection(
}
}
+test "undo and redo walk back and forth through every step of random edits, joined entries and all" {
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const pane = try p.setTestFile("seed text\nof two lines\n");
+ const f = &pane.file.?;
+ const j = &f.history.journal;
+ var prng = std.Random.DefaultPrng.init(17);
+ const r = prng.random();
+ // The text at each step's edge, the last the text now.
+ var texts: std.ArrayList([]u8) = .empty;
+ defer {
+ for (texts.items) |t| gpa.free(t);
+ texts.deinit(gpa);
+ }
+ var at: usize = 0;
+ for (0..80) |_| {
+ const was = j.done;
+ try texts.append(gpa, try gpa.dupe(u8, f.content));
+ pushUndo(p, pane);
+ // A step that changed nothing goes on, its edge where it was.
+ if (j.done == was) gpa.free(texts.pop().?);
+ // Up to six edits: most where the last ended, as typing goes on.
+ for (0..1 + r.uintLessThan(usize, 6)) |_| {
+ const len = f.content.len;
+ at = if (r.uintLessThan(u8, 4) == 0) r.uintAtMost(usize, len) else @min(at, len);
+ const del = r.uintAtMost(usize, @min(3, len - at));
+ var ins: [3]u8 = undefined;
+ const n = r.uintAtMost(usize, 3);
+ for (ins[0..n]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)];
+ try std.testing.expect(replace(p, f, at, del, ins[0..n]));
+ at += n;
+ }
+ }
+ try texts.append(gpa, try gpa.dupe(u8, f.content));
+ try std.testing.expectEqual(texts.items.len - 1, j.done);
+ var k = texts.items.len - 1;
+ while (k > 0) {
+ k -= 1;
+ undoStep(p, pane, true);
+ try std.testing.expectEqualStrings(texts.items[k], f.content);
+ }
+ for (texts.items[1..]) |t| {
+ undoStep(p, pane, false);
+ try std.testing.expectEqualStrings(t, f.content);
+ }
+}
+
test "undo keeps its steps within a byte bound, the newest always" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
diff --git a/test/behaviour/flags.zig b/test/behaviour/flags.zig
index ead267fe..753f1ece 100644
--- a/test/behaviour/flags.zig
+++ b/test/behaviour/flags.zig
@@ -142,3 +142,86 @@ test "flags: an open's writes in a row go in as one edit, seen by the next reque
s.clunk(d);
try s.expect(body, "abone\n");
}
+
+test "flags: undo keeps the last 256 steps of a long session, back to where they begin, and redo takes them all again" {
+ var s = try Session.initFile(std.testing.allocator, "");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ // Each write its own open, so its own step: 300 of them.
+ for (0..300) |i| try s.write(body, s.at("line {d}\n", .{i}));
+ const all = try s.read(body);
+ for (0..256) |_| try s.write(ctl, "Undo\n");
+ // The first 44 are older than the ring: their text stays.
+ var kept: std.ArrayList(u8) = .empty;
+ defer kept.deinit(std.testing.allocator);
+ for (0..44) |i| try kept.print(std.testing.allocator, "line {d}\n", .{i});
+ try s.expect(body, kept.items);
+ try s.write(ctl, "Undo\n");
+ try s.expect(body, kept.items);
+ try std.testing.expect(has(try s.read("/log"), "Undo: nothing to undo"));
+ for (0..256) |_| try s.write(ctl, "Redo\n");
+ try s.expect(body, all);
+}
+
+test "flags: the writes after mark 0 are one undo step until mark 1, as acme's nomark makes them" {
+ var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ const addr = s.at("/pane/{d}/addr", .{n});
+ const data = s.at("/pane/{d}/data", .{n});
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ try s.write(s.at("/pane/{d}/mark", .{n}), "0");
+ try s.write(addr, "1");
+ try s.write(data, "ONE\n");
+ try s.write(addr, "3");
+ try s.write(data, "THREE\n");
+ try s.write(body, "four\n");
+ try s.write(s.at("/pane/{d}/mark", .{n}), "1");
+ try s.write(addr, "2");
+ try s.write(data, "TWO\n");
+ try s.expect(body, "ONE\nTWO\nTHREE\nfour\n");
+ try s.write(ctl, "Undo\n");
+ try s.expect(body, "ONE\ntwo\nTHREE\nfour\n");
+ try s.write(ctl, "Undo\n");
+ try s.expect(body, "one\ntwo\nthree\n");
+ try s.write(ctl, "Redo\n");
+ try s.expect(body, "ONE\ntwo\nTHREE\nfour\n");
+}
+
+test "flags: an open's data writes are one undo step, the next open's another" {
+ var s = try Session.initFile(std.testing.allocator, "one\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ const addr = s.at("/pane/{d}/addr", .{n});
+ const data = s.at("/pane/{d}/data", .{n});
+ const ctl = s.at("/pane/{d}/ctl", .{n});
+ try s.write(addr, "#0");
+ const d = try s.hold(data, 1);
+ for ([_][]const u8{ "a", "b", "c\n" }) |piece| try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, piece));
+ s.clunk(d);
+ try s.write(addr, "$");
+ try s.write(data, "end\n");
+ try s.expect(body, "abc\none\nend\n");
+ try s.write(ctl, "Undo\n");
+ try s.expect(body, "abc\none\n");
+ try s.write(ctl, "Undo\n");
+ try s.expect(body, "one\n");
+}
+
+test "flags: a key typed at three ranges is one undo step, and its undo puts the three back" {
+ var s = try Session.initFile(std.testing.allocator, "a\na\na\n");
+ defer s.deinit();
+ const n = try s.focused();
+ const body = s.at("/pane/{d}/body", .{n});
+ // C twice: a range on each of the three lines; then X typed at each.
+ try s.keys("CCiX\x1b");
+ try s.expect(body, "Xa\nXa\nXa\n");
+ try s.keys("u");
+ try s.expect(body, "a\na\na\n");
+ try s.keys("U");
+ try s.expect(body, "Xa\nXa\nXa\n");
+}