From dbff3a5bb1d47e5a348ce7e91073df43b29b3e44 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 22:49:11 -0300 Subject: kinds #51, builtins-json lists the shipped addons' words, read off the Zig kinds' `Word` lines (image.zig and pdf.zig, handed to the generator and read by the staleness test), each `"addon"` first so the docs test's search for a builtin never meets one and an addon word the docs name is checked against the sources (renaming PdfFit fails it); the glossary says whose addon a word is, and the changelog Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/builtins_json.zig | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'src/builtins_json.zig') diff --git a/src/builtins_json.zig b/src/builtins_json.zig index e8ec5768..c5bc6bb3 100644 --- a/src/builtins_json.zig +++ b/src/builtins_json.zig @@ -2,7 +2,9 @@ //! 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 +//! 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"); @@ -19,7 +21,11 @@ fn lessName(_: void, a: Entry, b: Entry) bool { return std.mem.lessThan(u8, a.name, b.name); } -pub fn write(w: *std.Io.Writer) std.Io.Writer.Error!void { +/// 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 @@ -35,6 +41,7 @@ pub fn write(w: *std.Io.Writer) std.Io.Writer.Error!void { 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\": "); @@ -67,9 +74,35 @@ pub fn write(w: *std.Io.Writer) std.Io.Writer.Error!void { 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.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"); + 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-.json is what the registry says now" { @@ -84,7 +117,14 @@ test "docs/typ/builtins-.json is what the registry says now" { const gpa = std.testing.allocator; var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - try write(&out.writer); + 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)); -- cgit v1.3