diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-24 22:04:55 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-24 22:04:55 -0300 |
| commit | 3da01ac676283c6d20ca6f2c0321b9dfe8cc59cd (patch) | |
| tree | 10e3e31fa22186261856df814d6657a7e61cebe4 /core/Prompt.zig | |
| parent | b28053aede910b8714db4f27ef66e59dd96945c0 (diff) | |
| download | pardes-3da01ac676283c6d20ca6f2c0321b9dfe8cc59cd.tar.gz pardes-3da01ac676283c6d20ca6f2c0321b9dfe8cc59cd.zip | |
Prompt: sam prompts one after another make one recipe word, and `s S K A-K` show their scope and what K drops (walk UI, pick-ui SPEC.md §7.1's table, §7.5, item 3 of line E): a sam prompt (`s S K A-K | A-|`, sam's `x y g v | >`) right after another of the same pane's, no key between (Prompt.chain, ended by any key at rest but one that opens such a prompt: Prompt.keyAtRest from keys.handleKey, by a search, by `! A-! $`), extends the pane's one recipe word in its trail, the Edit line it is (`Edit x/self../ g/a/ |tr a-z A-Z`, Trail.setWord); a selecting one that found nothing adds no link; Look on the word puts back the selections its sam prompts made (Pane.recipe_sels, recipe_pri), Exec runs it as Edit (edit_cmd.run, sam's dot the primary selection: a recipe of selections alone prints its matches, as acme's Edit does); while `s S K A-K` is typed the lines the selections it began with cover are fenced `┃` in the gutter, and K's and A-K's selections that would go are dimmed and struck through (a visible line's ranges found by a binary search, never all of them a frame); wire 38; THE FAULTS THE 40,000-SEED FUZZ MET over a session holding a recipe, a search word and an open prompt (a dump or an image cut, flipped and grown loaded and used): View.focus_told's pane slot was never held to the panes (Core.zig reportFocus indexed p.panes past 64: a damaged image's), now a `.index` of its own; A's Field's caret was not held to its text (Field.insert copied past the buffer at caret 513), now a `.place`; Places.current past its places (Places.addTicks's tickOf, the prompt's count and the gutter) taken as none; a search word's `/re/` length past its text taken as its text's; a user loading a damaged Dump could have hit the first three; TEST FIRST: Prompt.zig "sam prompts one after another make one recipe word ..."; fuzz "a dump and an image cut" at 40,000 join and 1,779 image seeds (a temporary edit, put back): pass; the soak, 5 clients: diverged 0, unequal 0 at rtt 30/80/207, quiet checks 413/0 426/0 619/0, the corpus 22 scenes the same, exit 0; goldens pipe (the recipe word in the trail), selregex (the fence); core-test 1017/1183 (166 skipped), shell-test 1230/1233, pane-test 182/184, behaviour-test and its replay 274/277 (the failures A's, as on item 1), process-test A's six and none of mine (`exec` failed once under a load of 8 and passes alone), term-test 17/17, kitty-test 45/45
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 265 |
1 files changed, 253 insertions, 12 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig index 53f6cd5c..1c0b1d31 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -6,6 +6,11 @@ //! 6 lines`), its effect drawn live in the body on every key; Enter applies //! it and keeps the line in its history, Esc puts the body back. //! +//! A sam prompt (`s S K A-K | A-|`, sam's `x y g v | >`) right after +//! another of the same pane's, no key between, extends one recipe word in +//! the pane's trail, the Edit line it is (`Edit x/self/ |tr a-z A-Z`): Look +//! on it puts back the selections it made, Exec runs it as Edit (SPEC §7.5). +//! //! A view's own (TagState.prompt, the coordinator's R4): its field the //! head's (TagState.field); its histories the session's, three of them //! (`/ ? s S K A-K` share one, the pipes another, `:` its own). A prompt for @@ -60,6 +65,9 @@ seeking: bool = false, /// the places' `hits_gen` `counted`. lines: u32 = 0, counted: u32 = std.math.maxInt(u32), +/// The pane, by serial, whose sam prompt Enter took last with no key since: +/// the next one extends its recipe (0: none). +chain: u32 = 0, /// The session's prompt histories: the lines Enter took, newest last. pub const History = struct { @@ -215,7 +223,17 @@ pub fn open(p: *Pardes, pane: *Pane, op: Op) void { s.host = pane.serial; s.field.clear(); s.rows = .{}; - s.prompt = .{ .op = op, .scope = scope }; + s.prompt = .{ .op = op, .scope = scope, .chain = s.prompt.chain }; +} + +/// A key the tag at rest takes (keys.handleKey): any but one that opens a +/// sam prompt ends the recipe Enter left open (a motion, an edit). +pub fn keyAtRest(p: *Pardes, k: Key) void { + const s = &p.view().tag_state; + if (s.prompt.chain == 0) return; + if (hit(k, config.select_regex) or hit(k, config.split_regex) or hit(k, config.keep_regex) or + hit(k, config.remove_regex) or hit(k, config.pipe_selection) or hit(k, config.pipe_selection_to)) return; + s.prompt.chain = 0; } /// A key in the prompt of the live view's tag: the field's editing keys, @@ -331,10 +349,16 @@ fn commit(p: *Pardes, id: usize, pane: *Pane) void { // helix keeps the pattern in `/` p.registers.put(p.gpa, '/', typed, 0, 1, true) catch |err| return p.reportError(.at(id), "prompt", err); p.registers.last_search = '/'; + // Nothing found: nothing changed, no link. + if (s.prompt.found == 0) return; + link(p, pane, op, typed); + keepSelections(p, pane); return; } if (behavior(op)) |how| { p.registers.put(p.gpa, '|', typed, 0, 1, true) catch |err| return p.reportError(.at(id), "prompt", err); + // `! A-! $` apply, and end the recipe: no Edit line says them. + if (op == .pipe or op == .pipe_to) link(p, pane, op, typed) else s.prompt.chain = 0; return Pipe.submit(p, id, &pane.body, how, typed); } } @@ -344,6 +368,8 @@ fn commit(p: *Pardes, id: usize, pane: *Pane) void { /// `/re/ 3/7` in its trail (SPEC §7.3). fn land(p: *Pardes, id: usize, pane: *Pane, op: Op, pat: []const u8, bad: bool) void { const set = &pane.places; + // A search moves the selection: the recipe ends. + p.view().tag_state.prompt.chain = 0; if (bad) { undo(p, pane, op); return p.setMessage(.at(id), regexMessage(pat)); @@ -431,6 +457,77 @@ pub fn dims(p: *const Pardes, pane: *const Pane) bool { return s.kind == .prompt and searches(s.prompt.op) and s.host == pane.serial and s.prompt.bad; } +// ---- the recipe word (SPEC §7.5) ---- + +fn recipeOf(pane: *Pane) ?*Trail.Word { + for (pane.trail.steps[0..pane.trail.n]) |*step| if (step.* == .recipe) return &step.recipe; + return null; +} + +/// Sam prompt `op`'s `line` as a link of `pane`'s recipe: after the one +/// its last sam prompt left open, else a recipe of its own in place of the +/// pane's one. One past the trail's room starts none. +fn link(p: *Pardes, pane: *Pane, op: Op, line: []const u8) void { + const s = &p.view().tag_state; + var buf: [Trail.max_text]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const open_recipe = if (s.prompt.chain == pane.serial) recipeOf(pane) else null; + s.prompt.chain = 0; + (if (open_recipe) |r| w.writeAll(r.text.slice()) else w.writeAll("Edit")) catch return; + w.writeByte(' ') catch return; + const said = switch (op) { + .select => samWord(&w, 'x', line), + .split => samWord(&w, 'y', line), + .keep => samWord(&w, 'g', line), + .remove => samWord(&w, 'v', line), + .pipe => w.print("|{s}", .{line}), + .pipe_to => w.print(">{s}", .{line}), + else => return, + }; + said catch return; + var word_: Trail.Word = .{}; + word_.text.set(w.buffered()); + pane.trail.setWord(.{ .recipe = word_ }); + s.prompt.chain = pane.serial; +} + +/// The selections a recipe's sam prompts made, kept for Look on its word. +fn keepSelections(p: *Pardes, pane: *Pane) void { + const text = edit.flatSurface(p.editor(), &pane.body) catch return; + var ranges: [Text.max_selections]modal.Selection = undefined; + const got = pane.body.ranges(text, 0, &ranges); + const kept = p.gpa.dupe(modal.Selection, ranges[0..got.n]) catch return; + p.gpa.free(pane.recipe_sels); + pane.recipe_sels = kept; + pane.recipe_pri = @intCast(got.pri); +} + +/// Look on pane `id`'s recipe word: the selections it made, again. +pub fn lookRecipe(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + p.view().active = id; + if (pane.recipe_sels.len == 0) return; + const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(id), "recipe", err); + for (pane.recipe_sels) |*r| { + r.anchor = @min(r.anchor, text.len); + r.head = @min(r.head, text.len); + } + pane.body.setRanges(p.settings.tab, text, pane.recipe_sels, &.{}, @min(pane.recipe_pri, pane.recipe_sels.len - 1), true); + pane.body.show(p.settings.tab); +} + +/// Exec on it: the Edit line it is, run on what is selected now +/// (edit_cmd.zig, sam's dot the primary selection). +pub fn execRecipe(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const r = recipeOf(pane) orelse return; + var line: [Trail.max_text]u8 = undefined; + const text = r.text.slice(); + @memcpy(line[0..text.len], text); + p.view().active = id; + pardes.edit_cmd.run(p, id, line["Edit ".len..text.len]); +} + // ---- the search word (SPEC §7.3) ---- /// Pane `pane`'s search word, `/re/ 3/7`: one a pane, its `index` the @@ -441,8 +538,14 @@ fn wordOf(pane: *Pane) ?*Trail.Word { } fn patternOf(w: *const Trail.Word) []const u8 { - const end: u16 = @truncate(w.index); - return w.text.slice()[1 .. end - 1]; + const end = patternEnd(w); + return if (end < 2) "" else w.text.slice()[1 .. end - 1]; +} + +/// Where a search word's `/re/` ends: held to its text (a damaged image's +/// says anything). +fn patternEnd(w: *const Trail.Word) u16 { + return std.math.clamp(@as(u16, @truncate(w.index)), @min(2, w.text.len), w.text.len); } /// Whether the places the pane's body shows are its search word's. @@ -472,7 +575,7 @@ fn refresh(pane: *Pane, wrapped: ?bool) void { if (!lights(pane, w)) return; const set = &pane.places; const f = &pane.file.?; - const end: u16 = @truncate(w.index); + const end = patternEnd(w); const round = wrapped orelse (w.index >> 16 != 0); const hits = set.hits.items; const sel = pane.body.primaryRange(f.content, 0); @@ -525,6 +628,7 @@ pub fn stepped(p: *Pardes, id: usize, forward: bool, before: ?usize) void { const set = &pane.places; if (!lights(pane, w)) return; const after = set.current orelse return; + if (after >= set.hits.items.len) return; refresh(pane, if (before) |b| (if (forward) after <= b else after >= b) else false); const index = File.lineIndex(p.gpa, &pane.file.?) catch return; var said: [Trail.max_text + 32]u8 = undefined; @@ -550,8 +654,7 @@ pub fn lookWord(p: *Pardes, id: usize) void { set.search(p, pane, .search, 0, pat, false, null, false); set.fill(p, pane, std.math.maxInt(usize), regexp.Regex.budget); p.view().active = id; - const end: u16 = @truncate(w.index); - const rest = w.text.slice()[end..]; + const rest = w.text.slice()[patternEnd(w)..]; const n = std.fmt.parseInt(usize, std.mem.trim(u8, rest[0 .. std.mem.indexOfScalar(u8, rest, '/') orelse rest.len], " "), 10) catch return; if (n > 0 and n <= set.hits.items.len) set.goTo(p.settings.tab, pane, n - 1, false); } @@ -587,17 +690,21 @@ pub fn unlit(pane: *Pane) ?[2]usize { /// The gutter's marks while a prompt is typed (SPEC §7.1): `▸` on the /// current place's line and `▪` on every other line holding a place (`/ -/// ?`). In the blank after a row's line number, on a line's first row. +/// ?`); `┃` on each line the selections a sam prompt began with cover (`s +/// S K A-K`), its scope. In the blank after a row's line number, on a +/// line's first row. pub fn paintGutter(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) void { const ts = &p.view().tag_state; - if (ts.kind != .prompt or ts.host != pane.serial or !searches(ts.prompt.op)) return; + if (ts.kind != .prompt or ts.host != pane.serial) return; const f = &(pane.file orelse return); + if (mode(ts.prompt.op) != null) return fence(p, s, pane, r, body_h); + if (!searches(ts.prompt.op)) return; const set = &pane.places; if (!set.shows(pane)) return; const hits = set.hits.items; if (hits.len == 0) return; const tab = p.settings.tab; - const x = r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1); + const x = gutterX(pane, r); const y = p.bodyTop(pane, r); const ink = p.chromeTheme().box; const nlines = File.nlines(p.gpa, f); @@ -612,7 +719,7 @@ pub fn paintGutter(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: const next = File.lineStart(p.gpa, f, @intCast(line + 1)); const i = Places.firstFrom(hits, ls); if (i == hits.len or hits[i].start >= @max(next, ls + 1)) continue; - const current = if (set.current) |c| hits[c].start >= ls and hits[c].start < @max(next, ls + 1) else false; + const current = if (set.current) |c| c < hits.len and hits[c].start >= ls and hits[c].start < @max(next, ls + 1) else false; var style = s.at(x, y + vr).style; style.fg = .{ .rgb = ink }; s.set(x, y + vr, if (current) "▸" else "▪", style); @@ -654,6 +761,90 @@ fn plainWord(w: *std.Io.Writer, op: Op, line: []const u8) !?[]const u8 { return if (op == .search or op == .search_back) "Look" else "Exec"; } +/// The x of the blank after pane `pane`'s line numbers. +fn gutterX(pane: *const Pane, r: pardes.Rect) u16 { + return r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1); +} + +/// `┃` on the lines the selections a sam prompt began with cover, and +/// the ones `K` or `A-K` would drop dimmed and struck through. +fn fence(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) void { + const f = &pane.file.?; + const tab = p.settings.tab; + const snap = pane.sel_snap[0..pane.nsel_snap]; + if (snap.len == 0) return; + const x = gutterX(pane, r); + const y = p.bodyTop(pane, r); + const ink = p.chromeTheme().box; + const nlines = File.nlines(p.gpa, f); + const op = p.view().tag_state.prompt.op; + var kept: [Text.max_selections]modal.Selection = undefined; + const now = if (op == .keep or op == .remove) pane.body.ranges(f.content, 0, &kept).n else 0; + var last: i32 = -1; + var vr: u16 = 0; + while (vr < body_h) : (vr += 1) { + const line = pane.wrapAt(tab, vr).line; + if (line == last or line < 0) continue; + if (line >= nlines) break; + last = line; + const ls = File.lineStart(p.gpa, f, @intCast(line)); + const le = File.lineStart(p.gpa, f, @intCast(line + 1)); + // The ranges are in the text's order: the first ending past the + // line's start, on while they start before its end. + var i = endingPast(snap, ls); + if (i < snap.len and @min(snap[i].anchor, snap[i].head) < @max(le, ls + 1)) { + var style = s.at(x, y + vr).style; + style.fg = .{ .rgb = ink }; + s.set(x, y + vr, "┃", style); + } + // K and A-K: the selections that would go, on this line. + if (op != .keep and op != .remove) continue; + while (i < snap.len and @min(snap[i].anchor, snap[i].head) < le) : (i += 1) { + const lo = @min(snap[i].anchor, snap[i].head); + const hi = @max(snap[i].anchor, snap[i].head); + const k = endingPast(kept[0..now], lo); + const stays = k < now and @min(kept[k].anchor, kept[k].head) == lo and @max(kept[k].anchor, kept[k].head) == hi; + if (!stays) strike(p, s, pane, r, line, @intCast(@max(lo, ls) - ls), @intCast(@min(hi, le) - ls)); + } + } +} + +/// The first of `ranges` (in the text's order) ending past byte `at`. +fn endingPast(ranges: []const modal.Selection, at: usize) usize { + return std.sort.partitionPoint(modal.Selection, ranges, at, struct { + fn before(byte: usize, r: modal.Selection) bool { + return @max(r.anchor, r.head) <= byte; + } + }.before); +} + +/// Bytes [lo, hi) of `line`, as many of its rows as show them, dimmed and +/// struck through (File.paintWordSelection's walk). +fn strike(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, line: i32, lo_: i32, hi_: i32) void { + const tab = p.settings.tab; + const tw = r.w - config.GUTTER; + const prefix: i32 = File.gutterWidth(pane); + const body_y = p.bodyTop(pane, r); + var vr: i32 = 0; + while (vr + @as(i32, pane.tag_rows) < @as(i32, r.h)) : (vr += 1) { + const here = pane.wrapAt(tab, vr); + if (here.line != line) continue; + var hi = hi_; + const next = pane.wrapAt(tab, vr + 1); + if (next.line == line) hi = @min(hi, next.at); + const lo = @max(lo_, here.at); + if (hi <= lo) continue; + const c0 = prefix + here.pad + File.displayOffset(tab, pane, line, here.at, lo); + const c1 = prefix + here.pad + File.displayEndOffset(tab, pane, line, here.at, hi - 1); + var col = @max(prefix, c0); + while (col <= c1 and col < tw) : (col += 1) { + const style = &s.at(r.x + config.GUTTER + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(vr))).style; + style.dim = true; + style.strikethrough = true; + } + } +} + /// The head of the prompt's tag: ` s self▌ = Exec Edit x/self/ 4 /// selections in 6 lines ⏎ select · C-r history · esc`. pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { @@ -702,11 +893,12 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c const hits = set.hits.items; if (set.complete() and hits.len == 0) return text.appendSlice(arena, " 0"); try text.appendSlice(arena, " "); - if (set.current) |c| try text.print(arena, "{d}", .{c + 1}) else try text.appendSlice(arena, "–"); + const at = if (set.current) |c| (if (c < hits.len) c else null) else null; + if (at) |c| try text.print(arena, "{d}", .{c + 1}) else try text.appendSlice(arena, "–"); if (!set.complete()) return text.appendSlice(arena, "/⋯"); try text.print(arena, "/{d} · {d} line{s}", .{ hits.len, pr.lines, plural(pr.lines) }); // Round the end from where it began. - const c = set.current orelse return; + const c = at orelse return; const origin = set.origin(pane); if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " · wrapped"); }, @@ -940,3 +1132,52 @@ test "? searches back from the caret; no match is 0; a pattern that is no regex try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(p.panes[p.view().active].?.query != null); } + +test "sam prompts one after another make one recipe word, the Edit line it is; a motion ends it and the next starts the pane's one again; ! adds nothing; the scope fenced in the gutter, what K drops struck; Look puts its selections back, Exec runs it as Edit" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + while (p.nextEffect()) |_| {} + const pane = try p.setTestFile("self.a = 1;\nself.b = 2;\nother = 3;\n"); + typeKeys(p, "%sother"); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/other/ › /test.txt ")); + // Look on it: the selection it made, again. + typeKeys(p, "gg"); + Trail.lookStep(p, 0, 0); + const back = pane.body.primaryRange(pane.file.?.content, 0); + try std.testing.expectEqual(@as(usize, 24), @min(back.anchor, back.head)); + try std.testing.expectEqual(@as(usize, 29), @max(back.anchor, back.head)); + // A motion ends it: the next s is a recipe of its own, the pane's one. + typeKeys(p, "j%sself.."); + for ([_]u16{ 0, 1, 2 }) |row| try std.testing.expectEqualStrings("┃", try gutterAt(p, frame.allocator(), row)); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ › /test.txt ")); + try std.testing.expectEqual(@as(u8, 1), pane.trail.n); + // K right after: `self.b` would go, struck through. + typeKeys(p, "Ka"); + p.sync(); + const s = try p.render(frame.allocator()); + const r = p.view().rects[0]; + const cell = s.at(r.x + config.GUTTER + File.gutterWidth(pane), p.bodyTop(pane, r) + 1); + try std.testing.expect(cell.style.strikethrough and cell.style.dim); + try std.testing.expect(!s.at(r.x + config.GUTTER + File.gutterWidth(pane), p.bodyTop(pane, r)).style.strikethrough); + p.update(.{ .key = .{ .cp = Key.enter } }); + typeKeys(p, "|"); + p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ g/a/ |tr a-z A-Z › /test.txt ")); + // `!` applies and adds nothing: an Edit line cannot say it. + typeKeys(p, "!"); + p.update(.{ .key = .{ .cp = 'e', .text = "echo" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "Edit x/self../ g/a/ |tr a-z A-Z › /test.txt ")); + while (p.nextEffect()) |_| {} + if (p.pipe.wait) |*w| w.deinit(); + p.pipe.wait = null; + // Exec on it: Edit runs it, its command off the loop. + typeKeys(p, "%"); + Trail.execStep(p, 0, 0); + try std.testing.expect(p.pipe.edit_run != null); +} |
