summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig262
1 files changed, 158 insertions, 104 deletions
diff --git a/src/File.zig b/src/File.zig
index 7aac4cb9..92e5b210 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -27,33 +27,10 @@ const Text = panes.Text;
const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2;
const long_paint: usize = 16 << 10;
-/// Content and primary selection at one file edit boundary. Keeping only the
-/// primary avoids putting Text.max_selections ranges in every history entry.
-pub const Snapshot = struct {
- pub const replica = .{ .blob = .{"content"} };
- content: []u8,
- cur_row: i32,
- cur_col: i32,
- vsel: Text.CharSel,
- /// The pane's name then, as History.gen counts its renames, and whether
- /// and as what the text was saved under it: an undo across a load into
- /// the pane under another name (Get file, Edit's e) puts the name back
- /// with the text, and the dirty state that went with them, as acme's
- /// undo does. No allocation a step: a name is kept once, at its rename.
- gen: u32 = 0,
- clean: bool = false,
- saved_hash: ?u64 = null,
- saved_len: ?usize = null,
-
- fn deinit(s: Snapshot, gpa: std.mem.Allocator) void {
- pardes.memory.freeRaw(gpa, s.content);
- }
-};
-
-/// The pane's text, place and name now, for an undo or redo step.
-fn snapshotOf(gpa: std.mem.Allocator, pane: *const Pane, f: *const State) ?Snapshot {
+/// Where the pane is in its text and what the text is saved as now: an
+/// undo step's edge (Text.Place), kept once, never a copy of the text.
+fn placeOf(pane: *const Pane, f: *const State) Text.Place {
return .{
- .content = pardes.memory.dupeUnfilled(gpa, f.content) catch return null,
.cur_row = pane.body.cur_row,
.cur_col = pane.body.cur_col,
.vsel = pane.body.vsel,
@@ -73,8 +50,9 @@ pub fn noteRenamed(gpa: std.mem.Allocator, history: *History, old: []u8) void {
}
/// After a step's text went in: if it was under another name, that name
-/// back, and the step's saved state with it (Get file, Edit's e).
-fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void {
+/// back, and the step's saved state with it (Get file, Edit's e), as acme's
+/// undo puts them back with the text.
+fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Text.Place) void {
const history = f.history;
if (snap.gen == history.gen) return;
// The latest name kept for that generation.
@@ -103,33 +81,24 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void {
if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } });
}
+/// A file's undo, which its twins (Zerox) share: its edits as a journal
+/// (Text.Journal), and the names it had.
pub const History = struct {
- pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } };
- undo: [limits.undo_max]Snapshot = undefined,
- undo_len: usize = 0,
- redo: [limits.undo_max]Snapshot = undefined,
- redo_len: usize = 0,
- /// The text `undo` and `redo` hold, kept near limits.undo_bytes.
- bytes: usize = 0,
-
+ journal: Text.Journal = .{},
/// The names the file had, each with the rename generation it ended
- /// (Snapshot.gen), and the generation now.
+ /// (Text.Place.gen), and the generation now: no allocation a step, a
+ /// name is kept once, at its rename.
names: std.ArrayList(struct { gen: u32, path: []u8 }) = .empty,
gen: u32 = 0,
pub fn create(gpa: std.mem.Allocator) !*History {
const history = try gpa.create(History);
- history.undo_len = 0;
- history.redo_len = 0;
- history.bytes = 0;
- history.names = .empty;
- history.gen = 0;
+ history.* = .{};
return history;
}
pub fn destroy(history: *History, gpa: std.mem.Allocator) void {
- for (history.undo[0..history.undo_len]) |snap| snap.deinit(gpa);
- for (history.redo[0..history.redo_len]) |snap| snap.deinit(gpa);
+ history.journal.deinit(gpa);
for (history.names.items) |name| gpa.free(name.path);
history.names.deinit(gpa);
gpa.destroy(history);
@@ -870,7 +839,7 @@ pub fn setContent(p: *Pardes, f: *State, new: []u8) void {
/// caller made anyway, as the text's buffer.
pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
const span = span_in orelse spanOf(f.content, new);
- _ = edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], new);
+ _ = edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], new, true);
}
/// The one change a text takes: the `len` bytes at `off` replaced by
@@ -878,7 +847,7 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
/// them, never a copy of the text. False with no memory for it, and
/// nothing changed.
pub fn replace(p: *Pardes, f: *State, off: usize, len: usize, bytes: []const u8) bool {
- return edit(p, f, off, len, bytes, null);
+ return edit(p, f, off, len, bytes, null, true);
}
/// Whether `f`'s bytes must stay as they are, an edit putting a new buffer
@@ -891,9 +860,10 @@ fn keepsBytes(p: *const Pardes, f: *const State) bool {
/// `replace`, or with `whole` that text's new buffer, `bytes` its middle,
/// taken as it is. Its room comes first, so an edit with no memory for it
/// changes nothing; then what holds a place in the text moves through the
-/// edit (twins, other views, readers of its events, its line index), the
-/// bytes go in and it is a new revision.
-fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, whole: ?[]u8) bool {
+/// edit (twins, other views, readers of its events, its line index, the
+/// journal when `journaled`: an undo's own edits are not), the bytes go in
+/// and it is a new revision.
+fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, whole: ?[]u8, journaled: bool) bool {
const old_len = f.content.len;
const new_len = old_len - len + bytes_in.len;
const span: Span = .{ .head = off, .tail = old_len - off - len };
@@ -945,6 +915,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
if (f.mini) |*mini| mini.deinit(p.gpa);
f.mini = null;
if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes, pardes.rollback.isEpoch(p.gpa));
+ if (journaled) f.history.journal.record(p.gpa, f.content, off, len, bytes.len);
// ponytail: a whole-text hash per edit that keeps the saved length; a
// rolling hash when that too matters.
if (f.revision == f.saved_revision or f.saved_hash == null) {
@@ -1328,7 +1299,7 @@ test "a pager's text is a diff when it holds a diff header or a unified hunk" {
try std.testing.expect(!looksLikeDiff("commit abc\nAuthor: x\n\n plain log\n"));
}
-pub fn restoreSnap(pane: *Pane, f: *State, snap: Snapshot) void {
+pub fn restoreSnap(pane: *Pane, f: *State, snap: Text.Place) void {
const n = nlines(pane.gpa, f);
const row: usize = @min(@as(usize, @intCast(@max(0, snap.cur_row))), n - 1);
const llen = modal.lineSlice(f.content, row).len;
@@ -1340,56 +1311,140 @@ pub fn restoreSnap(pane: *Pane, f: *State, snap: Snapshot) void {
pane.ensureCursorVisible();
}
-/// Bounded in steps and in bytes: the oldest go first, though the one
-/// pushed always stays.
-fn pushHistory(gpa: std.mem.Allocator, history: *History, slots: []Snapshot, len: *usize, snap: Snapshot) void {
- while (len.* > 0 and (len.* == slots.len or history.bytes + snap.content.len > limits.undo_bytes)) {
- history.bytes -= slots[0].content.len;
- slots[0].deinit(gpa);
- std.mem.copyForwards(Snapshot, slots[0 .. len.* - 1], slots[1..len.*]);
- len.* -= 1;
- }
- slots[len.*] = snap;
- len.* += 1;
- history.bytes += snap.content.len;
-}
-
+/// An undo step's edge, made before an edit (Text.Journal.mark): the edits
+/// after it are one step back to the pane's place now.
pub fn pushUndo(p: *Pardes, pane: *Pane) void {
const f = if (pane.file) |*file| file else return;
- const history = f.history;
- if (history.undo_len > 0) {
- const last = history.undo[history.undo_len - 1];
- if (std.mem.eql(u8, last.content, f.content) and last.gen == history.gen) return;
- }
- const snap = snapshotOf(p.gpa, pane, f) orelse return;
- for (history.redo[0..history.redo_len]) |item| {
- history.bytes -= item.content.len;
- item.deinit(p.gpa);
- }
- history.redo_len = 0;
- pushHistory(p.gpa, history, &history.undo, &history.undo_len, snap);
+ f.history.journal.mark(p.gpa, f.content, placeOf(pane, f));
}
-/// 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.
+/// Undo (back) or redo one step: its edits turned round in the text, and the
+/// place and name it began at (or, redone, was undone from) back.
pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) void {
const f = if (pane.file) |*file| file else return;
- const history = f.history;
- 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, 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);
+ const j = &f.history.journal;
+ if (if (back) j.done == 0 else j.done == j.steps.items.len) return;
+ const at = if (back) j.done - 1 else j.done;
+ const now = placeOf(pane, f);
+ const step = j.steps.items[at];
+ if (!turn(p, f, j.entries.items[step.first..][0..step.n], back)) return;
+ const s = &j.steps.items[at];
+ if (back) {
+ s.after = now;
+ j.done -= 1;
+ } else {
+ s.before = now;
+ j.done += 1;
+ }
+ const to = if (back) s.before else s.after;
+ restoreName(p, pane, f, to);
+ restoreSnap(pane, f, to);
}
+/// A step's entries turned round in `f`'s text: each one's bytes put back
+/// and the bytes it had put in kept in its place, an undo's last first and
+/// a redo's first first. One entry is turned in place, its bytes alone
+/// moved; several in a copy of the text whose gap goes from one to the next
+/// (`Gap`), put in as one edit; none is an edit that changes nothing, as
+/// the snapshot equal to the text was put in once. False with no memory,
+/// nothing changed.
+fn turn(p: *Pardes, f: *State, entries: []Text.Entry, back: bool) bool {
+ const j = &f.history.journal;
+ if (entries.len == 0) return edit(p, f, f.content.len, 0, "", null, false);
+ if (entries.len == 1) {
+ const e = &entries[0];
+ const put = pardes.memory.dupeUnfilled(p.gpa, f.content[e.off..][0..e.ins]) catch return false;
+ if (!edit(p, f, e.off, e.ins, e.removed, null, false)) {
+ pardes.memory.freeRaw(p.gpa, put);
+ return false;
+ }
+ j.bytes = j.bytes - e.removed.len + put.len;
+ e.ins = e.removed.len;
+ pardes.memory.freeRaw(p.gpa, e.removed);
+ e.removed = put;
+ return true;
+ }
+ // The gap's room: what the entries put back past what they take out.
+ var room: usize = 0;
+ for (entries) |e| room += e.removed.len -| e.ins;
+ var gap = Gap.init(p.gpa, f.content, room) catch return false;
+ const puts = p.scratch.allocator().alloc([]u8, entries.len) catch {
+ pardes.memory.freeRaw(p.gpa, gap.buf);
+ return false;
+ };
+ @memset(puts, &.{});
+ for (0..entries.len) |k| {
+ const i = if (back) entries.len - 1 - k else k;
+ const e = entries[i];
+ puts[i] = gap.replace(p.gpa, e.off, e.ins, e.removed) catch {
+ for (puts) |put| pardes.memory.freeRaw(p.gpa, put);
+ pardes.memory.freeRaw(p.gpa, gap.buf);
+ return false;
+ };
+ }
+ const new = gap.whole();
+ const span = spanOf(f.content, new);
+ if (!edit(p, f, span.head, f.content.len - span.head - span.tail, new[span.head .. new.len - span.tail], gap.buf, false)) {
+ for (puts) |put| pardes.memory.freeRaw(p.gpa, put);
+ pardes.memory.freeRaw(p.gpa, gap.buf);
+ return false;
+ }
+ for (entries, puts) |*e, put| {
+ j.bytes = j.bytes - e.removed.len + put.len;
+ e.ins = e.removed.len;
+ pardes.memory.freeRaw(p.gpa, e.removed);
+ e.removed = put;
+ }
+ return true;
+}
+
+/// A copy of a text with a gap where it was last written (`turn`): moving
+/// the gap costs the bytes between two places, never the text, so a step of
+/// many entries (a key typed at many ranges) is turned in one pass.
+const Gap = struct {
+ /// The text is `buf[0..lo]` then `buf[hi..]`.
+ buf: []u8,
+ lo: usize,
+ hi: usize,
+
+ fn init(gpa: std.mem.Allocator, bytes: []const u8, room: usize) !Gap {
+ const buf = try pardes.memory.unfilled(gpa, bytes.len + room);
+ @memcpy(buf[0..bytes.len], bytes);
+ return .{ .buf = buf, .lo = bytes.len, .hi = buf.len };
+ }
+
+ fn move(g: *Gap, at: usize) void {
+ if (at < g.lo) {
+ const n = g.lo - at;
+ @memmove(g.buf[g.hi - n .. g.hi], g.buf[at..g.lo]);
+ g.lo = at;
+ g.hi -= n;
+ } else if (at > g.lo) {
+ const n = at - g.lo;
+ @memmove(g.buf[g.lo..][0..n], g.buf[g.hi..][0..n]);
+ g.lo += n;
+ g.hi += n;
+ }
+ }
+
+ /// The `len` bytes at `at` replaced by `bytes` (in the room `init` was
+ /// given); what they were, copied out.
+ fn replace(g: *Gap, gpa: std.mem.Allocator, at: usize, len: usize, bytes: []const u8) ![]u8 {
+ g.move(at);
+ const out = try pardes.memory.dupeUnfilled(gpa, g.buf[g.hi..][0..len]);
+ g.hi += len;
+ @memcpy(g.buf[g.lo..][0..bytes.len], bytes);
+ g.lo += bytes.len;
+ return out;
+ }
+
+ /// The text whole, at the buffer's start.
+ fn whole(g: *Gap) []u8 {
+ g.move(g.lo + (g.buf.len - g.hi));
+ return g.buf[0..g.lo];
+ }
+};
+
/// A pane's file deleted on disk: the text stays, and it says so once,
/// `<file> deleted on disk (Save writes it again)`, logged `changed
/// <serial> deleted`.
@@ -1441,13 +1496,7 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void {
pushUndo(p, pane);
setContent(p, f, new);
f.saved_revision = f.revision;
- // restoreSnap only consumes cursor/selection from this synthetic snapshot.
- restoreSnap(pane, f, .{
- .content = undefined,
- .cur_row = pane.body.cur_row,
- .cur_col = pane.body.cur_col,
- .vsel = pane.body.vsel,
- });
+ restoreSnap(pane, f, .{ .cur_row = pane.body.cur_row, .cur_col = pane.body.cur_col, .vsel = pane.body.vsel });
}
/// re-highlight the visible window of any file whose syntax went stale
@@ -2294,12 +2343,17 @@ test "undo keeps its steps within a byte bound, the newest always" {
setContent(p, f, try gpa.dupe(u8, text));
}
pushUndo(p, pane);
- const history = f.history;
- try std.testing.expect(history.undo_len > 0 and history.undo_len < 8);
- try std.testing.expect(history.bytes <= limits.undo_bytes + step);
+ const j = &f.history.journal;
+ try std.testing.expect(j.done > 0 and j.done < 8);
+ try std.testing.expect(j.bytes <= limits.undo_bytes + step + @sizeOf(Text.Entry));
var sum: usize = 0;
- for (history.undo[0..history.undo_len]) |snap| sum += snap.content.len;
- try std.testing.expectEqual(sum, history.bytes);
+ for (j.entries.items) |e| sum += e.removed.len + @sizeOf(Text.Entry);
+ try std.testing.expectEqual(sum, j.bytes);
undoStep(p, pane, true);
try std.testing.expectEqualStrings(text[0..1], f.content[0..1]);
+ // Back as far as the bound kept: each step's text whole again.
+ while (j.done > 0) {
+ undoStep(p, pane, true);
+ try std.testing.expect(std.mem.allEqual(u8, f.content, f.content[0]));
+ }
}