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
|
//! 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");
fn str(w: *std.Io.Writer, s: []const u8) !void {
try w.writeByte('"');
for (s) |c| switch (c) {
'"' => try w.writeAll("\\\""),
'\\' => try w.writeAll("\\\\"),
'\n' => try w.writeAll("\\n"),
0...9, 11...0x1f => try w.print("\\u{x:0>4}", .{c}),
else => try w.writeByte(c),
};
try w.writeByte('"');
}
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 order: [pardes.builtin_rows.len]Entry = undefined;
for (pardes.builtin_rows, 0..) |row, i| order[i] = .{ .name = @tagName(row.cmd), .row = i };
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(w, name);
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(w, part);
};
try w.writeAll("], \"arg\": ");
try w.writeAll(if (builtins.registry.takesArg(b)) "true" else "false");
try w.writeAll(", \"scope\": ");
try str(w, if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane");
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(w, v) 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(w, doc);
try w.writeAll(if (n + 1 < order.len) "},\n" else "}\n");
}
try w.writeAll("]\n");
}
test "docs/typ/builtins-<platform>.json is what the registry says now" {
// a terminal build checks its file, a window build its own
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 <dir>`\n", .{ file, @tagName(pardes.platform) });
return error.StaleBuiltinsJson;
}
}
|