From ffd67bb16e9080a9f06cf4726000ec3b0bfa07df Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 11:44:28 -0300 Subject: text journal, the step boundaries pinned through the 9P tree: the undo ring keeps a long session's last 256 steps (300 appends, each its own open, undone back to the 44th and no further, then redone whole), the writes between mark 0 and mark 1 are one step, an open's data writes one step and the next open's another, a key typed at three ranges one step whose undo puts the three back (test/behaviour/flags.zig, four new tests); and in File.zig a walk back and forth through 80 steps of random edits, most where the last ended so their entries join, every step's text met again both ways (one new test) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/File.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src') 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 }); -- cgit v1.3