diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Output.zig | 171 | ||||
| -rw-r--r-- | core/builtins.zig | 16 |
2 files changed, 151 insertions, 36 deletions
diff --git a/core/Output.zig b/core/Output.zig index 09ec2a51..a1e5865e 100644 --- a/core/Output.zig +++ b/core/Output.zig @@ -757,13 +757,16 @@ fn openStepped(p: *Pardes, id: usize, from: Origin, content: []u8) !void { look.armLookWalk(p, free); } +/// The +Help's text: a builtin a row (its keys, its name, its other +/// shortcuts) with its documentation under it, a line a paragraph, and a +/// blank line; bare (`prefix` empty) every builtin of this view, then the +/// addons' words, the language keys and how a query pane is driven; with +/// `prefix`, the builtins under SPC and those keys. fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { - const full_header = "pardes builtins, and how to run each:\nSPC and its keys, a chord, a button, the\ntopbar - or the name, executed anywhere.\n\n"; + const full_header = "pardes builtins: each one's keys and its name, then what it does. Run one by its keys, or by its name anywhere: B2 on the word, Exec <name>, a line of a ctl file, or `pardes do <name>` from a shell. `Help <name>` shows one builtin alone; `Help <keys>` (or SPC, those keys, then ?) the builtins under SPC and those keys.\n\n"; const group_header = "pardes builtins under SPC"; - const calls_help = "\nCallstack: static caller chains,\nwith signatures and clickable call sites.\nCallers / Callees: one direction at a time.\n"; - const show_calls_help = prefix.len == 0 or std.mem.eql(u8, prefix, "l"); const language_footer = - "\nlanguage keys (motions, not words):\n" ++ + "language keys (motions, not words):\n" ++ "gd gD gy gi gr goto: definition,\n" ++ " declaration, type-def,\n" ++ " implementation, refs\n" ++ @@ -775,7 +778,7 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![] "Tab after a . completion, in insert\n" ++ "C-left-click definition, by mouse\n" ++ "SPC l ... hover, rename, symbols,\n" ++ - " calls, types: above\n"; + " calls, types: above\n\n"; var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); const w = &out.writer; @@ -790,26 +793,15 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![] // word is none in a terminal's view if (!std.mem.startsWith(u8, row.path orelse "", prefix)) continue; if (!builtins.registry.here(p, row.cmd)) continue; - try w.print("{s}\n", .{row.line}); + try w.print("{s}\n{s}\n\n", .{ row.line, builtins.registry.doc(row.cmd).text }); } // An addon's words, after the core's, each saying whose it is. for (p.kinds.words[0..p.kinds.nwords]) |*kw| { const path = kw.leader.slice(); if (!std.mem.startsWith(u8, path, prefix) or (prefix.len > 0 and path.len == 0)) continue; - var keys: [64]u8 = undefined; - var n: usize = 0; - if (path.len > 0) for ("SPC") |ch| { - keys[n] = ch; - n += 1; - }; - for (path) |ch| if (n + 2 <= keys.len) { - keys[n] = ' '; - keys[n + 1] = ch; - n += 2; - }; - try w.print("{s: <12}{s} {s} (addon {s})\n", .{ keys[0..n], kw.name.slice(), kw.doc.slice(), p.kinds.kinds[kw.kind].nameOf() }); + try addonRow(w, p, kw); + try w.print("{s}\n\n", .{kw.doc.slice()}); } - if (show_calls_help) try w.writeAll(calls_help); if (prefix.len == 0) try w.writeAll(language_footer); // The paths that run a builtin with a word (config.leader_args), and how // a query pane is driven: under SPC f, b, s and in the whole listing. @@ -817,7 +809,7 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![] if (std.mem.startsWith(u8, la.path, prefix)) break true; } else std.mem.eql(u8, prefix, "f"); if (prefix.len == 0 or search_group) { - try w.writeAll("\nsearch: a query pane under the text,\nits line 1 the query, its first word\nthe source (/ Find Grep Recent Panes\nSymbols All Refs Def Impl Diagnostics)\n"); + try w.writeAll("search: a query pane under the text,\nits line 1 the query, its first word\nthe source (/ Find Grep Recent Panes\nSymbols All Refs Def Impl Diagnostics)\n"); for (config.leader_args) |la| { if (!std.mem.startsWith(u8, la.path, prefix)) continue; var keys: [16]u8 = @splat(' '); @@ -836,6 +828,91 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![] return out.toOwnedSlice(); } +/// The column a builtin's name stands at in its row (pardes.builtin_rows): +/// an addon's word's row puts its name there too. +const name_column = std.mem.indexOf(u8, pardes.builtin_rows[0].line, @tagName(pardes.builtin_rows[0].cmd)).?; + +/// An addon's word's row: its keys, its name, whose it is. +fn addonRow(w: *std.Io.Writer, p: *const Pardes, kw: *const pardes.Kind.Word) std.Io.Writer.Error!void { + var key_buf: [32]u8 = undefined; + var keys: std.Io.Writer = .fixed(&key_buf); + const path = kw.leader.slice(); + if (path.len > 0) keys.writeAll("SPC") catch {}; + for (path) |ch| keys.print(" {c}", .{ch}) catch {}; + try w.print("{s}", .{keys.buffered()}); + try w.splatByteAll(' ', name_column -| keys.buffered().len); + try w.print("{s} (addon {s})\n", .{ kw.name.slice(), p.kinds.kinds[kw.kind].nameOf() }); +} + +/// Builtin `b` alone (`Help <name>`, `pardes help <name>`): its row, its +/// documentation, and what the list does not say, a line each: its keys +/// spelled out, its argument, what it acts on, and the same word from a +/// shell. +pub fn writeWordHelp(w: *std.Io.Writer, b: Builtin) std.Io.Writer.Error!void { + const row = for (pardes.builtin_rows) |r| { + if (r.cmd == b) break r; + } else unreachable; // a row a builtin + const name = @tagName(b); + try w.print("{s}\n\n{s}\n\n", .{ row.line, builtins.registry.doc(b).text }); + try w.writeAll("keys: "); + const shortcuts = std.mem.trim(u8, row.line[std.mem.indexOf(u8, row.line, name).? + name.len ..], " "); + var first = true; + if (row.path) |path| { + try w.writeAll("SPC"); + for (path) |c| try w.print(" {c}", .{c}); + try w.writeAll(" in normal mode"); + first = false; + } + var parts = std.mem.splitSequence(u8, shortcuts, ", "); + while (parts.next()) |part| if (part.len > 0) { + if (!first) try w.writeAll("; "); + first = false; + try w.writeAll(if (std.mem.eql(u8, part, "topbar")) "its word in the workspace tag" else part); + }; + if (row.path == null and shortcuts.len == 0) try w.print("none: run its name (B2 on it, Exec {s})", .{name}); + try w.writeAll("\nargument: "); + if (config.Settings.find(name)) |setting| { + try w.writeAll(config.Settings.takes(setting.action) orelse "a value"); + if (!builtins.registry.requiresArg(b)) try w.writeAll(", or none"); + } else try w.writeAll(if (!builtins.registry.takesArg(b)) "none" else if (builtins.registry.requiresArg(b)) "required" else "optional"); + try w.writeAll("\nacts on: "); + if (builtins.registry.needs(b)) |kind| try w.print("{s}, ", .{kind.noun()}); + try w.writeAll(if (b == .Edit) + "the pane it runs from (over 9P, a pane's ctl, or the root's for the pane with the keyboard)" + else if (builtins.registry.scope(b) == .session) + "the session (over 9P, the root ctl)" + else + "the pane it runs from (over 9P, that pane's ctl)"); + if (!builtins.registry.hereFor(b, builtins.capabilities, false)) try w.writeAll("; a window's word: none in a terminal"); + try w.print("\nfrom a shell: pardes do {s}{s}\n", .{ name, if (!builtins.registry.takesArg(b)) "" else if (builtins.registry.requiresArg(b)) " ARG" else " [ARG]" }); +} + +/// An addon's word alone, as `writeWordHelp` writes a builtin. +fn writeAddonWordHelp(w: *std.Io.Writer, p: *const Pardes, kw: *const pardes.Kind.Word) std.Io.Writer.Error!void { + try addonRow(w, p, kw); + try w.print("\n{s}\n\nkeys: ", .{kw.doc.slice()}); + const path = kw.leader.slice(); + if (path.len == 0) try w.print("none: run its name (B2 on it, Exec {s})", .{kw.name.slice()}) else { + try w.writeAll("SPC"); + for (path) |c| try w.print(" {c}", .{c}); + try w.writeAll(" in normal mode"); + } + const arg = switch (kw.arg) { + pardes.Kind.c.PA_ARG_NONE => "none", + pardes.Kind.c.PA_ARG_REQUIRED => "required", + else => "optional", + }; + try w.print("\nargument: {s}\nacts on: {s} pane, the one it runs from (over 9P, that pane's ctl)\nfrom a shell: pardes do {s}{s}\n", .{ + arg, + p.kinds.kinds[kw.kind].nameOf(), + kw.name.slice(), + if (kw.arg == pardes.Kind.c.PA_ARG_NONE) "" else if (kw.arg == pardes.Kind.c.PA_ARG_REQUIRED) " ARG" else " [ARG]", + }); +} + +/// The +Help (keys.zig's `SPC ... ?`, the `Help` word): bare, every +/// builtin; `prefix`, those under SPC and those keys. One +Help: a second +/// fills it again. pub fn openHelp(p: *Pardes, id: usize, prefix: []const u8) !void { const content = try helpContent(p, p.gpa, prefix); // content is handed off unfreed on purpose: openRead adopts it or frees @@ -843,6 +920,35 @@ pub fn openHelp(p: *Pardes, id: usize, prefix: []const u8) !void { return openRead(p, id, .{ .cmd = .Help }, prefix, content); } +/// `Help <what>`: a builtin's name or an addon's word, as the registry +/// takes them, that one alone in the +Help; SPC keys, the builtins under +/// them. False for a word that is neither, the +Help left as it was. +pub fn openHelpOn(p: *Pardes, id: usize, what: []const u8) !bool { + const b = std.meta.stringToEnum(Builtin, what); + const word = for (p.kinds.words[0..p.kinds.nwords]) |*kw| { + if (std.mem.eql(u8, kw.name.slice(), what)) break kw; + } else null; + if (b == null and word == null) { + if (!underKeys(p, what)) return false; + try openHelp(p, id, what); + return true; + } + var out: std.Io.Writer.Allocating = .init(p.gpa); + errdefer out.deinit(); + if (b) |one| try writeWordHelp(&out.writer, one) else try writeAddonWordHelp(&out.writer, p, word.?); + try openRead(p, id, .{ .cmd = .Help }, what, try out.toOwnedSlice()); + return true; +} + +/// Whether some builtin of this view, or an addon's word, is under SPC and +/// `keys`. +fn underKeys(p: *const Pardes, keys: []const u8) bool { + for (pardes.builtin_rows) |row| if (row.path) |path| if (std.mem.startsWith(u8, path, keys) and builtins.registry.here(p, row.cmd)) return true; + for (config.leader_args) |la| if (std.mem.startsWith(u8, la.path, keys)) return true; + for (p.kinds.words[0..p.kinds.nwords]) |*kw| if (kw.leader.slice().len > 0 and std.mem.startsWith(u8, kw.leader.slice(), keys)) return true; + return false; +} + /// Where a dump goes by default, as the session's environment says (the /// authority's, `tape.authority`): in `buf`. fn dumpDirectory(buf: []u8) []const u8 { @@ -1078,33 +1184,30 @@ test "plain output does not opt into location decoration" { try std.testing.expect(traits(.{ .query = .completion }).locations); } -test "full Help renders builtin rows, a call hierarchy guide, then language keys" { +test "full Help renders each builtin's row with its documentation under it, then language keys" { const core = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer core.deinit(); const content = try helpContent(core, std.testing.allocator, ""); defer std.testing.allocator.free(content); - // FIRST blank line: the end of the header (the footer opens with one too) + // FIRST blank line: the end of the header const body = content[(std.mem.indexOf(u8, content, "\n\n") orelse return error.MissingHelpHeader) + 2 ..]; var lines = std.mem.splitScalar(u8, body, '\n'); - for (pardes.builtin_rows) |row| if (builtins.registry.here(core, row.cmd)) - try std.testing.expectEqualStrings(row.line, lines.next() orelse - return error.MissingBuiltinHelpRow); - // The short call guide disambiguates the three related commands. - try std.testing.expectEqualStrings("", lines.next() orelse return error.MissingCallHelp); - try std.testing.expectEqualStrings("Callstack: static caller chains,", lines.next() orelse return error.MissingCallHelp); - try std.testing.expectEqualStrings("with signatures and clickable call sites.", lines.next() orelse return error.MissingCallHelp); - try std.testing.expectEqualStrings("Callers / Callees: one direction at a time.", lines.next() orelse return error.MissingCallHelp); + for (pardes.builtin_rows) |row| if (builtins.registry.here(core, row.cmd)) { + try std.testing.expectEqualStrings(row.line, lines.next() orelse return error.MissingBuiltinHelpRow); + // A line a paragraph, then a blank line. + var paragraphs = std.mem.splitScalar(u8, builtins.registry.doc(row.cmd).text, '\n'); + while (paragraphs.next()) |paragraph| try std.testing.expectEqualStrings(paragraph, lines.next() orelse return error.MissingDocumentation); + try std.testing.expectEqualStrings("", lines.next() orelse return error.MissingBlank); + }; // The language keys close the page. - try std.testing.expectEqualStrings("", lines.next() orelse - return error.MissingLanguageKeys); try std.testing.expectEqualStrings("language keys (motions, not words):", lines.next() orelse return error.MissingLanguageKeys); try std.testing.expect(std.mem.indexOf(u8, body, "\ngd gD gy gi gr goto: definition,\n") != null); - // The language group also explains call commands, without the motion footer. + // The language group: its words' documentation, without the motion footer. const group = try helpContent(core, std.testing.allocator, "l"); defer std.testing.allocator.free(group); try std.testing.expect(std.mem.indexOf(u8, group, "language keys") == null); - try std.testing.expect(std.mem.indexOf(u8, group, "Callstack: static caller chains") != null); + try std.testing.expect(std.mem.indexOf(u8, group, builtins.registry.doc(.Callstack).text) != null); } diff --git a/core/builtins.zig b/core/builtins.zig index b7e1a111..81ec4c2b 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1520,12 +1520,24 @@ pub const Tutor = struct { } }; -/// Open the help: every key and every builtin. +/// Show every builtin with its keys and documentation, or one builtin's alone. +/// +/// `Help <name>`: that builtin alone, its keys spelled out, what it acts on +/// and its line for a shell (`Help Diff`); a setting or an addon's word the +/// same. `Help <keys>`: the builtins under SPC and those keys (`Help l`, as +/// `SPC l ?` shows them). Each fills the one +Help: a second Help replaces +/// its text. pub const Help = struct { pub const scope: Scope = .session; + pub const takes_arg = true; pub const output: OutputTraits = .{ .name = config.help_buffer }; pub fn run(c: Ctx) void { - panes.Output.openHelp(c.p, c.id, "") catch |err| c.p.reportError(.at(c.id), "help", err); + const what = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); + if (what.len == 0) return panes.Output.openHelp(c.p, c.id, "") catch |err| c.p.reportError(.at(c.id), "help", err); + const found = panes.Output.openHelpOn(c.p, c.id, what) catch |err| return c.p.reportError(.at(c.id), "help", err); + if (found) return; + var said: pardes.Kind.Buf(128) = .{}; + c.p.reportFailure(c.id, said.print("Help: {s}: no such builtin, nor SPC keys", .{what[0..@min(what.len, 64)]})); } }; |
