summaryrefslogtreecommitdiff
path: root/src/config.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.zig')
-rw-r--r--src/config.zig71
1 files changed, 70 insertions, 1 deletions
diff --git a/src/config.zig b/src/config.zig
index e9595a2a..f83e7945 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -2101,9 +2101,16 @@ const doc_typ = struct {
}
}
+ /// The other build's builtins (docs/typ/builtins-*.json), so the docs
+ /// may name a window-only word in a terminal build's test, and back.
+ var other_build: []const u8 = "";
+
fn word(w: []const u8) bool {
const name = w[0 .. std.mem.indexOfScalar(u8, w, '+') orelse w.len];
- return std.meta.stringToEnum(Builtin, name) != null;
+ if (std.meta.stringToEnum(Builtin, name) != null) return true;
+ var buf: [96]u8 = undefined;
+ const needle = std.fmt.bufPrint(&buf, "{{\"name\": \"{s}\",", .{name}) catch return false;
+ return std.mem.indexOf(u8, other_build, needle) != null;
}
fn chord(tok: []const u8) ?Chord {
@@ -2195,6 +2202,10 @@ test "docs/typ names only words, keys and default tags that exist" {
var dir = try std.Io.Dir.cwd().openDir(io, @import("doc_typ").dir, .{ .iterate = true });
defer dir.close(io);
var found: [3]std.ArrayList([]const u8) = @splat(.empty);
+ for ([_][]const u8{ "builtins-tty.json", "builtins-gui.json" }) |f| {
+ const text = dir.readFileAlloc(io, f, gpa, .limited(1 << 20)) catch continue;
+ doc_typ.other_build = try std.mem.concat(gpa, u8, &.{ doc_typ.other_build, text });
+ }
var it = dir.iterate();
while (try it.next(io)) |entry| if (entry.kind == .file and std.mem.endsWith(u8, entry.name, ".typ")) {
const source = try dir.readFileAlloc(io, entry.name, gpa, .limited(1 << 20));
@@ -2331,3 +2342,61 @@ test "the tutor names only words, keys, paths and default tags that exist" {
}
}
}
+
+test "reference.typ's #fstree is the tree the session serves, file for file" {
+ const tree = @import("ninep/tree.zig");
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const gpa = arena.allocator();
+ const io = std.testing.io;
+ var dir = try std.Io.Dir.cwd().openDir(io, @import("doc_typ").dir, .{});
+ defer dir.close(io);
+ const source = try dir.readFileAlloc(io, "reference.typ", gpa, .limited(1 << 20));
+ const open = "#fstree(```";
+ const start = (std.mem.indexOf(u8, source, open) orelse return error.NoFsTree) + open.len;
+ const end = std.mem.indexOfPos(u8, source, start, "```)") orelse return error.NoFsTree;
+
+ // what the reference draws: a path per line, from its indent
+ var drawn: std.StringArrayHashMapUnmanaged(void) = .empty;
+ var stack: [8][]const u8 = undefined;
+ var lines = std.mem.tokenizeScalar(u8, source[start..end], '\n');
+ while (lines.next()) |line| {
+ const body = std.mem.trimStart(u8, line, " ");
+ if (body.len == 0) continue;
+ const depth = (line.len - body.len) / 2;
+ const name = body[0 .. std.mem.indexOfScalar(u8, body, ' ') orelse body.len];
+ stack[depth] = name;
+ var path: std.ArrayList(u8) = .empty;
+ for (stack[0 .. depth + 1]) |part| try path.appendSlice(gpa, part);
+ try drawn.put(gpa, path.items, {});
+ }
+
+ // what the session serves: tree.zig's files, and fs.zig's os/ and src/
+ var served: std.StringArrayHashMapUnmanaged(void) = .empty;
+ for (std.enums.values(tree.TopFile)) |f| switch (f) {
+ .root => {},
+ .new => try served.put(gpa, "pane/new", {}),
+ else => try served.put(gpa, if (f.dir()) try std.fmt.allocPrint(gpa, "{s}/", .{f.fileName()}) else f.fileName(), {}),
+ };
+ try served.put(gpa, "os/", {});
+ try served.put(gpa, "src/", {});
+ try served.put(gpa, "pane/<n>/", {});
+ for (std.enums.values(tree.PaneFile)) |f| if (f != .dir) {
+ const under = if (f.inPty() and f != .pty) "pane/<n>/pty/" else "pane/<n>/";
+ try served.put(gpa, try std.fmt.allocPrint(gpa, "{s}{s}{s}", .{ under, f.fileName(), if (f.isDir()) "/" else "" }), {});
+ };
+ try served.put(gpa, "col/<n>/", {});
+ for (std.enums.values(tree.ColFile)) |f| if (f != .dir)
+ try served.put(gpa, try std.fmt.allocPrint(gpa, "col/<n>/{s}", .{f.fileName()}), {});
+
+ var drift = false;
+ for (served.keys()) |k| if (!drawn.contains(k)) {
+ std.debug.print("reference.typ's tree lacks {s}\n", .{k});
+ drift = true;
+ };
+ for (drawn.keys()) |k| if (!served.contains(k)) {
+ std.debug.print("reference.typ's tree has {s}, which the session does not serve\n", .{k});
+ drift = true;
+ };
+ if (drift) return error.FsTreeDrift;
+}