summaryrefslogtreecommitdiff
path: root/core/builtins_json.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/builtins_json.zig')
-rw-r--r--core/builtins_json.zig136
1 files changed, 136 insertions, 0 deletions
diff --git a/core/builtins_json.zig b/core/builtins_json.zig
new file mode 100644
index 00000000..3773e5ae
--- /dev/null
+++ b/core/builtins_json.zig
@@ -0,0 +1,136 @@
+//! Every builtin as JSON, for the docs' glossary (docs/typ/builtins-tui.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), then the shipped addons'
+//! words, read off their kinds' `Word` lines (`"addon"` first, so a search
+//! for a builtin's `{"name": ` never meets one). 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("root.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);
+}
+
+/// The kinds whose words are listed (under addons/), their sources in
+/// this order.
+pub const addons = [_][]const u8{ "image/image.zig", "pdf/pdf.zig" };
+
+pub fn write(w: *std.Io.Writer, addon_sources: []const []const u8) std.Io.Writer.Error!void {
+ var all: [pardes.builtin_rows.len]Entry = undefined;
+ var len: usize = 0;
+ // This build's own frontend's words (a desktop build has the other's
+ // too, which a view of its kind takes: builtins.zig `here`).
+ for (pardes.builtin_rows, 0..) |row, i| if (pardes.builtins.registry.hereFor(row.cmd, pardes.builtins.frontend, pardes.platform != .tui)) {
+ all[len] = .{ .name = @tagName(row.cmd), .row = i };
+ len += 1;
+ };
+ const order = all[0..len];
+ 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);
+ if (n > 0) try w.writeAll(",\n");
+ 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.writeByte('}');
+ }
+ // A kind's word is a line of its own: `.{ .name = "PdfFit", .leader =
+ // "tz", .arg = .optional, .doc = "..." },` (a doc's Zig escapes are
+ // copied as written: none has one).
+ for (addon_sources) |source| {
+ var kind: []const u8 = "";
+ var lines = std.mem.splitScalar(u8, source, '\n');
+ while (lines.next()) |line| {
+ if (std.mem.indexOf(u8, line, "pub const head: addon.Head = ") != null) kind = quoted(line, ".name = ").?;
+ if (!std.mem.startsWith(u8, std.mem.trimStart(u8, line, " "), ".{ .name = \"")) continue;
+ try w.print(",\n {{\"addon\": \"{s}\", \"name\": \"{s}\", \"leader\": ", .{ kind, quoted(line, ".name = ").? });
+ if (quoted(line, ".leader = ")) |l| {
+ try w.writeAll("\"SPC");
+ for (l) |c| try w.print(" {c}", .{c});
+ try w.writeByte('"');
+ } else try w.writeAll("null");
+ try w.print(", \"also\": [], \"arg\": {}, \"scope\": \"pane\", \"setting\": false, \"choices\": null, \"doc\": ", .{std.mem.indexOf(u8, line, ".arg = .") != null});
+ try str(quoted(line, ".doc = ") orelse "", .{}, w);
+ try w.writeByte('}');
+ }
+ }
+ try w.writeAll("\n]\n");
+}
+
+/// The string after `field` on a line: `.name = "PdfFit"` is PdfFit.
+fn quoted(line: []const u8, comptime field: []const u8) ?[]const u8 {
+ const at = (std.mem.indexOf(u8, line, field ++ "\"") orelse return null) + field.len + 1;
+ return line[at .. std.mem.indexOfScalarPos(u8, line, at, '"') orelse return null];
+}
+
+test "docs/typ/builtins-<platform>.json is what the registry says now" {
+ // a terminal build checks its file, a window build its own; both are
+ // Linux builds' lists
+ if (comptime @import("builtin").os.tag != .linux) return error.SkipZigTest;
+ const file = switch (pardes.platform) {
+ .tui => "builtins-tui.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();
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var sources: [addons.len][]const u8 = undefined;
+ for (addons, &sources) |path, *source| {
+ var buf: [4096]u8 = undefined;
+ source.* = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, try std.fmt.bufPrint(&buf, "{s}/{s}", .{ @import("doc_typ").addons, path }), arena.allocator(), .limited(1 << 20));
+ }
+ try write(&out.writer, &sources);
+ 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;
+ }
+}