summaryrefslogtreecommitdiff
path: root/src/lib_editor.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 07:29:27 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitb954bf23cf3137bb7fa731ab3a9dbe388a485166 (patch)
treec586d8d4528b9c5735ce9b76581bd4068149c2c0 /src/lib_editor.zig
parentf233d87376bbdfde3918f803e31b52a214ea9bdb (diff)
downloadpardes-b954bf23cf3137bb7fa731ab3a9dbe388a485166.tar.gz
pardes-b954bf23cf3137bb7fa731ab3a9dbe388a485166.zip
+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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/lib_editor.zig')
-rw-r--r--src/lib_editor.zig12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/lib_editor.zig b/src/lib_editor.zig
index f9149ef0..9bfa5cff 100644
--- a/src/lib_editor.zig
+++ b/src/lib_editor.zig
@@ -384,6 +384,18 @@ pub fn Editor(comptime cap: Capacity) type {
return ed.keep(a, live, .done);
}
+ /// The bytes from `from` to `to` replaced with `with`, as one step
+ /// to undo, the ranges following as an edit moves them (one inside
+ /// the bytes goes to their start). `join`: part of the step before
+ /// it instead, as the frames of a drag after its first, so one undo
+ /// takes the whole drag back.
+ pub fn replace(ed: *Self, a: std.mem.Allocator, from: usize, to: usize, with: []const u8, join: bool) Outcome {
+ std.debug.assert(from <= to and to <= ed.len());
+ const live = ed.thaw(a, null) catch return .{ .refused = .out_of_memory };
+ edit.replaceBytes(live.context(a), &live.text, from, to, with, !join) catch return .{ .refused = .out_of_memory };
+ return ed.keep(a, live, .done);
+ }
+
/// The register `name`'s values, one per range that wrote it.
pub fn register(ed: *const Self, a: std.mem.Allocator, name: u21) error{OutOfMemory}!?[]const []const u8 {
var r: Registers = .{};