diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-09 03:29:30 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-09 03:30:49 -0300 |
| commit | 84c8209d02f6830db861c3a8dde0c710c88075f0 (patch) | |
| tree | 10f77e3ab1becb5a0af4de152bb8d8e0e9ee0f03 /src/Text.zig | |
| parent | 446292e15d56c9c248dae5d6fde1be3a83ade82e (diff) | |
| download | pardes-84c8209d02f6830db861c3a8dde0c710c88075f0.tar.gz pardes-84c8209d02f6830db861c3a8dde0c710c88075f0.zip | |
idioms: the edit surface fails by name: the undo history (Journal.mark, Text.remember and step, File's, terminal's and edit's pushUndo and undoStep) returns error{OutOfMemory} instead of dropping an undo point, setEditText, enterInsert and insertTab return edit.Error with try where they reported each failure, Registers.put and setYank return error{OutOfMemory} instead of losing a yank, and their other-file callers decide (try under edit.Error, said on the pane by Undo, Redo, Pipe, Edit, Query, keys, look, completion, Incl and Plumb, ENOMEM or null where a 9P write answers, Ask's false); ctl.get and getFile return GetError (fs.ReadError, now named at fs.zig's surface, and InvalidName) for the *anyerror out-parameter and errno, the 9P get, Get and Edit's e mapping it as before; the OOM undo test pins the error; perf.zig's try relabels its harness hash, the three baselines re-keyed to it (their numbers the base's)
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Text.zig')
| -rw-r--r-- | src/Text.zig | 21 |
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.*]; |
