summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/edit.zig18
-rw-r--r--src/lib_editor.zig12
-rw-r--r--test/lib/editor.zig36
3 files changed, 66 insertions, 0 deletions
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
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 = .{};
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));
+}