summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 19:47:19 -0300
committerGabriel Schneider <[email protected]>2026-11-05 20:49:41 -0300
commit9e504559356ed985999d8625947d9b08ad582792 (patch)
tree458f191f28d949d3025bd9f7f2c085e92fba634b /src
parent992d5b1aa2813fcba05d8564872f8d426eade9f6 (diff)
downloadpardes-9e504559356ed985999d8625947d9b08ad582792.tar.gz
pardes-9e504559356ed985999d8625947d9b08ad582792.zip
rollback: a replica's core writes a file's text and its line index in place, as the session's does, and its rollback copy keeps none of their bytes: the epoch notes what each write into a text or an index that was there when the copy was taken took out (rollback.Epoch.wrote, from File.edit and shiftIndex), and putting the copy back writes that back, the newest first, before the saved bytes go back (unwrite), where a replica's edit made a whole new text and a whole new index at every key so that the copy could pin the old ones; the bridge the journal left for this goes with it: rollback.isEpoch, File.keepsBytes (a text a join is still sending is copied on write as before, memory.isPinned), the replica's copy-on-write in File.edit and shiftIndex's `keep` (its in-place shift is File.shiftStarts, which the rollback runs turned round); the journal's steps, its `done` and its entries come back as the copy saved them, an entry a speculation merged into and the redo entries it dropped being saved bytes and put-off frees; File.State's content and line_starts are annotated `.journal`, which the copy neither saves nor pins (line_starts leaves `.blob`); a write that could not be noted (no memory) fails the replica at its rollback; tests: rollback.zig's 'a text written in place under a copy comes back byte for byte: typed into its room, undone, cut, and grown past it' is new, and File.zig's 'a line index shifted in place equals a rebuilt one' becomes '..., and the edit turned round gives the one before back' (its every tenth shift made anew, as a replica's core made them, gives way to the shift turned round in a copy, as a rollback makes it)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/File.zig109
-rw-r--r--src/memory.zig6
-rw-r--r--src/replica.zig5
-rw-r--r--src/rollback.zig180
4 files changed, 226 insertions, 74 deletions
diff --git a/src/File.zig b/src/File.zig
index 89acd4d8..8e5c0101 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -111,10 +111,15 @@ pub const State = struct {
/// from the text whenever it is wanted.
pub const replica = .{
.derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" },
- // Written in place by `replace`, but never in a replica's core or
- // while a join sends them (`keepsBytes`): there an edit puts a new
- // buffer in their place.
- .blob = .{ "content", "line_starts", "location_rows" },
+ // Big texts, dumped and sent by their hash (src/walk.zig). The
+ // text is written in place by `replace`, but never while a join
+ // sends it (memory.zig `pin`): then an edit puts a new buffer in
+ // its place.
+ .blob = .{ "content", "location_rows" },
+ // Written in place in a replica's core too: its rollback copy keeps
+ // none of their bytes, and takes back what a speculation wrote
+ // (src/rollback.zig `Epoch.wrote`).
+ .journal = .{ "content", "line_starts" },
};
path: []u8,
@@ -604,55 +609,63 @@ fn ascending(key: usize, item: usize) std.math.Order {
pub const Span = struct { head: usize, tail: usize };
/// `f`'s line index through the edit that puts `bytes` over the `len` bytes
-/// at `off`, in its own buffer: the starts before the edit stand, those it
-/// took out go, its newlines' come in, and those after it move by what it
-/// added; only the moving is a pass over the index, and only from the edit
-/// on. `keep` (a replica's core, whose rollback copy holds the old index)
-/// makes a new one instead, as every edit did once. On no memory the index
-/// goes, to be made again when it is asked for.
-fn shiftIndex(gpa: std.mem.Allocator, f: *State, off: usize, len: usize, bytes: []const u8, keep: bool) void {
+/// at `off` (`shiftStarts`), in its own buffer, grown by an eighth more when
+/// it must be. On no memory the index goes, to be made again when it is
+/// asked for.
+fn shiftIndex(gpa: std.mem.Allocator, f: *State, off: usize, len: usize, bytes: []const u8) void {
const starts = f.line_starts;
const room = indexRoom(f);
- const kept = std.sort.upperBound(usize, starts, off, ascending);
- const moved = starts.len - std.sort.upperBound(usize, starts, off + len, ascending);
- const n = kept + std.mem.count(u8, bytes, "\n") + moved;
- const from = starts.len - moved;
- const to = n - moved;
+ const n = starts.len - (std.sort.upperBound(usize, starts, off + len, ascending) - std.sort.upperBound(usize, starts, off, ascending)) + std.mem.count(u8, bytes, "\n");
var out = room;
- if (keep or n > room.len) {
- const want = if (keep) n else n + @max(n / 8, 512);
- out = gpa.alloc(usize, want) catch {
+ if (n > room.len) {
+ out = gpa.alloc(usize, n + @max(n / 8, 512)) catch {
pardes.memory.freeRaw(gpa, room);
f.line_starts = &.{};
f.line_cap = 0;
return;
};
- @memcpy(out[0..kept], starts[0..kept]);
- }
- // In place, the starts that move go back to front when the edit grew
- // the index, front to back when it shrank it.
- if (to > from and out.ptr == room.ptr) {
+ @memcpy(out[0..starts.len], starts);
+ } else pardes.rollback.Epoch.wrote(gpa, usize, room, starts.len, n, off, f.content[off..][0..len], bytes.len);
+ shiftStarts(out, starts.len, n, off, len, bytes);
+ if (out.ptr != room.ptr) pardes.memory.freeRaw(gpa, room);
+ f.line_starts = out[0..n];
+ f.line_cap = out.len;
+}
+
+/// The first `len` of `room`, a line index, through the edit that puts
+/// `bytes` over the `cut` bytes at `off`, in place, `n` long after it
+/// (`room` holds that): the starts before the edit stand, those it took
+/// out go, its newlines' come in, and those after it move by what it added;
+/// only the moving is a pass over the index, and only from the edit on. The
+/// edit turned round (`cut` its length in, `bytes` what it took out) gives
+/// the index before it back: a rollback's (src/rollback.zig `unwrite`).
+pub fn shiftStarts(room: []usize, len: usize, n: usize, off: usize, cut: usize, bytes: []const u8) void {
+ const starts = room[0..len];
+ const kept = std.sort.upperBound(usize, starts, off, ascending);
+ const moved = len - std.sort.upperBound(usize, starts, off + cut, ascending);
+ const from = len - moved;
+ const to = n - moved;
+ // The starts that move go back to front when the edit grew the index,
+ // front to back when it shrank it.
+ if (to > from) {
var k = moved;
while (k > 0) {
k -= 1;
- out[to + k] = room[from + k] - len + bytes.len;
+ room[to + k] = room[from + k] - cut + bytes.len;
}
} else for (0..moved) |k| {
- out[to + k] = room[from + k] - len + bytes.len;
+ room[to + k] = room[from + k] - cut + bytes.len;
}
var i = kept;
var at: usize = 0;
while (std.mem.indexOfScalarPos(u8, bytes, at, '\n')) |nl| {
at = nl + 1;
- out[i] = off + at;
+ room[i] = off + at;
i += 1;
}
- if (out.ptr != room.ptr) pardes.memory.freeRaw(gpa, room);
- f.line_starts = out[0..n];
- f.line_cap = out.len;
}
-test "a line index shifted in place equals a rebuilt one" {
+test "a line index shifted in place equals a rebuilt one, and the edit turned round gives the one before back" {
const gpa = std.testing.allocator;
var prng = std.Random.DefaultPrng.init(11);
const r = prng.random();
@@ -668,13 +681,25 @@ test "a line index shifted in place equals a rebuilt one" {
var ins: [12]u8 = undefined;
const ins_len = r.uintAtMost(usize, 12);
for (ins[0..ins_len]) |*c| c.* = "ab\n"[r.uintLessThan(usize, 3)];
- // In place, and every tenth as a replica's core makes it, anew.
- shiftIndex(gpa, &f, at, del, ins[0..ins_len], round % 10 == 0);
+ const before = try gpa.dupe(usize, f.line_starts);
+ defer gpa.free(before);
+ const cut = try gpa.dupe(u8, text.items[at..][0..del]);
+ defer gpa.free(cut);
+ f.content = text.items;
+ shiftIndex(gpa, &f, at, del, ins[0..ins_len]);
try text.replaceRange(gpa, at, del, ins[0..ins_len]);
var fresh: State = .{ .path = &.{}, .content = text.items, .history = undefined };
const want = try lineIndex(gpa, &fresh);
defer gpa.free(want);
try std.testing.expectEqualSlices(usize, want, f.line_starts);
+ // Every tenth turned round in a copy, as a rollback takes it back.
+ if (round % 10 == 0) {
+ const room = try gpa.alloc(usize, indexRoom(&f).len);
+ defer gpa.free(room);
+ @memcpy(room[0..f.line_starts.len], f.line_starts);
+ shiftStarts(room, f.line_starts.len, before.len, at, ins_len, cut);
+ try std.testing.expectEqualSlices(usize, before, room[0..before.len]);
+ }
}
}
@@ -801,8 +826,9 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void {
if (p.panes[id].?.file.?.line_starts.ptr == file.line_starts.ptr) break true;
} else false;
p.gpa.free(file.path);
- // Raw, as every text is freed (a replica's rollback copy pins them,
- // src/rollback.zig, and a safe build's free writes over what it frees).
+ // Raw, as every text is freed (a replica's rollback copy keeps the
+ // buffers it was taken over, src/rollback.zig, and a safe build's free
+ // writes over what it frees).
if (ntwins == 0) pardes.memory.freeRaw(p.gpa, textRoom(file));
if (file.listing) |listing| p.gpa.free(listing.names);
if (file.mini) |*mini| mini.deinit(p.gpa);
@@ -850,13 +876,6 @@ pub fn replace(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8)
return edit(p, f, off, len, bytes, null, true, null);
}
-/// Whether `f`'s bytes must stay as they are, an edit putting a new buffer
-/// in their place: a replica's core, whose rollback copy holds them
-/// (src/rollback.zig), or a text a join is still sending (memory.zig `pin`).
-fn keepsBytes(p: *const Pardes, f: *const State) bool {
- return pardes.rollback.isEpoch(p.gpa) or pardes.memory.isPinned(f.content);
-}
-
/// A replacement that takes out at least this much, and most of the text,
/// keeps the old buffer for its undo (Text.Journal.swap) instead of copying
/// what it takes out.
@@ -882,7 +901,8 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
const own = whole == null and @intFromPtr(bytes_in.ptr) < @intFromPtr(room.ptr) + room.len and @intFromPtr(bytes_in.ptr) + bytes_in.len > @intFromPtr(room.ptr);
const bytes = if (own) p.scratch.allocator().dupe(u8, bytes_in) catch return false else bytes_in;
const swapping = journaled and len >= swap_min and len * 2 >= old_len;
- const keep = whole == null and (swapping or keepsBytes(p, f));
+ // A text a join is still sending stays as it is (memory.zig `pin`).
+ const keep = whole == null and (swapping or pardes.memory.isPinned(f.content));
const fresh: ?[]u8 = whole orelse if (keep) copySpliced(p.gpa, f.content, off, len, bytes) catch return false else null;
if (fresh == null) {
grow(p.gpa, f, new_len) catch return false;
@@ -922,7 +942,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
reportEdit(p, f, off, len, bytes);
if (f.mini) |*mini| mini.deinit(p.gpa);
f.mini = null;
- if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes, pardes.rollback.isEpoch(p.gpa));
+ if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes);
if (journaled and !swapping) f.history.journal.record(p.gpa, f.content, off, len, bytes.len);
// ponytail: a whole-text hash per edit that keeps the saved length; a
// rolling hash when that too matters.
@@ -946,6 +966,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
}
} else {
const buffer = textRoom(f);
+ pardes.rollback.Epoch.wrote(p.gpa, u8, buffer, old_len, new_len, off, buffer[off..][0..len], bytes.len);
const tail = old_len - off - len;
if (bytes.len != len) @memmove(buffer[off + bytes.len ..][0..tail], buffer[off + len ..][0..tail]);
@memcpy(buffer[off..][0..bytes.len], bytes);
diff --git a/src/memory.zig b/src/memory.zig
index 7021a1bb..676793c2 100644
--- a/src/memory.zig
+++ b/src/memory.zig
@@ -344,9 +344,9 @@ pub fn dupeUnfilled(allocator: Allocator, bytes: []const u8) Allocator.Error![]u
}
/// Frees without the safe-build overwrite `free` does: text-sized buffers
-/// freed on every edit, and a text's line index, made anew at every edit
-/// too (and pinned by a replica's rollback copy, src/rollback.zig, which a
-/// safe build's overwrite would spoil).
+/// and line indexes, freed as an edit grows or replaces them (and kept by a
+/// replica's rollback copy, src/rollback.zig, while it holds the core it
+/// was taken over, which a safe build's overwrite would spoil).
pub fn freeRaw(allocator: Allocator, items: anytype) void {
const E = @typeInfo(@TypeOf(items)).pointer.child;
const bytes = std.mem.sliceAsBytes(items);
diff --git a/src/replica.zig b/src/replica.zig
index e104391f..f58a4b27 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -692,7 +692,7 @@ pub const Replica = struct {
for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa);
if (r.t.core) |c| {
const was = Stepping.begin(&r.t, &r.epoch);
- if (r.copy.held) r.copy.restore();
+ if (r.copy.held) _ = r.copy.restore();
c.deinit();
was.end();
}
@@ -872,8 +872,9 @@ pub const Replica = struct {
fn toConfirmed(r: *Replica) void {
const held = r.copy.held;
if (held) {
- r.copy.restore();
+ const whole = r.copy.restore();
rollback.Epoch.select(null);
+ if (!whole) return r.fail("out of memory for a rollback");
// The rows a terminal was read as are no copy's: what the
// speculation built was the epoch's, freed with it, and what
// was there before is of a grid the deltas since moved on
diff --git a/src/rollback.zig b/src/rollback.zig
index 320e7949..46e00721 100644
--- a/src/rollback.zig
+++ b/src/rollback.zig
@@ -11,16 +11,23 @@
//! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a
//! digest covers, so views, buffers, registers, counters and terminal
//! metadata), saving the bytes of every allocation it reaches, the core
-//! struct's own included. A file's text, its line index, its undo
-//! snapshots and a register's texts are pinned instead: an edit puts a
-//! new buffer in their place (File.setContentSpan, Registers.put) and
-//! never writes one, so the copy keeps a reference and the put-off free
-//! keeps it alive.
+//! struct's own included. A file's text and its line index are neither
+//! saved nor pinned (`.journal`): an edit writes them in place, in a
+//! replica's core as in the session's (File.replace), and the epoch
+//! notes what each write into one of then took out (`wrote`), which
+//! putting the copy back takes back, the newest first. The journal's
+//! entries, a register's texts and a file's location rows are pinned
+//! instead: an edit puts a new buffer in their place and never writes
+//! one, so the copy keeps a reference and the put-off free keeps it
+//! alive. So the journal's steps, its `done` and its entries come back
+//! as they were: an entry a speculation merged into, or the steps to
+//! redo it dropped, are its saved bytes and its put-off frees.
//!
-//! Putting the copy back writes each saved allocation's bytes over it, so
-//! every pointer in them points where it did, at what was there; then the
-//! epoch's allocations are freed and its put-off frees forgotten. Letting
-//! the copy go (the speculation was right) makes the put-off frees.
+//! Putting the copy back undoes the writes, then writes each saved
+//! allocation's bytes over it, so every pointer in them points where it
+//! did, at what was there; then the epoch's allocations are freed and its
+//! put-off frees forgotten. Letting the copy go (the speculation was right)
+//! makes the put-off frees.
//!
//! ponytail: an allocation reached only through a field the walk leaves
//! out (a derived cache's own heap, say) is not saved; that cache is the
@@ -61,8 +68,18 @@ pub const Epoch = struct {
born: std.ArrayListUnmanaged(usize) = .empty,
deferred: std.ArrayListUnmanaged(usize) = .empty,
trees: pardes.syntax.HeldTrees = .{ .meta = std.heap.c_allocator },
+ /// The writes in place into a text or a line index that was there when
+ /// the copy was taken (`wrote`), in order, and the bytes they took out.
+ writes: std.ArrayListUnmanaged(Write) = .empty,
+ taken: std.ArrayListUnmanaged(u8) = .empty,
+ /// A write could not be noted (no memory): the copy cannot be put back.
+ lost: bool = false,
const Entry = struct { len: usize, alignment: Alignment, epoch: u32, freed: bool = false };
+ /// At `off` of the text or index at `addr` (`room` long, its elements
+ /// `was` before the write and `len` after), `ins` bytes put over
+ /// `taken[at..][0..cut]`.
+ const Write = struct { index: bool, addr: usize, room: usize, was: usize, len: usize, off: usize, at: usize, cut: usize, ins: usize };
const vtable: Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free };
@@ -78,6 +95,8 @@ pub const Epoch = struct {
e.deferred.deinit(e.meta);
e.trees.born.deinit(e.meta);
e.trees.freed.deinit(e.meta);
+ e.writes.deinit(e.meta);
+ e.taken.deinit(e.meta);
}
fn mine(e: *const Epoch, entry: Entry) bool {
@@ -159,6 +178,7 @@ pub const Epoch = struct {
e.id +%= 1;
if (e.id == 0) e.id = 1;
e.on = true;
+ e.lost = false;
e.born.clearRetainingCapacity();
e.deferred.clearRetainingCapacity();
e.trees.born.clearRetainingCapacity();
@@ -185,6 +205,8 @@ pub const Epoch = struct {
fn end(e: *Epoch) void {
e.on = false;
+ e.writes.clearRetainingCapacity();
+ e.taken.clearRetainingCapacity();
e.born.clearRetainingCapacity();
e.deferred.clearRetainingCapacity();
e.trees.born.clearRetainingCapacity();
@@ -192,6 +214,54 @@ pub const Epoch = struct {
if (pardes.syntax.held_trees == &e.trees) pardes.syntax.held_trees = null;
}
+ /// A write about to be made in place into `room`, a text's bytes or its
+ /// line index (`E` u8 or usize, File.zig `edit`): at the text's offset
+ /// `off`, `ins` bytes over `cut`, the bytes taken out, `room` `was`
+ /// elements long before and `len` after. Noted when `room` was there when the copy was
+ /// taken, so that putting it back takes the write back (`unwrite`); one
+ /// the epoch made goes whole. Nothing outside a replica's core.
+ pub fn wrote(gpa: Allocator, comptime E: type, room: []const E, was: usize, len: usize, off: usize, cut: []const u8, ins: usize) void {
+ if (gpa.vtable != &vtable) return;
+ const e: *Epoch = @ptrCast(@alignCast(gpa.ptr));
+ if (!e.on) return;
+ const entry = e.live.get(@intFromPtr(room.ptr)) orelse return;
+ if (e.mine(entry)) return;
+ const at = e.taken.items.len;
+ e.taken.appendSlice(e.meta, cut) catch {
+ e.lost = true;
+ return;
+ };
+ e.writes.append(e.meta, .{ .index = E == usize, .addr = @intFromPtr(room.ptr), .room = room.len, .was = was, .len = len, .off = off, .at = at, .cut = cut.len, .ins = ins }) catch {
+ e.lost = true;
+ };
+ }
+
+ /// Every write `wrote` noted taken back, the last first: each text and
+ /// index as it was when the copy was taken.
+ fn unwrite(e: *Epoch) void {
+ var i = e.writes.items.len;
+ while (i > 0) {
+ i -= 1;
+ const w = e.writes.items[i];
+ const cut = e.taken.items[w.at..][0..w.cut];
+ if (w.index) {
+ const room = @as([*]usize, @ptrFromInt(w.addr))[0..w.room];
+ pardes.panes.File.shiftStarts(room, w.len, w.was, w.off, w.ins, cut);
+ } else {
+ const room = @as([*]u8, @ptrFromInt(w.addr))[0..w.room];
+ const tail = w.len - w.off - w.ins;
+ @memmove(room[w.off + cut.len ..][0..tail], room[w.off + w.ins ..][0..tail]);
+ @memcpy(room[w.off..][0..cut.len], cut);
+ }
+ }
+ }
+
+ /// Whether a write was noted into the allocation at `addr`.
+ fn wroteAt(e: *const Epoch, addr: usize) bool {
+ for (e.writes.items) |w| if (w.addr == addr) return true;
+ return false;
+ }
+
/// The trees of whichever core is stepped next: one in-process harness
/// steps several (syntax.held_trees is process-wide).
pub fn select(e: ?*Epoch) void {
@@ -199,13 +269,6 @@ pub const Epoch = struct {
}
};
-/// Whether `a` is an epoch's: a replica's core, whose texts a copy holds,
-/// so an edit puts a new buffer in place of one and never writes it
-/// (File.replace).
-pub fn isEpoch(a: Allocator) bool {
- return a.vtable == &Epoch.vtable;
-}
-
pub const Copy = struct {
meta: Allocator = std.heap.c_allocator,
regions: std.ArrayListUnmanaged(Region) = .empty,
@@ -270,13 +333,17 @@ pub const Copy = struct {
c.held = true;
}
- /// The core as it was when the copy was taken.
- pub fn restore(c: *Copy) void {
+ /// The core as it was when the copy was taken: false when a write into
+ /// a text could not be noted (no memory), and that text is not.
+ pub fn restore(c: *Copy) bool {
std.debug.assert(c.held);
+ const whole = !c.epoch.?.lost;
+ c.epoch.?.unwrite();
c.check();
for (c.regions.items) |r| @memcpy(@as([*]u8, @ptrFromInt(r.addr))[0..r.len], c.bytes.items[r.off..][0..r.len]);
c.epoch.?.discard();
c.held = false;
+ return whole;
}
/// The speculation was right: the copy is not wanted.
@@ -287,9 +354,11 @@ pub const Copy = struct {
c.held = false;
}
+ /// Every pinned blob as it was, but one a text became since and was
+ /// written as one (an undo swapped a text back into its buffer).
pub fn check(c: *const Copy) void {
if (comptime !checked) return;
- for (c.pins.items) |pin| if (std.hash.Wyhash.hash(0, @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]) != pin.hash) {
+ for (c.pins.items) |pin| if (!c.epoch.?.wroteAt(pin.addr) and std.hash.Wyhash.hash(0, @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]) != pin.hash) {
const now = @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len];
const entry = c.epoch.?.live.get(pin.addr);
std.debug.print("rollback: {s}, {d} bytes at {x}, pinned, was written in place: now {any}; entry {any}, epoch {d}\n", .{ pin.where, pin.len, pin.addr, now[0..32], entry, c.epoch.?.id });
@@ -372,7 +441,9 @@ pub const Copy = struct {
// queues a step drains (undefined between steps), nor the
// authority's.
if (attr.@"comptime" or comptime (annotated(T, "authority", name) or ((annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)))) continue;
- if (comptime opaqueTo(F)) continue;
+ // A text and its index: written in place, the epoch
+ // notes what each write takes out (`Epoch.wrote`).
+ if (comptime opaqueTo(F) or annotated(T, "journal", name)) continue;
c.where = @typeName(T) ++ "." ++ name;
c.pinnable = comptime walk.blob(T, name);
if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) {
@@ -517,7 +588,7 @@ test "a copy put back is the core as it was; one let go keeps what was done" {
p.pump(.{}) catch {};
const edited = walk.digest(Pardes, p);
try std.testing.expect(edited != before);
- c.restore();
+ try std.testing.expect(c.restore());
try std.testing.expectEqual(before, walk.digest(Pardes, p));
// The same again, let go this time: what was typed stays.
try c.take(&e, p);
@@ -531,6 +602,65 @@ test "a copy put back is the core as it was; one let go keeps what was done" {
try std.testing.expectEqual(@as(usize, 0), e.live.count());
}
+test "a text written in place under a copy comes back byte for byte: typed into its room, undone, cut, and grown past it" {
+ var e: Epoch = .{ .child = std.testing.allocator };
+ defer e.deinit();
+ const gpa = e.allocator();
+ const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
+ p.pump(.{}) catch {};
+ const f = for (p.panes) |slot| {
+ if (slot) |pane| if (pane.file) |*file| break file;
+ } else return error.NoFile;
+ const keys = struct {
+ fn typed(core: *Pardes, text: []const u8) void {
+ for (text) |k| core.update(.{ .key = .{ .cp = k, .text = &.{k} } });
+ core.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ core.pump(.{}) catch {};
+ }
+ };
+ // One edit first, so that the text has room to be typed into.
+ keys.typed(p, "oa");
+ try std.testing.expect(f.cap > f.content.len);
+ _ = try pardes.panes.File.lineIndex(gpa, f);
+ const text = try std.testing.allocator.dupe(u8, f.content);
+ defer std.testing.allocator.free(text);
+ const index = try std.testing.allocator.dupe(usize, f.line_starts);
+ defer std.testing.allocator.free(index);
+ const before = walk.digest(Pardes, p);
+ const room = f.content.ptr;
+ var c: Copy = .{};
+ defer c.deinit();
+ try c.take(&e, p);
+ // The copy pins none of the text.
+ try std.testing.expect(c.pinnedBytes() < text.len);
+ // Typed and undone in place; a line cut, into a buffer of the epoch's,
+ // and typed into past its room.
+ keys.typed(p, "ix y z");
+ try std.testing.expectEqual(room, f.content.ptr);
+ keys.typed(p, "u");
+ try std.testing.expectEqual(room, f.content.ptr);
+ keys.typed(p, "ib");
+ keys.typed(p, "xd");
+ var long: [16 << 10]u8 = undefined;
+ @memset(&long, 'q');
+ long[0] = 'i';
+ keys.typed(p, &long);
+ try std.testing.expect(f.content.ptr != room);
+ // Each write into the text and its index of then, noted.
+ try std.testing.expect(e.writes.items.len >= 4);
+ try std.testing.expect(c.restore());
+ try std.testing.expectEqual(before, walk.digest(Pardes, p));
+ try std.testing.expectEqualStrings(text, f.content);
+ try std.testing.expectEqualSlices(usize, index, f.line_starts);
+ // Let go this time: what was typed in place stays.
+ try c.take(&e, p);
+ keys.typed(p, "iz");
+ c.release();
+ try std.testing.expect(f.content.len > text.len);
+ p.deinit();
+ try std.testing.expectEqual(@as(usize, 0), e.live.count());
+}
+
test "a view's slot, live or drawn outside a step (a replica's own, another live), keeps no pointer a rollback could put old bytes back through" {
// The C heap: a freed chunk comes back to the next allocation its size.
const Read = struct {
@@ -571,7 +701,7 @@ test "a view's slot, live or drawn outside a step (a replica's own, another live
try c.take(&e, p);
// Written in place while the copy is held, then the copy put back.
@memset(other, 'b');
- c.restore();
+ try std.testing.expect(c.restore());
try std.testing.expect(std.mem.allEqual(u8, other, 'b'));
}
if (skipped) return error.SkipZigTest;
@@ -597,7 +727,7 @@ test "a register's texts are pinned, not copied, and come back whole" {
try std.testing.expect(c.pinnedBytes() >= 4 << 20);
// A speculation yanks over it, and is undone.
p.registers.put(gpa, reg, "a word", 0, 1, true);
- c.restore();
+ try std.testing.expect(c.restore());
const back = p.registers.value(reg, 0) orelse return error.Lost;
try std.testing.expectEqual(grid.len, back.len);
try std.testing.expect(std.mem.allEqual(u8, back, 'y'));
@@ -626,7 +756,7 @@ test "a file's location rows are pinned whole, with what they own, and come back
// A speculation's new search drops them, and is undone.
locations.freeRows(gpa, f.location_rows);
f.location_rows = &.{};
- c.restore();
+ try std.testing.expect(c.restore());
try std.testing.expectEqual(@as(usize, 500), f.location_rows.len);
try std.testing.expectEqualStrings("src/rollback.zig", f.location_rows[499].path);
p.deinit();
@@ -663,7 +793,7 @@ test "a rollback leaves the store and Recent as a speculation left them: they li
p.update(.{ .command = try lookAt(&line, "build.zig") });
p.update(.{ .command = "Del" });
p.pump(.{}) catch {};
- c.restore();
+ try std.testing.expect(c.restore());
// What the store and Recent hold is where they say it is.
try std.testing.expectEqual(@as(usize, 2), p.store.closed.items.len);
try std.testing.expect(std.mem.endsWith(u8, p.store.closed.items[1].name, "build.zig"));