From 181170ff00a2019465b6401047407d9914b3b631 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 30 Sep 2026 19:38:59 -0300 Subject: An edit that changes nothing, an empty range deleted or a range replaced by what it holds, makes no undo step A shell's : > data on an empty address, echo abc > data over abc, or Edit s/d/d/ each pushed an undo step and a new revision, so Undo spent a press on nothing and the pane read as edited. The splice of the 9P writes and the Edit builtin now see the text is unchanged and stop. Co-Authored-By: Claude Opus 5.5 --- src/ninep/pane.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/ninep') diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 71484538..ce6d0422 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -290,6 +290,9 @@ pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const const take = if (bytes.len == 0) 0 else wholeUtf8(bytes); const lo = @min(q0, f.content.len); const hi = @max(lo, @min(q1, f.content.len)); + // An empty range deleted, or a range replaced by what it holds: no + // edit, so no undo step and no new revision. + if (std.mem.eql(u8, f.content[lo..hi], bytes[0..take])) return take; const new = p.gpa.alloc(u8, f.content.len - (hi - lo) + take) catch return null; @memcpy(new[0..lo], f.content[0..lo]); @memcpy(new[lo..][0..take], bytes[0..take]); @@ -301,6 +304,41 @@ pub fn spliceBody(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const return take; } +test "a write that changes nothing, an empty range deleted or a range replaced by itself, makes no undo step" { + const p = try withFile(testing.allocator, "abc\ndef\n"); + defer p.deinit(); + const pane = p.panes[0].?; + const serial = pane.serial; + const f = &pane.file.?; + const steps = f.history.undo_len; + const revision = f.revision; + const data = Node.of(serial, .data); + // Each as a shell does it, on an open of its own: `: > data` after + // an empty address, then `echo abc > data` over the line it holds. + _ = wr(p, Node.of(serial, .addr), "#2,#2"); + const h = call(p, .{ .tag = 1, .op = .open, .node = data, .omode = 1 }).reply.handle; + try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 2, .op = .setattr, .node = data, .truncate = true }).reply.status); + _ = call(p, .{ .tag = 3, .op = .release, .node = data, .handle = h, .opened = true }); + for ([_][]const u8{ "abc\n", "abd\n" }) |text| { + _ = wr(p, Node.of(serial, .addr), "1"); + const w = call(p, .{ .tag = 4, .op = .open, .node = data, .omode = 1 }).reply.handle; + try testing.expectEqual(tree.Status.ok, call(p, .{ .tag = 5, .op = .write, .node = data, .handle = w, .data = text }).reply.status); + _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = w, .opened = true }); + if (text[2] == 'c') { + try testing.expectEqualStrings("abc\ndef\n", f.content); + try testing.expectEqual(steps, f.history.undo_len); + try testing.expectEqual(revision, f.revision); + } + } + // Edit's `s/d/d/` is the same nothing. + const undone = f.history.undo_len; + try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .ctl), "Edit ,s/d/d/\n").reply.status); + try testing.expectEqual(undone, f.history.undo_len); + // A real change is still one. + try testing.expectEqualStrings("abd\ndef\n", f.content); + try testing.expectEqual(steps + 1, f.history.undo_len); +} + test "rows through the line index are the ones counting newlines gives, at every offset" { const p = try withFile(testing.allocator, "héllo\nwo\u{301}rld\n\n last line é\ntail"); defer p.deinit(); -- cgit v1.3