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 --- src/edit.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/edit.zig') diff --git a/src/edit.zig b/src/edit.zig index 8c59ffa1..32943c19 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -1563,6 +1563,24 @@ fn mapThroughChanges(chs: []const sam.Op, pos: usize) usize { return @intCast(@as(i64, @intCast(pos)) + delta); } +/// The bytes from `from` to `to` replaced with `bytes` (the library's +/// `replace`), every range following the edit as `normalAlign`'s do; a step +/// to undo of its own when `step`, else part of the one before it. +pub fn replaceBytes(e: Editor, t: *Text, from: usize, to: usize, bytes: []const u8, step: bool) Error!void { + const expl = t.vsel.active and t.vsel.explicit; + const text = try flatSurface(e, t); + var rs: [Text.max_selections]modal.Selection = undefined; + const got = t.ranges(text, 0, &rs); + const ch = [1]sam.Op{.{ .q0 = @intCast(from), .q1 = @intCast(to), .text = bytes }}; + var cells: [Text.max_selections]Text.Cells = undefined; + for (rs[0..got.n], cells[0..got.n]) |r, *cell| { + const c = Text.rangeCells(text, r); + cell.* = .{ .cur = mapThroughChanges(&ch, c.cur), .anc = mapThroughChanges(&ch, c.anc) }; + } + if (step) try pushUndo(e, t); + try setEditCells(e, t, try sam.apply(e.gpa, text, &ch), 0, cells[0..got.n], got.pri, expl); +} + /// `J` / `Alt-J`: join every range's lines with the next, in ONE edit /// (helix join_selections): the newline and the next line's leading blanks /// become one space, or nothing when that line is blank, and a line two -- cgit v1.3