summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:58:54 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:16:28 -0300
commita3e38a8fd01c54b1fab8a7eaf225ac6147c74330 (patch)
treea2f697389827f58bf6c6c69302b789bc770de889 /src/File.zig
parent54b0c95fc5522b5ad48d857bb0a08c253c17ece3 (diff)
downloadpardes-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
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig40
1 files changed, 16 insertions, 24 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]);
}