diff options
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 40 |
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]); } |
