1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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("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);
}
/// 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;
}
}
|