diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 15:14:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 15:47:11 -0300 |
| commit | 33b1f209162f94a9200f8c00d131aae772eb02f2 (patch) | |
| tree | 9bb64b28ec984326939aa42952ffd3dd34d0a4dc /core/ninep/ctl.zig | |
| parent | f34d3a74cc4a6b735c98ce98cef0df42bd065e67 (diff) | |
| download | pardes-33b1f209162f94a9200f8c00d131aae772eb02f2.tar.gz pardes-33b1f209162f94a9200f8c00d131aae772eb02f2.zip | |
help: a builtin's documentation is the `///` block right above its `pub const <Word> = 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 <Word> = 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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/ninep/ctl.zig')
| -rw-r--r-- | core/ninep/ctl.zig | 115 |
1 files changed, 12 insertions, 103 deletions
diff --git a/core/ninep/ctl.zig b/core/ninep/ctl.zig index 7b4e2754..cfc59e7e 100644 --- a/core/ninep/ctl.zig +++ b/core/ninep/ctl.zig @@ -809,63 +809,8 @@ fn sizeOf(line: []const u8) ?[2]u16 { // ---- /commands ---- /// Every builtin the registry holds, one a line: its word, `arg` when it -/// takes one, and which ctl takes it, `root` or `pane` -- the registry -/// itself, so a new builtin is listed the moment it exists. -/// A builtin's doc comment, its first sentence, from the source itself: a -/// line of /commands says what each word does without a second copy of it. -pub fn summaryOf(word: []const u8, buf: []u8) []const u8 { - const source = @embedFile("../builtins.zig"); - var needle_buf: [64]u8 = undefined; - const needle = std.fmt.bufPrint(&needle_buf, "\npub const {s} = struct", .{word}) catch return ""; - const at = std.mem.indexOf(u8, source, needle) orelse return ""; - // Back over the `///` lines just above it. - var start = at + 1; - while (start > 0) { - const prev_end = start - 1; - const prev_start = if (std.mem.lastIndexOfScalar(u8, source[0..prev_end], '\n')) |nl| nl + 1 else 0; - if (!std.mem.startsWith(u8, std.mem.trimStart(u8, source[prev_start..prev_end], " "), "///")) break; - start = prev_start; - } - var w = std.Io.Writer.fixed(buf); - var lines = std.mem.splitScalar(u8, source[start .. at + 1], '\n'); - while (lines.next()) |line| { - const text = std.mem.trim(u8, std.mem.trimStart(u8, std.mem.trimStart(u8, line, " "), "/"), " "); - if (text.len == 0) continue; - if (w.buffered().len > 0) w.writeByte(' ') catch break; - w.writeAll(text) catch break; - } - const all = stripCitations(w.buffered()); - // The first sentence, whole: to a `.` that ends one. - var i: usize = 0; - while (std.mem.indexOfScalarPos(u8, all, i, '.')) |dot| : (i = dot + 1) { - if (dot + 1 == all.len or all[dot + 1] == ' ') return all[0 .. dot + 1]; - } - return all; -} - -/// Drops a parenthesis that cites source (`(editors/acme/exec.c:892)`, -/// `(exec.c, \`kill\`; acme.c's WKill)`): a reader of /commands wants what -/// the word does, not where acme has it. In place; answers the rest. -fn stripCitations(text: []u8) []u8 { - var out: usize = 0; - var i: usize = 0; - while (i < text.len) { - if (text[i] == '(') if (std.mem.indexOfScalarPos(u8, text, i, ')')) |close| { - const inside = text[i + 1 .. close]; - if (std.mem.indexOf(u8, inside, ".c") != null or std.mem.indexOf(u8, inside, ".zig") != null) { - // With the space before it, and a colon after it kept. - if (out > 0 and text[out - 1] == ' ') out -= 1; - i = close + 1; - continue; - } - }; - text[out] = text[i]; - out += 1; - i += 1; - } - return text[0..out]; -} - +/// takes one, which ctl takes it, `root` or `pane`, and its summary -- the +/// registry itself, so a new builtin is listed the moment it exists. fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void { for (std.enums.values(Builtin)) |b| { // A window's own word is none in a terminal's view. @@ -885,15 +830,8 @@ fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void { var first = true; while (parts.next()) |v| : (first = false) try w.print("{s}{s}", .{ if (first) "" else ",", v }); }; - // Then ` -- ` and what it does: a setting's doc, else its source's. - var summary_buf: [2048]u8 = undefined; - const summary = if (config.Settings.find(@tagName(b))) |setting| setting.doc else summaryOf(@tagName(b), &summary_buf); - if (summary.len > 0) { - // The sentence whole unless it is long, then cut between words. - const kept = @import("../Messages.zig").clip(summary, 240); - try w.print(" -- {s}{s}", .{ kept, if (kept.len < summary.len) "…" else "" }); - } - try w.writeByte('\n'); + // Then ` -- ` and what it does, in a line (builtins.summary_max). + try w.print(" -- {s}\n", .{builtins.registry.doc(b).summary}); } // The addons' words, after the core's: `(addon <name>)` says whose. for (p.kinds.words[0..p.kinds.nwords]) |*kw| try w.print("{s}{s} {s} -- {s} (addon {s})\n", .{ @@ -1381,7 +1319,7 @@ test "commands lists every builtin with its argument and the ctl that takes it" var here: usize = 0; for (std.enums.values(Builtin)) |b| here += @intFromBool(builtins.registry.here(p, b)); try testing.expectEqual(here, lines); - for ([_][]const u8{ "\nDel arg pane -- ", "\nNewcol root -- ", "\nSave arg pane", "\nVerbose arg root on,off", "\nUndo pane -- ", "\nEdit arg both -- acme's Edit" }) |row| + for ([_][]const u8{ "\nDel arg pane -- ", "\nNewcol root -- ", "\nSave arg pane", "\nVerbose arg root on,off", "\nUndo pane -- ", "\nEdit arg both -- " }) |row| try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null); } @@ -2461,48 +2399,19 @@ test "a file changed on disk under unsaved edits is not reloaded: it says so, ge try testing.expect(wr(p, Node.of(serial, .ctl), "Save\n").saved); } -test "every word's /commands description is its own doc comment's first sentence, no citation" { +test "every word's /commands description is its documentation's summary, a line" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); const listed = rd(p, @backingInt(tree.TopFile.commands), 0, 1 << 16).bytes; - for ([_][]const u8{ - "\nRepl arg pane -- Binds this terminal as a language's REPL, so an exec on that language's files is typed into it instead of run.\n", - "\nJoincol root -- Fold the active pane's column into the one on its right, keeping its panes.\n", - "\nExit root -- Quit the editor, refusing once while a pane holds unsaved text: an Exit with nothing edited since discards it.\n", - "\nWrap arg root on,off -- Long lines wrap onto the next row, on or off.\n", - // the reference's examples, as they read. - "\nNewcol root -- An empty column right of the keyboard's, its tag taking the keyboard.\n", - "\nSave arg pane -- Write the pane's text to its file, or to the file its argument names.\n", - "\nVerbose arg root on,off -- A builtin says its own name on the message row as it runs, on or off.\n", - "\nKill arg root -- acme's Kill: stop the commands pardes started", - }) |row| try testing.expect(std.mem.indexOf(u8, listed, row) != null); - try testing.expect(std.mem.indexOf(u8, listed, ".c:") == null); - // Every description is the first sentence of the doc comment just - // above its word's declaration in builtins.zig. - const source = @embedFile("../builtins.zig"); var it = std.mem.tokenizeScalar(u8, listed, '\n'); - var undescribed: usize = 0; while (it.next()) |line| { - // Every builtin has one: a word nobody can look up is no use. - const dashes = std.mem.indexOf(u8, line, " -- ") orelse { - std.debug.print("no doc comment: {s}\n", .{line}); - undescribed += 1; - continue; - }; - const word = line[0..std.mem.indexOfScalar(u8, line, ' ').?]; - if (config.Settings.find(word)) |setting| { - try testing.expect(std.mem.endsWith(u8, line, setting.doc)); - continue; - } - var needle: [64]u8 = undefined; - const at = std.mem.indexOf(u8, source, try std.fmt.bufPrint(&needle, "\npub const {s} = struct", .{word})).?; - const said = std.mem.trimEnd(u8, line[dashes + 4 ..], "…"); - // Its opening words are in the comment block right above. - const head = said[0..@min(said.len, 10)]; - const block_start = std.mem.lastIndexOf(u8, source[0..at], "\n\n") orelse 0; - try testing.expect(std.mem.indexOf(u8, source[block_start..at], head) != null); + const b = std.meta.stringToEnum(Builtin, line[0..std.mem.indexOfScalar(u8, line, ' ').?]).?; + var want: [builtins.summary_max + 8]u8 = undefined; + try testing.expect(std.mem.endsWith(u8, line, try std.fmt.bufPrint(&want, " -- {s}", .{builtins.registry.doc(b).summary}))); } - try testing.expectEqual(0, undescribed); + // A setting's is its own doc's first sentence; a sentence names no source. + try testing.expect(std.mem.indexOf(u8, listed, "\nWrap arg root on,off -- Long lines wrap onto the next row, on or off.\n") != null); + try testing.expect(std.mem.indexOf(u8, listed, ".c:") == null); } test "Newcol at the column cap fails the write and says the cap" { |
