summaryrefslogtreecommitdiff
path: root/src/Text.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text.zig')
-rw-r--r--src/Text.zig21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/Text.zig b/src/Text.zig
index 6d0e28c9..650caea1 100644
--- a/src/Text.zig
+++ b/src/Text.zig
@@ -761,14 +761,15 @@ pub const Journal = struct {
/// an undo takes back to `place`. None when the last step changed
/// nothing, its edits undone giving `text` as it is: that step goes on,
/// its place kept, as a snapshot equal to the text was none.
- pub fn mark(j: *Journal, gpa: std.mem.Allocator, text: []const u8, place: Place) void {
+ pub fn mark(j: *Journal, gpa: std.mem.Allocator, text: []const u8, place: Place) error{OutOfMemory}!void {
if (j.done > 0) {
const top = j.steps.items[j.done - 1];
- if (top.before.gen == place.gen and unchanged(gpa, j.entries.items[top.first..][0..top.n], text)) return;
+ if (top.before.gen == place.gen and try unchanged(gpa, j.entries.items[top.first..][0..top.n], text)) return;
}
+ try j.steps.ensureUnusedCapacity(gpa, 1);
j.dropRedo(gpa);
if (j.steps.items.len == memory.limits.undo_max) j.dropOldest(gpa);
- j.steps.append(gpa, .{ .first = j.entries.items.len, .before = place }) catch return;
+ j.steps.appendAssumeCapacity(.{ .first = j.entries.items.len, .before = place });
j.done = j.steps.items.len;
}
@@ -914,7 +915,7 @@ pub const Journal = struct {
/// Whether undoing `entries` gives `text` back as it is. Only the stretch
/// they fell in is undone, in a copy: before their lowest offset nothing
/// changed, nor after the furthest end any reached.
-fn unchanged(gpa: std.mem.Allocator, entries: []const Entry, text: []const u8) bool {
+fn unchanged(gpa: std.mem.Allocator, entries: []const Entry, text: []const u8) error{OutOfMemory}!bool {
if (entries.len == 0) return true;
var grown: isize = 0;
for (entries) |e| grown += @as(isize, @intCast(e.ins)) - @as(isize, @intCast(e.removed.len));
@@ -929,12 +930,12 @@ fn unchanged(gpa: std.mem.Allocator, entries: []const Entry, text: []const u8) b
}
var window: std.ArrayList(u8) = .empty;
defer window.deinit(gpa);
- window.appendSlice(gpa, text[lo..hi]) catch return false;
+ try window.appendSlice(gpa, text[lo..hi]);
var i = entries.len;
while (i > 0) {
i -= 1;
const e = entries[i];
- window.replaceRange(gpa, e.off - lo, e.ins, e.removed) catch return false;
+ try window.replaceRange(gpa, e.off - lo, e.ins, e.removed);
}
return std.mem.eql(u8, window.items, text[lo..hi]);
}
@@ -952,14 +953,14 @@ fn pushHistory(gpa: std.mem.Allocator, slots: []terminal.Snapshot, len: *usize,
/// Record an edit buffer (a terminal's overlay, a tag's own text) as it
/// stands before an edit, once per change of it, and forget what could have
/// been redone. Null is a buffer not made yet.
-pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer) void {
+pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer) error{OutOfMemory}!void {
if (t.ed_undo_len > 0) {
const top = t.ed_undo[t.ed_undo_len - 1];
const same = if (top.ovl) |overlay| if (current) |now| overlay.row == now.row and
overlay.rows == now.rows and std.mem.eql(u8, overlay.text, now.text) else false else current == null;
if (same) return;
}
- const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = gpa.dupe(u8, now.text) catch return } else null;
+ const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null;
pushHistory(gpa, &t.ed_undo, &t.ed_undo_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel });
for (t.ed_redo[0..t.ed_redo_len]) |item| if (item.ovl) |overlay| gpa.free(overlay.text);
t.ed_redo_len = 0;
@@ -969,13 +970,13 @@ pub fn remember(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer)
/// other history and the state to return to comes back, its cursor and
/// selection already restored here. The caller installs its buffer, which
/// it then owns. Null when there is nothing to step to.
-pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer, back: bool) ?terminal.Snapshot {
+pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?terminal.EditBuffer, back: bool) error{OutOfMemory}!?terminal.Snapshot {
const from, const from_len, const to, const to_len = if (back)
.{ &t.ed_undo, &t.ed_undo_len, &t.ed_redo, &t.ed_redo_len }
else
.{ &t.ed_redo, &t.ed_redo_len, &t.ed_undo, &t.ed_undo_len };
if (from_len.* == 0) return null;
- const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = gpa.dupe(u8, now.text) catch return null } else null;
+ const copy: ?terminal.EditBuffer = if (current) |now| .{ .row = now.row, .rows = now.rows, .text = try gpa.dupe(u8, now.text) } else null;
pushHistory(gpa, to, to_len, .{ .ovl = copy, .cur_row = t.cur_row, .cur_col = t.cur_col, .vsel = t.vsel });
from_len.* -= 1;
const back_to = from[from_len.*];