//! 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 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 != .tty)) { 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); 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-.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) { .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 `\n", .{ file, @tagName(pardes.platform) }); return error.StaleBuiltinsJson; } }