summaryrefslogtreecommitdiff
path: root/src/builtins_json.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
committerGabriel Schneider <[email protected]>2026-10-30 17:35:06 -0300
commit6d8da8721141284cf33bb901168b26135893eb86 (patch)
tree791d63401d4e85fbca1d597a8ce0326e70efd545 /src/builtins_json.zig
parentefd43a3a2cf03bb741366331158b368abf9ac583 (diff)
parentbf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff)
downloadpardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz
pardes-6d8da8721141284cf33bb901168b26135893eb86.zip
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'src/builtins_json.zig')
-rw-r--r--src/builtins_json.zig87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/builtins_json.zig b/src/builtins_json.zig
new file mode 100644
index 00000000..4440130d
--- /dev/null
+++ b/src/builtins_json.zig
@@ -0,0 +1,87 @@
+//! Every builtin as JSON, for the docs' glossary (docs/typ/builtins-tty.json
+//! and builtins-gui.json, a build's list each; style.typ merges them):
+//! the rows the `Help` builtin draws (pardes.builtin_rows: the leader path
+//! and the other shortcuts) with what /commands says of each word (its doc
+//! comment's first sentence, or a setting's doc). One source, so the
+//! glossary and Help cannot drift; the test below fails when the committed
+//! copy is stale, and `zig build builtins-json` rewrites it.
+const std = @import("std");
+const pardes = @import("pardes.zig");
+const builtins = @import("builtins.zig");
+const config = @import("config.zig");
+const ctl = @import("ninep/ctl.zig");
+
+const str = std.json.Stringify.encodeJsonString;
+
+const Entry = struct { name: []const u8, row: usize };
+
+fn lessName(_: void, a: Entry, b: Entry) bool {
+ return std.mem.lessThan(u8, a.name, b.name);
+}
+
+pub fn write(w: *std.Io.Writer) !void {
+ var order: [pardes.builtin_rows.len]Entry = undefined;
+ for (pardes.builtin_rows, 0..) |row, i| order[i] = .{ .name = @tagName(row.cmd), .row = i };
+ std.mem.sort(Entry, &order, {}, lessName);
+ try w.writeAll("[\n");
+ for (order, 0..) |e, n| {
+ const row = pardes.builtin_rows[e.row];
+ const b = row.cmd;
+ const name = @tagName(b);
+ try w.writeAll(" {\"name\": ");
+ try str(name, .{}, w);
+ try w.writeAll(", \"leader\": ");
+ if (row.path) |p| {
+ try w.writeAll("\"SPC");
+ for (p) |c| try w.print(" {c}", .{c});
+ try w.writeByte('"');
+ } else try w.writeAll("null");
+ // Help's line ends in the other shortcuts, after the name's column.
+ try w.writeAll(", \"also\": [");
+ const at = std.mem.indexOf(u8, row.line, name).? + name.len;
+ const rest = std.mem.trim(u8, row.line[at..], " ");
+ var parts = std.mem.splitSequence(u8, rest, ", ");
+ var first = true;
+ while (parts.next()) |part| if (part.len > 0) {
+ if (!first) try w.writeAll(", ");
+ first = false;
+ try str(part, .{}, w);
+ };
+ try w.writeAll("], \"arg\": ");
+ try w.writeAll(if (builtins.registry.takesArg(b)) "true" else "false");
+ try w.writeAll(", \"scope\": ");
+ try str(if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane", .{}, w);
+ const setting = config.Runtime.find(name);
+ try w.writeAll(", \"setting\": ");
+ try w.writeAll(if (setting != null) "true" else "false");
+ try w.writeAll(", \"choices\": ");
+ if (setting) |s| if (config.Runtime.choices(s.action)) |v| try str(v, .{}, w) else try w.writeAll("null") else try w.writeAll("null");
+ var buf: [2048]u8 = undefined;
+ const doc = if (setting) |s| s.doc else ctl.summaryOf(name, &buf);
+ try w.writeAll(", \"doc\": ");
+ try str(doc, .{}, w);
+ try w.writeAll(if (n + 1 < order.len) "},\n" else "}\n");
+ }
+ try w.writeAll("]\n");
+}
+
+test "docs/typ/builtins-<platform>.json is what the registry says now" {
+ // a terminal build checks its file, a window build its own
+ const file = switch (pardes.platform) {
+ .tty => "builtins-tty.json",
+ .gui => "builtins-gui.json",
+ else => return error.SkipZigTest,
+ };
+ const gpa = std.testing.allocator;
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ defer out.deinit();
+ try write(&out.writer);
+ var dir = try std.Io.Dir.cwd().openDir(std.testing.io, @import("doc_typ").dir, .{});
+ defer dir.close(std.testing.io);
+ const committed = try dir.readFileAlloc(std.testing.io, file, gpa, .limited(1 << 20));
+ defer gpa.free(committed);
+ if (!std.mem.eql(u8, committed, out.written())) {
+ std.debug.print("docs/typ/{s} is stale: run `zig build builtins-json -Dplatform={s} --prefix <dir>`\n", .{ file, @tagName(pardes.platform) });
+ return error.StaleBuiltinsJson;
+ }
+}