diff options
Diffstat (limited to 'src/Text.zig')
| -rw-r--r-- | src/Text.zig | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/src/Text.zig b/src/Text.zig index 1e419732..6d0e28c9 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -703,10 +703,16 @@ pub fn clampCursor(t: *Text, text: []const u8, row0: i32) void { pub const Entry = struct { /// Never written once made: a new one takes its place and the old is /// freed raw (a replica's rollback copy pins it, a digest hashes it once). - pub const replica = .{ .blob = .{"removed"} }; + pub const replica = .{ .blob = .{"removed"}, .derived = .{"room"} }; off: usize, removed: []u8, ins: usize, + /// A text replaced whole (Get, Edit's e, a Restore, a 9P > body write): + /// `removed` is the text as it was, its own buffer kept by pointer, not + /// a copy (`Journal.swap`), and an undo swaps the buffers back. + swap: bool = false, + /// The bytes `removed`'s buffer has room for (0: as many as it holds). + room: usize = 0, }; /// Where a text was at an undo step's edge: its cursor and selection, and @@ -745,7 +751,7 @@ pub const Journal = struct { bytes: usize = 0, pub fn deinit(j: *Journal, gpa: std.mem.Allocator) void { - for (j.entries.items) |e| memory.freeRaw(gpa, e.removed); + for (j.entries.items) |e| free(gpa, e); j.entries.deinit(gpa); j.steps.deinit(gpa); j.* = .{}; @@ -779,7 +785,7 @@ pub const Journal = struct { j.dropRedo(gpa); if (j.done == 0) return; const top = &j.steps.items[j.done - 1]; - if (top.n > 0) { + if (top.n > 0 and !j.entries.items[j.entries.items.len - 1].swap) { const last = &j.entries.items[j.entries.items.len - 1]; if (off >= last.off and off + len <= last.off + last.ins) { last.ins = last.ins - len + ins; @@ -818,6 +824,37 @@ pub const Journal = struct { j.bound(gpa); } + /// A text replaced whole, its old buffer `room` (holding `len` bytes) + /// kept in the last step by pointer where `record` would copy it, so + /// neither the edit nor its undo moves a byte; counted in the bound in + /// bytes by its entry alone. Two are kept a text: a third drops the + /// oldest steps until two are left, and answers true, that the undo + /// before them is gone. With no step to go in it is freed. + pub fn swap(j: *Journal, gpa: std.mem.Allocator, room: []u8, len: usize, ins: usize) bool { + j.dropRedo(gpa); + const kept: Entry = .{ .off = 0, .removed = room[0..len], .ins = ins, .swap = true, .room = room.len }; + if (j.done == 0) { + free(gpa, kept); + return false; + } + j.entries.append(gpa, kept) catch { + free(gpa, kept); + j.deinit(gpa); + return false; + }; + j.steps.items[j.done - 1].n += 1; + j.bytes += @sizeOf(Entry); + var dropped = false; + while (j.done > 1) { + var swaps: usize = 0; + for (j.entries.items) |e| swaps += @intFromBool(e.swap); + if (swaps <= 2) break; + j.dropOldest(gpa); + dropped = true; + } + return dropped; + } + /// The oldest steps dropped while over the bound in bytes, the last kept. fn bound(j: *Journal, gpa: std.mem.Allocator) void { while (j.bytes > memory.limits.undo_bytes and j.done > 1) j.dropOldest(gpa); @@ -827,8 +864,8 @@ pub const Journal = struct { if (j.done == j.steps.items.len) return; const first = j.steps.items[j.done].first; for (j.entries.items[first..]) |e| { - j.bytes -= e.removed.len + @sizeOf(Entry); - memory.freeRaw(gpa, e.removed); + j.bytes -= held(e); + free(gpa, e); } j.entries.shrinkRetainingCapacity(first); j.steps.shrinkRetainingCapacity(j.done); @@ -837,8 +874,8 @@ pub const Journal = struct { fn dropOldest(j: *Journal, gpa: std.mem.Allocator) void { const n = j.steps.items[0].n; for (j.entries.items[0..n]) |e| { - j.bytes -= e.removed.len + @sizeOf(Entry); - memory.freeRaw(gpa, e.removed); + j.bytes -= held(e); + free(gpa, e); } const rest = j.entries.items.len - n; @memmove(j.entries.items[0..rest], j.entries.items[n..]); @@ -847,6 +884,31 @@ pub const Journal = struct { for (j.steps.items) |*s| s.first -= n; j.done -= 1; } + + /// Step `at` made one entry, its first: the rest freed and gone (`turn` + /// swapped the whole text back, and its first holds what they made). + pub fn fold(j: *Journal, gpa: std.mem.Allocator, at: usize) void { + const folded = &j.steps.items[at]; + if (folded.n < 2) return; + const rest = j.entries.items[folded.first + 1 ..][0 .. folded.n - 1]; + for (rest) |e| { + j.bytes -= held(e); + free(gpa, e); + } + j.entries.replaceRangeAssumeCapacity(folded.first + 1, rest.len, &.{}); + for (j.steps.items[at + 1 ..]) |*s| s.first -= rest.len; + folded.n = 1; + } + + /// What an entry counts in the bound: its copied bytes and its own size. + pub fn held(e: Entry) usize { + return @sizeOf(Entry) + if (e.swap) 0 else e.removed.len; + } + + /// An entry's bytes freed, a swap's whole buffer. + pub fn free(gpa: std.mem.Allocator, e: Entry) void { + memory.freeRaw(gpa, e.removed.ptr[0..@max(e.room, e.removed.len)]); + } }; /// Whether undoing `entries` gives `text` back as it is. Only the stretch |
