From 99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 22:03:14 -0300 Subject: core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/recent.zig | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 core/recent.zig (limited to 'core/recent.zig') diff --git a/core/recent.zig b/core/recent.zig new file mode 100644 index 00000000..5cccf18b --- /dev/null +++ b/core/recent.zig @@ -0,0 +1,282 @@ +//! The files opened lately, most recent first, those since closed too, as +//! `Recent` lists them and a jump to a closed pane reopens them: a path +//! and where the dot was when it closed. Kept across sessions, and shared +//! by every session of the user's, in Recent's file (Store.zig) when the +//! host gives one: written whole at the end of a step that changed it, and +//! read back under this session's changes (`merge`). acme has nothing like +//! it (its dump and Load are the nearest): a file closed by accident, its +//! path forgotten, is found here again. + +const std = @import("std"); + +/// At most this many files are remembered, the oldest dropped first. +pub const cap = 200; + +pub const Entry = struct { + path: []u8, + /// Where its dot was when it last closed (1-based; 0 none). + line: u32 = 0, + col: u32 = 0, + /// The selection's other end then (its anchor), 0 when there was none. + aline: u32 = 0, + acol: u32 = 0, + /// Open in a pane now (opened, not closed since): never the one the + /// cap drops. ponytail: a pane renamed away leaves its old path marked + /// open until the session ends; kept rather than tracked by pane. + open: bool = false, + /// Changed here since the file was last written: it goes over what the + /// file says when it is read again, and is written at the end of the + /// step (Store.flush). + changed: bool = false, +}; + +pub const List = struct { + entries: std.ArrayList(Entry) = .empty, + /// Some entry is `changed`. + dirty: bool = false, + + pub fn deinit(l: *List, gpa: std.mem.Allocator) void { + for (l.entries.items) |e| gpa.free(e.path); + l.entries.deinit(gpa); + } + + fn find(l: *const List, path: []const u8) ?usize { + for (l.entries.items, 0..) |e, i| if (std.mem.eql(u8, e.path, path)) return i; + return null; + } + + /// `path` opened: first in the list, once, keeping the dot it closed at. + pub fn opened(l: *List, gpa: std.mem.Allocator, path: []const u8) void { + if (path.len == 0) return; + var entry: Entry = if (l.find(path)) |i| l.entries.orderedRemove(i) else .{ + .path = gpa.dupe(u8, path) catch return, + }; + entry.open = true; + entry.changed = true; + l.dropOldest(gpa); + l.entries.insert(gpa, 0, entry) catch { + gpa.free(entry.path); + entry = undefined; + return; + }; + l.dirty = true; + } + + /// `path` closed with its dot at `line`:`col` and its selection's anchor + /// at `aline`:`acol` (0 0 for none): where a reopen lands. Its place in + /// the list is its opening's. + pub fn closed(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void { + const i = l.find(path) orelse found: { + l.opened(gpa, path); + // Out of memory for it: nothing to note the dot on. + break :found l.find(path) orelse return; + }; + l.entries.items[i].line = line; + l.entries.items[i].col = col; + l.entries.items[i].aline = aline; + l.entries.items[i].acol = acol; + l.entries.items[i].open = false; + l.entries.items[i].changed = true; + l.dirty = true; + } + + /// Full: the oldest closed file goes, to make room for one more; an + /// open one never does. + fn dropOldest(l: *List, gpa: std.mem.Allocator) void { + if (l.entries.items.len < cap) return; + var i = l.entries.items.len; + while (i > 0) : (i -= 1) if (!l.entries.items[i - 1].open) { + gpa.free(l.entries.orderedRemove(i - 1).path); + return; + }; + } + + /// The list again from its file's `text`, which other sessions write + /// too: this session's changes not written yet first (they are the + /// newest), then the file's lines in their order, then the files open + /// here that the file has lost (another session's cap dropped them). + /// Whether a file is open HERE stays as it was. A line is matched only + /// against those two, a few: the file holds each path once. + pub fn merge(l: *List, gpa: std.mem.Allocator, text: []const u8) void { + var old = l.entries; + l.entries = .empty; + var opens: std.ArrayList(Entry) = .empty; + defer { + for (opens.items) |e| gpa.free(e.path); + opens.deinit(gpa); + } + for (old.items) |e| { + if (e.changed) { + l.entries.append(gpa, e) catch gpa.free(e.path); + } else if (e.open) { + opens.append(gpa, e) catch gpa.free(e.path); + } else gpa.free(e.path); + } + old.deinit(gpa); + const changed = l.entries.items.len; + var lines = std.mem.splitScalar(u8, text, '\n'); + next: while (lines.next()) |line| { + if (l.entries.items.len >= cap) break; + const e = parseLine(line) orelse continue; + for (l.entries.items[0..changed]) |c| if (std.mem.eql(u8, c.path, e.path)) continue :next; + const at = for (opens.items, 0..) |o, k| { + if (std.mem.eql(u8, o.path, e.path)) break k; + } else null; + var entry: Entry = if (at) |k| opens.orderedRemove(k) else .{ .path = gpa.dupe(u8, e.path) catch break }; + entry.line = e.nums[0]; + entry.col = e.nums[1]; + entry.aline = e.nums[2]; + entry.acol = e.nums[3]; + l.entries.append(gpa, entry) catch { + gpa.free(entry.path); + break; + }; + } + l.entries.appendSlice(gpa, opens.items) catch return; + opens.clearRetainingCapacity(); + } + + /// This session's changes first, in their order: written so, they are + /// where a read of the file puts them too (`merge`), above the rest. + pub fn changedFirst(l: *List) void { + var w: usize = 0; + for (0..l.entries.items.len) |i| if (l.entries.items[i].changed) { + const e = l.entries.items[i]; + std.mem.copyBackwards(Entry, l.entries.items[w + 1 .. i + 1], l.entries.items[w..i]); + l.entries.items[w] = e; + w += 1; + }; + } + + /// A copy, for the session a Restore makes: its files closed until it + /// opens them again. Out of memory, what was copied so far. + pub fn clone(l: *const List, gpa: std.mem.Allocator) List { + var copy: List = .{ .dirty = l.dirty }; + for (l.entries.items) |e| { + const path = gpa.dupe(u8, e.path) catch break; + var kept = e; + kept.path = path; + kept.open = false; + copy.entries.append(gpa, kept) catch { + gpa.free(path); + break; + }; + } + return copy; + } + + pub fn get(l: *const List, path: []const u8) ?Entry { + return l.entries.items[l.find(path) orelse return null]; + } + + /// An entry as a line of the file, ` `. + pub fn writeEntry(e: Entry, w: *std.Io.Writer) std.Io.Writer.Error!void { + // A name with a newline in it would read back as two lines: not kept. + if (std.mem.indexOfScalar(u8, e.path, '\n') == null) + try w.print("{d} {d} {d} {d} {s}\n", .{ e.line, e.col, e.aline, e.acol, e.path }); + } + + /// A line of the file read back: ` `. + pub fn parseLine(line: []const u8) ?struct { nums: [4]u32, path: []const u8 } { + var nums: [4]u32 = @splat(0); + var rest = line; + for (&nums) |*n| { + const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse return null; + n.* = std.fmt.parseInt(u32, rest[0..sp], 10) catch return null; + rest = rest[sp + 1 ..]; + } + if (rest.len == 0 or rest[0] != '/') return null; + return .{ .nums = nums, .path = rest }; + } +}; + +test "the list keeps a path once, most recent first, its dot from closing, capped" { + const gpa = std.testing.allocator; + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/a"); + l.opened(gpa, "/b"); + l.closed(gpa, "/a", 7, 3, 0, 0); + l.opened(gpa, "/a"); + try std.testing.expectEqualStrings("/a", l.entries.items[0].path); + try std.testing.expectEqual(@as(usize, 2), l.entries.items.len); + try std.testing.expectEqual(@as(u32, 7), l.get("/a").?.line); + var buf: [256]u8 = undefined; + var w = std.Io.Writer.fixed(&buf); + for (l.entries.items) |e| try List.writeEntry(e, &w); + try std.testing.expectEqualStrings("7 3 0 0 /a\n0 0 0 0 /b\n", w.buffered()); + // Each a line of the file, read back. + const back = List.parseLine("7 3 0 0 /a").?; + try std.testing.expectEqual(@as(u32, 7), back.nums[0]); + try std.testing.expectEqualStrings("/a", back.path); + try std.testing.expect(List.parseLine("junk") == null); + for (0..cap + 5) |i| { + var name: [16]u8 = undefined; + const path = std.fmt.bufPrint(&name, "/f{d}", .{i}) catch unreachable; + l.opened(gpa, path); + l.closed(gpa, path, 1, 1, 0, 0); + } + try std.testing.expectEqual(@as(usize, cap), l.entries.items.len); +} + +test "the cap drops the oldest closed file, never an open one" { + const gpa = std.testing.allocator; + var l: List = .{}; + defer l.deinit(gpa); + var name: [16]u8 = undefined; + // The oldest is open; the next is closed. + l.opened(gpa, "/open"); + l.opened(gpa, "/shut"); + l.closed(gpa, "/shut", 1, 1, 0, 0); + for (2..cap) |i| l.opened(gpa, std.fmt.bufPrint(&name, "/f{d}", .{i}) catch unreachable); + try std.testing.expectEqual(@as(usize, cap), l.entries.items.len); + l.opened(gpa, "/new"); + try std.testing.expectEqual(@as(usize, cap), l.entries.items.len); + try std.testing.expect(l.get("/open") != null); + try std.testing.expect(l.get("/shut") == null); +} + +test "a merge puts this session's changes over the file's list, keeps what is open here, and drops what the file lost" { + const gpa = std.testing.allocator; + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/mine"); + l.opened(gpa, "/kept"); + l.closed(gpa, "/kept", 4, 2, 0, 0); + l.opened(gpa, "/gone"); + l.closed(gpa, "/gone", 1, 1, 0, 0); + // Written: nothing changed since. + for (l.entries.items) |*e| e.changed = false; + // Another session closed /kept at 9:1 and opened /theirs; its cap lost + // /gone and /mine, which is open here. + l.closed(gpa, "/new", 3, 3, 0, 0); + l.merge(gpa, "0 0 0 0 /theirs\n9 1 0 0 /kept\nnot a line\n"); + var order: [4][]const u8 = undefined; + for (l.entries.items, 0..) |e, i| order[i] = e.path; + try std.testing.expectEqual(@as(usize, 4), l.entries.items.len); + try std.testing.expectEqualStrings("/new", order[0]); + try std.testing.expectEqualStrings("/theirs", order[1]); + try std.testing.expectEqualStrings("/kept", order[2]); + try std.testing.expectEqualStrings("/mine", order[3]); + try std.testing.expectEqual(@as(u32, 9), l.get("/kept").?.line); + try std.testing.expect(l.get("/mine").?.open); + try std.testing.expect(!l.get("/theirs").?.open); +} + +test "a merge that runs out of memory at any allocation leaks nothing" { + var fail_index: usize = 0; + while (fail_index < 64) : (fail_index += 1) { + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = std.math.maxInt(usize) }); + const gpa = failing.allocator(); + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/mine"); + l.opened(gpa, "/kept"); + l.closed(gpa, "/kept", 4, 2, 0, 0); + for (l.entries.items) |*e| e.changed = false; + // From here, the fail_index'th allocation fails. + failing.fail_index = failing.alloc_index + fail_index; + l.merge(gpa, "0 0 0 0 /theirs\n9 1 0 0 /kept\n"); + if (!failing.has_induced_failure) break; + } +} -- cgit v1.3