diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:58:54 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:16:28 -0300 |
| commit | a3e38a8fd01c54b1fab8a7eaf225ac6147c74330 (patch) | |
| tree | a2f697389827f58bf6c6c69302b789bc770de889 | |
| parent | 54b0c95fc5522b5ad48d857bb0a08c253c17ece3 (diff) | |
| download | pardes-a3e38a8fd01c54b1fab8a7eaf225ac6147c74330.tar.gz pardes-a3e38a8fd01c54b1fab8a7eaf225ac6147c74330.zip | |
File, terminal, edit: undo and redo are one undoStep(back) each, as Text.step already is, and their callers (normal, builtins, edit_cmd, file_watch, ninep/ctl's pager test, test/panes) say which way
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/File.zig | 40 | ||||
| -rw-r--r-- | src/builtins.zig | 4 | ||||
| -rw-r--r-- | src/edit.zig | 18 | ||||
| -rw-r--r-- | src/edit_cmd.zig | 2 | ||||
| -rw-r--r-- | src/file_watch.zig | 2 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 2 | ||||
| -rw-r--r-- | src/normal.zig | 4 | ||||
| -rw-r--r-- | src/terminal.zig | 14 | ||||
| -rw-r--r-- | test/panes.zig | 10 |
9 files changed, 38 insertions, 58 deletions
diff --git a/src/File.zig b/src/File.zig index d2e8352a..2e12fedf 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1278,32 +1278,24 @@ pub fn pushUndo(p: *Pardes, pane: *Pane) void { pushHistory(p.gpa, history, &history.undo, &history.undo_len, snap); } -pub fn undo(p: *Pardes, pane: *Pane) void { +/// Undo (back) or redo one step: the text, its place and its name go back +/// to the step's, and the text as it is goes on the other history. +pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) void { const f = if (pane.file) |*file| file else return; const history = f.history; - if (history.undo_len == 0) return; + const from, const from_len, const to, const to_len = if (back) + .{ &history.undo, &history.undo_len, &history.redo, &history.redo_len } + else + .{ &history.redo, &history.redo_len, &history.undo, &history.undo_len }; + if (from_len.* == 0) return; const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, history, &history.redo, &history.redo_len, current); - history.undo_len -= 1; - const previous = history.undo[history.undo_len]; - history.bytes -= previous.content.len; // the file's text now - setContent(p, f, previous.content); - restoreName(p, pane, f, previous); - restoreSnap(pane, f, previous); -} - -pub fn redo(p: *Pardes, pane: *Pane) void { - const f = if (pane.file) |*file| file else return; - const history = f.history; - if (history.redo_len == 0) return; - const current = snapshotOf(p.gpa, pane, f) orelse return; - pushHistory(p.gpa, history, &history.undo, &history.undo_len, current); - history.redo_len -= 1; - const next = history.redo[history.redo_len]; - history.bytes -= next.content.len; // the file's text now - setContent(p, f, next.content); - restoreName(p, pane, f, next); - restoreSnap(pane, f, next); + pushHistory(p.gpa, history, to, to_len, current); + from_len.* -= 1; + const snap = from[from_len.*]; + history.bytes -= snap.content.len; // the file's text now + setContent(p, f, snap.content); + restoreName(p, pane, f, snap); + restoreSnap(pane, f, snap); } /// A pane's file deleted on disk: the text stays, and it says so once, @@ -2216,6 +2208,6 @@ test "undo keeps its steps within a byte bound, the newest always" { var sum: usize = 0; for (history.undo[0..history.undo_len]) |snap| sum += snap.content.len; try std.testing.expectEqual(sum, history.bytes); - undo(p, pane); + undoStep(p, pane, true); try std.testing.expectEqualStrings(text[0..1], f.content[0..1]); } diff --git a/src/builtins.zig b/src/builtins.zig index 1afba918..750000ba 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1373,7 +1373,7 @@ pub const Undo = struct { pub fn run(c: Ctx) void { if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Undo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.undo_len == 0) return c.p.setMessage(c.id, "Undo: nothing to undo"); - edit.doUndo(c.p, &c.pane.body); + edit.undoStep(c.p, &c.pane.body, true); } }; @@ -1382,7 +1382,7 @@ pub const Redo = struct { pub fn run(c: Ctx) void { if (@import("edit_cmd.zig").busyOn(c.p, c.id)) return c.p.reportFailure(c.id, "Redo: " ++ @import("edit_cmd.zig").e_busy); if (c.pane.file) |f| if (f.history.redo_len == 0) return c.p.setMessage(c.id, "Redo: nothing to redo"); - edit.doRedo(c.p, &c.pane.body); + edit.undoStep(c.p, &c.pane.body, false); } }; diff --git a/src/edit.zig b/src/edit.zig index 42fc0eaa..5d46b80d 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -1998,20 +1998,14 @@ pub fn pushUndo(p: *Pardes, t: *Text) void { panes.terminal.pushUndo(p, pane); } -pub fn doUndo(p: *Pardes, t: *Text) void { - if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(p, t, true); +/// Undo (back) or redo one step of the text's history: its file's, its +/// terminal overlay's, or a tag's or header's own. +pub fn undoStep(p: *Pardes, t: *Text, back: bool) void { + if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(p, t, back); if (t.what != .body) return; const pane = t.pane() orelse return; - if (pane.file != null) return panes.File.undo(p, pane); - panes.terminal.undo(p, pane); -} - -pub fn doRedo(p: *Pardes, t: *Text) void { - if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(p, t, false); - if (t.what != .body) return; - const pane = t.pane() orelse return; - if (pane.file != null) return panes.File.redo(p, pane); - panes.terminal.redo(p, pane); + if (pane.file != null) return panes.File.undoStep(p, pane, back); + panes.terminal.undoStep(p, pane, back); } /// Undo or redo an edit of a text's own characters; stepping back past its diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig index 876c6157..8bde605b 100644 --- a/src/edit_cmd.zig +++ b/src/edit_cmd.zig @@ -197,7 +197,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void { } } if (res.undo != 0) { - for (0..@abs(res.undo)) |_| if (res.undo > 0) panes.File.undo(p, pane) else panes.File.redo(p, pane); + for (0..@abs(res.undo)) |_| panes.File.undoStep(p, pane, res.undo > 0); return; } pd.res = res; diff --git a/src/file_watch.zig b/src/file_watch.zig index b8f9df39..33c14248 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -763,7 +763,7 @@ test "ordinary file watches still reconcile changes between open and watch" { }; try std.testing.expect(armed and watches[id] != null); try std.testing.expectEqualStrings("changed before watch\n", pane.file.?.content); - pardes.panes.File.undo(core, pane); + pardes.panes.File.undoStep(core, pane, true); try std.testing.expectEqualStrings("opened\n", pane.file.?.content); } diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index d075e24e..fd8f47d5 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -2708,7 +2708,7 @@ test "what a /pager open writes after its directory is the text: plain in the bo try testing.expectEqual(Status.ok, call(p, .{ .tag = 6, .op = .write, .node = Node.of(pn.serial, .body), .data = "more\n" }).reply.status); try testing.expectEqual(@as(usize, 0), f.pager_spans.len); // Undone back to the paged text: its colours come back with it. - panes.File.undo(p, pn); + panes.File.undoStep(p, pn, true); try testing.expectEqualStrings("red /a\nNext\n", f.content); try testing.expectEqual(@as(usize, 2), f.pager_spans.len); diff --git a/src/normal.zig b/src/normal.zig index 8f777ec7..c7fc51a7 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -857,8 +857,8 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void .indent => return edit.normalIndent(p, t, op.count, true), .unindent => return edit.normalIndent(p, t, op.count, false), .comment_toggle => return edit.normalToggleComment(p, t), - .undo => return edit.doUndo(p, t), - .redo => return edit.doRedo(p, t), + .undo => return edit.undoStep(p, t, true), + .redo => return edit.undoStep(p, t, false), }, // Language shortcuts run the same builtins as tags and leader commands. .lsp => |request| return switch (request) { diff --git a/src/terminal.zig b/src/terminal.zig index 29a28e85..e8ab7a09 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -3138,17 +3138,11 @@ pub fn pushUndo(p: *Pardes, pane: *Pane) void { pane.body.remember(p.gpa, pane.ovl); } -pub fn undo(p: *Pardes, pane: *Pane) void { - const back_to = pane.body.step(p.gpa, pane.ovl, true) orelse return; +/// Undo (back) or redo one step of the edit overlay. +pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) void { + const to = pane.body.step(p.gpa, pane.ovl, back) orelse return; if (pane.ovl) |o| p.gpa.free(o.text); - pane.ovl = back_to.ovl; - pane.ensureCursorVisible(); -} - -pub fn redo(p: *Pardes, pane: *Pane) void { - const forward_to = pane.body.step(p.gpa, pane.ovl, false) orelse return; - if (pane.ovl) |o| p.gpa.free(o.text); - pane.ovl = forward_to.ovl; + pane.ovl = to.ovl; pane.ensureCursorVisible(); } diff --git a/test/panes.zig b/test/panes.zig index b111f5d1..f85daab3 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -1213,7 +1213,7 @@ test "terminal edits and undo survive partial history clearing and whole-screen try std.testing.expectEqualStrings("edit one\nedit two", snapshot.ovl.?.text); } try std.testing.expectEqual(@as(usize, 2), pane.terminal.?.vt.screens.active.pages.countTrackedPins()); - panes.terminal.undo(p, pane); + panes.terminal.undoStep(p, pane, true); try std.testing.expectEqualStrings("edit one\nedit two", pane.ovl.?.text); try std.testing.expectEqual(expected, pane.ovl.?.row); } @@ -1430,24 +1430,24 @@ test "file history owns bounded snapshot storage without per-edit bookkeeping al const current = file.content; failing.fail_index = failing.alloc_index; panes.File.pushUndo(p, pane); - panes.File.undo(p, pane); + panes.File.undoStep(p, pane, true); try std.testing.expectEqual(current.ptr, file.content.ptr); try std.testing.expectEqual(pardes.memory.limits.undo_max, history.undo_len); try std.testing.expectEqual(@as(usize, 0), history.redo_len); failing.fail_index = std.math.maxInt(usize); const before = failing.alloc_index; - panes.File.undo(p, pane); + panes.File.undoStep(p, pane, true); try std.testing.expectEqual(before + 2, failing.alloc_index); try std.testing.expectEqual(@as(usize, 1), file.line_starts.len); try std.testing.expectEqual(@as(usize, 1), history.redo_len); const undone = file.content; failing.fail_index = failing.alloc_index; - panes.File.redo(p, pane); + panes.File.undoStep(p, pane, false); try std.testing.expectEqual(undone.ptr, file.content.ptr); try std.testing.expectEqual(@as(usize, 1), history.redo_len); failing.fail_index = std.math.maxInt(usize); const before_redo = failing.alloc_index; - panes.File.redo(p, pane); + panes.File.undoStep(p, pane, false); try std.testing.expectEqual(before_redo + 2, failing.alloc_index); try std.testing.expectEqual(pardes.memory.limits.undo_max, history.undo_len); try std.testing.expectEqual(@as(usize, 0), history.redo_len); |
