From 33b1f209162f94a9200f8c00d131aae772eb02f2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 15:14:44 -0300 Subject: help: a builtin's documentation is the `///` block right above its `pub const = struct`, read at compile time into one table, `registry.doc` (its summary, the first sentence, and all of it, a line a paragraph), and every reader takes the table (the user's words of 2026-11-23: "the builtins should have their documentation in code somewhere (you may use comptime to extract that) near their definitions"): the summary was found at run time, each word's by a scan of the embedded source for `\npub const = struct` (core/ninep/ctl.zig `summaryOf`), `stripCitations` cutting `(exec.c:892)` and the like out of it: both are deleted; THE CONVENTION is written at the top of core/builtins.zig (the `///` above a builtin is what a user reads in Help, `pardes help`, /commands, `pardes words` and the glossary; no file names, no "registration", no history; notes for the code's reader are `//` lines inside the struct) and the compiler holds what it can read of it: a builtin with no `///` block, a first sentence over `summary_max` (100 bytes) or a `///` naming a .zig file fails the build, naming the builtin (one pass over the source, `registry.docs`; a setting's documentation is its `Setting.doc`, held the same); 35 builtins' docs cut to it (a first sentence over 100 bytes split at a sentence, acme and source citations into `//` notes in the struct), and DumpDir's setting doc (core/config.zig) the same; /commands prints each word's summary whole (no clip: the bound holds it); the docs' JSON (core/builtins_json.zig) takes `doc` from the table and gains `text`, the whole documentation, beside it (an addon's word: its one `.doc` in both); docs/typ/builtins-tui.json regenerated (`zig build builtins-json -Dplatform=tui`; builtins-gui.json in the change that runs the window's gates); tests: builtins.zig "a builtin's documentation is the whole `///` block above it, a line a paragraph, its summary first", ctl.zig "every word's /commands description is its documentation's summary, a line" (replacing the source-scanning one); core-test 942/1108 (166 skipped; the base measures 941/1107 here, see the report), Debug, tui, load 2-7 Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/builtins.zig | 428 +++++++++++++++++++++++++++++++++++---------- core/builtins_json.zig | 28 +-- core/config.zig | 2 +- core/ninep/ctl.zig | 115 ++---------- docs/typ/builtins-tui.json | 232 ++++++++++++------------ 5 files changed, 477 insertions(+), 328 deletions(-) diff --git a/core/builtins.zig b/core/builtins.zig index 51f22bcb..b7e1a111 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1,4 +1,15 @@ //! Command structs and runtime settings form the builtin registry; bindings live in config.zig. +//! +//! THE CONVENTION: the `///` block right above a builtin's `pub const +//! = struct` is what a USER reads, in Help, `pardes help`, /commands, +//! `pardes words` and the manual's glossary (`registry.doc`): what the word +//! does, its argument's forms as typed, what it does bare, where its answer +//! goes, an example where the forms are not obvious. No file names, no +//! "registration", no history: notes for whoever reads the code are `//` +//! lines inside the struct. Its first sentence (to the first `. `) is the +//! summary, at most `summary_max` bytes; a blank `///` starts a paragraph. +//! The compiler holds it: a builtin with no `///`, a summary past the bound +//! or a `///` naming a .zig file fails the build, naming the builtin. const std = @import("std"); const pardes = @import("root.zig"); const tagline = @import("tagline.zig"); @@ -152,6 +163,46 @@ pub fn word(comptime T: type) []const u8 { return n[dot + 1 ..]; } +/// A builtin's documentation (THE CONVENTION, at the top of this file). +pub const Doc = struct { + /// Its first sentence: a line of /commands and of `pardes words`. + summary: []const u8, + /// All of it, a line a paragraph, the summary first. + text: []const u8, +}; + +/// The longest summary, in bytes: a line's worth after its word. +pub const summary_max = 100; + +/// `text`'s first sentence: to the first `.` a blank or a line's end +/// follows (`a.zig@main` and `from..to` go on). +fn docOf(comptime text: []const u8) Doc { + for (text, 0..) |c, i| { + if (c == '\n') return .{ .summary = text[0..i], .text = text }; + if (c == '.' and (i + 1 == text.len or text[i + 1] == ' ' or text[i + 1] == '\n')) return .{ .summary = text[0 .. i + 1], .text = text }; + } + return .{ .summary = text, .text = text }; +} + +/// The `///` lines from `from` to `to` in `source` as paragraphs: each +/// line's text after `///` joined to the next by a blank, a blank `///` a +/// line break. +fn paragraphs(comptime source: []const u8, comptime from: usize, comptime to: usize) []const u8 { + var text: []const u8 = ""; + var lines = std.mem.splitScalar(u8, source[from..to], '\n'); + var gap: []const u8 = ""; + while (lines.next()) |line| { + const said = std.mem.trim(u8, line["///".len..], " "); + if (said.len == 0) { + if (text.len > 0) gap = "\n"; + continue; + } + text = text ++ (if (text.len == 0) "" else if (gap.len > 0) gap else " ") ++ said; + gap = ""; + } + return text; +} + pub const OutputTraits = struct { name: []const u8, steps: bool = false, @@ -204,6 +255,63 @@ pub const registry = struct { return setting; } + /// What a user reads of builtin `b` (THE CONVENTION, at the top). + pub fn doc(b: Builtin()) Doc { + return docs[@backingInt(b)]; + } + + /// Each builtin's documentation, by its number: a hand-written one's + /// the `///` block above it, read off this file at compile time; a + /// setting's its `doc`. + const docs = docs: { + const manual = manualBuiltinList(); + const generated = settingList(); + var table: [manual.len + generated.len]Doc = undefined; + var found: [manual.len]bool = @splat(false); + // a few branches a byte of the source, once + @setEvalBranchQuota(4_000_000); + const source = @embedFile("builtins.zig"); + var block: ?usize = null; // where the `///` lines above this one start + var block_end: usize = 0; + var at: usize = 0; + while (at < source.len) { + const end = std.mem.indexOfScalarPos(u8, source, at, '\n') orelse source.len; + const line = source[at..end]; + if (std.mem.startsWith(u8, line, "///")) { + if (block == null) block = at; + block_end = end; + } else { + if (block) |from| if (std.mem.startsWith(u8, line, "pub const ")) { + const rest = line["pub const ".len..]; + if (std.mem.indexOf(u8, rest, " = struct")) |name_end| for (manual, 0..) |T, i| { + if (!std.mem.eql(u8, word(T), rest[0..name_end])) continue; + table[i] = docOf(paragraphs(source, from, block_end)); + found[i] = true; + }; + }; + block = null; + } + at = end + 1; + } + for (manual, 0..) |T, i| { + if (!found[i]) @compileError("builtin " ++ word(T) ++ " has no documentation: a `///` block right above `pub const " ++ word(T) ++ " = struct`"); + held(word(T), table[i]); + } + for (generated, manual.len..) |setting, i| { + table[i] = docOf(setting.doc); + held(setting.word, table[i]); + } + break :docs table; + }; + + /// THE CONVENTION's two rules the compiler can read. + fn held(comptime name: []const u8, comptime d: Doc) void { + if (d.summary.len > summary_max) + @compileError(std.fmt.comptimePrint("builtin {s}: its first sentence is {d} bytes, over {d}: \"{s}\"", .{ name, d.summary.len, summary_max, d.summary })); + if (std.mem.indexOf(u8, d.text, ".zig") != null) + @compileError("builtin " ++ name ++ ": its documentation names a source file; a note for the code's reader is a `//` line inside the struct"); + } + pub fn takesArg(b: Builtin()) bool { // Every setting takes its value, and a switch flips when bare. return declOr(b, "takes_arg", false, true); @@ -394,10 +502,12 @@ pub fn warnModifiedIn(c: Ctx, asking: Pane.Discarding, which: Asked) bool { return count > 0; } -/// Quit the editor, refusing once while a pane holds unsaved text: an Exit -/// with nothing edited since discards it (acme's Exit, editors/acme/exec.c:892, -/// asking warnModified first). +/// Quit the editor; refused once while a pane holds unsaved text. +/// +/// Each pane with unsaved text is listed in +Unsaved and the notice counts +/// them; Exit again, nothing edited since, quits and discards it. pub const Exit = struct { + // acme's Exit (editors/acme/exec.c:892), asking warnModifiedIn first. pub const scope: Scope = .session; pub fn run(c: Ctx) void { if (warnModifiedIn(c, .Exit, .all)) return; @@ -406,14 +516,16 @@ pub const Exit = struct { } }; -/// acme's Kill (exec.c, `kill`; acme.c's WKill): stop the commands pardes -/// started -- the lines it typed into a terminal, from an exec, a middle -/// click or a pty/run, while their shell says they run (C mark to D). -/// Bare, every one; `Kill make ls`, those whose line starts with one of -/// the words. acme posts the "kill" note, which ends a process; here that -/// is SIGTERM to the terminal's foreground job, and never to the shell, -/// which is its own job and stays. +/// Stop the commands pardes started that are still running. +/// +/// They are the lines pardes typed into a terminal (from an exec, a middle +/// click, a pty/run) while their shell says they run. Bare, every one; `Kill +/// make ls`, those whose line starts with one of the words. Each gets +/// SIGTERM, sent to its terminal's foreground job, never to the shell, which +/// stays. pub const Kill = struct { + // acme's Kill (exec.c, `kill`; acme.c's WKill) posts the "kill" note, + // which ends a process; here a terminal's job between its C and D marks. pub const scope: Scope = .session; pub const takes_arg = true; pub fn run(c: Ctx) void { @@ -462,7 +574,8 @@ pub const Kill = struct { } }; -/// Write the session's panes and layout to the state file, for Restore; +/// Write the session's panes and layout to the state file, for Restore. +/// /// `Dump export ` writes them to a file of their own instead. pub const Dump = struct { pub const scope: Scope = .session; @@ -481,8 +594,11 @@ pub const Dump = struct { } }; -/// Replace every pane with those of a dump: the state file's of that label, -/// or a file's; bare, the last one this session made, else the newest. +/// Replace every pane with those of a dump. +/// +/// `Restore