From b954bf23cf3137bb7fa731ab3a9dbe388a485166 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:29:27 -0300 Subject: +66 -0 lib: `replace(a, from, to, with, join)` rewrites a byte range of the library's text (docs/next-editor.md): one step to undo, every range following the edit as a sam edit moves them (edit.replaceBytes: one sam.Op, the cursor and anchor cells mapped through it as normalAlign's are, then setEditCells), refused whole at capacity as a key is (keep); `join` makes it part of the step before it instead, so a drag that rewrites a number every frame passes false on its first frame and true on the rest and one undo takes the whole drag back; pardes calls none of it; test/lib: a number dragged three frames, the cursor after it following, one undo step for the drag, undone and redone, and a replace that would not fit refused with the value unchanged; hxlib 538 of 658 / --parity 618 of 738 with 0 mismatches, core-test 811 of 950, shell-test 990 of 993, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass, lib-test 10 of 10 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/lib/editor.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'test/lib') diff --git a/test/lib/editor.zig b/test/lib/editor.zig index 2b234cd8..2784b176 100644 --- a/test/lib/editor.zig +++ b/test/lib/editor.zig @@ -178,3 +178,39 @@ test "with a parse handed in, the tree-sitter walks run in the library; without, _ = try ed.ranges(a, &out); try std.testing.expectEqual(pe.Selection{ .anchor = 0, .head = 25 }, out[0]); } + +test "a byte range replaced is one step to undo, the ranges following it; a drag joined is one step, refused whole when it does not fit" { + const gpa = std.testing.allocator; + const ed = try gpa.create(Ed); + defer gpa.destroy(ed); + ed.* = .{}; + try ed.load("a = 12 + 3;\n"); + try typeKeys(ed, gpa, "9l"); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [4096]u8 = undefined; + var out: [pe.max_selections]pe.Selection = undefined; + // a drag on the number: its first frame a step, the rest joined to it + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 6, "130", false)); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 10, .head = 11 }, out[0]); // still on `3` + const steps = ed.undo.count; + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "131", true)); + try std.testing.expectEqual(pe.Outcome.done, ed.replace(a, 4, 7, "14", true)); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + try std.testing.expectEqual(steps, ed.undo.count); + _ = try ed.ranges(a, &out); + try std.testing.expectEqual(pe.Selection{ .anchor = 9, .head = 10 }, out[0]); + try typeKeys(ed, gpa, "u"); + try std.testing.expectEqualStrings("a = 12 + 3;\n", text(ed, &buf)); + try typeKeys(ed, gpa, "U"); + try std.testing.expectEqualStrings("a = 14 + 3;\n", text(ed, &buf)); + // what would not fit: refused, the value as it was + const kept = try gpa.create(Ed); + defer gpa.destroy(kept); + kept.* = ed.*; + const ys: [4096]u8 = @splat('y'); + try std.testing.expectEqual(pe.Outcome{ .refused = .text_full }, ed.replace(a, 0, 1, &ys, false)); + try std.testing.expectEqualSlices(u8, std.mem.asBytes(kept), std.mem.asBytes(ed)); +} -- cgit v1.3