From abe3817e8dd0af41e6b5786bdbeb631219b58c23 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 20:33:01 -0300 Subject: Prompt: a body's text prompts ask in its tag's head (walk UI, pick-ui SPEC.md §7.1, §7.2, item 1 of line E): `s S K A-K | A-| ! A-! $` from a body are a typing state of the view's tag (TagState.Kind.prompt, its state Prompt.zig, its field A's Field), drawn after the prefix in place of the tag's own text (R1): box `^`, the prompt's key cell, the field, the plain word it stands for (`= Exec Edit x/self/`, `Edit g/re/`, `Edit |tr a-z A-Z`, `Edit >cmd`; none for `! A-! $`), its count (`2 selections in 3 lines`, `keeps 1 of 2`, `0` for no match, the selections it began with back), the hints; the live machinery as it was (normal.applySelRegex, now returning what it found; the pipe off the loop, Pipe.submit split from the notice band's submitPipe); a pattern that is no regex changes nothing, the last result drawn dimmed (body_layer) and the reason at the count's place in the error ink (TagState.Style.err, regexp's message); Up/Down recall the history inline, Ctrl-r unrolls it as a band step filtered by the field (three session histories, Pardes.prompt_history: search and select's, the pipes', the command line's, 16 lines each, in the digest and the dump); Esc and Backspace on an empty field put the body back; Enter keeps the line in its history and the pattern in `/`, the command in `|`; a paste goes into the field; a prompt for a tag's or a header's text, Save's path, a terminal's `/` still ask on the notice band; wire 37; TEST FIRST: Prompt.zig "s asks in the tag's head ..." and "Enter keeps what s found ... a pipe from the body asks in the tag and runs off the loop"; Pipe.zig's and panes.zig's prompt tests and test/behaviour/keys.zig's two read the head; goldens pipe, selregex, msg (the prompt in the tag head, no chip); core-test 1014/1180 (166 skipped), shell-test 1227/1230, pane-test 182/184 (the 1 failure A's: `SPC h ?` is Help in the tag now), behaviour-test 274/277 (the 3 failures A's: window mode's head, a struck leader key), process-test the 6 of A's report plus none of mine, term-test 17/17, kitty-test 45/45 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/normal.zig | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'core/normal.zig') diff --git a/core/normal.zig b/core/normal.zig index 1f7ce1a7..c59a847c 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -22,6 +22,7 @@ const sel_slot = pardes.sel_slot; const Pardes = pardes.Pardes; const Macro = pardes.Macro; const Editor = @import("Editor.zig"); +const Prompt = @import("Prompt.zig"); fn isPrefix(key: Key, cp: u21) bool { return key.cp == cp and !key.ctrl and !key.alt; @@ -53,15 +54,19 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// Select (or split on, keep or remove by) `pat` inside the selections `t` /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on -/// `pane`. -pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); +/// `pane`. The ranges it found: none, the snapshot's back. +pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) usize { + return selectByRegex(p.editor(), t, pane.sel_snap[0..pane.nsel_snap], pane.sel_snap_pri, pane.sel_snap_expl, pat, mode); } /// Select (or split on, keep or remove by) `pat` inside `snap`, the -/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed. -pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(e, t) catch |err| return e.reportError(err); +/// ranges there were (primary `pri`) when s/S/K/Alt-K was pressed; the +/// ranges it found. +pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, expl: bool, pat: []const u8, mode: modal.Normal.RegexMode) usize { + const text = edit.flatSurface(e, t) catch |err| { + e.reportError(err); + return 0; + }; var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; if (regexp.Regex.compile(pat)) |compiled| { @@ -69,7 +74,10 @@ pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, e var hay_all = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| return e.reportError(err); + } else true) hay_all = std.ascii.allocLowerString(e.scratch, text) catch |err| { + e.reportError(err); + return 0; + }; for (snap) |r| { const from = @min(r.anchor, r.head); const to = @min(@max(r.anchor, r.head), text.len); @@ -116,9 +124,11 @@ pub fn selectByRegex(e: Editor, t: *Text, snap: []modal.Selection, pri: usize, e r.anchor = @min(r.anchor, text.len); r.head = @min(r.head, text.len); } - return t.setRanges(e.tab, text, snap, &.{}, pri, expl); + t.setRanges(e.tab, text, snap, &.{}, pri, expl); + return 0; } t.setRanges(e.tab, text, out[0..m], &.{}, 0, true); // helix keeps primary 0 (its own TODO) + return m; } fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp.Regex.Match { @@ -600,6 +610,9 @@ fn actionPass(e: Editor, t: *Text, action: modal.Normal.Action) edit.Error!void } fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { + // A body's prompt is its tag's (Prompt.zig); a tag's or a header's asks + // on the notice band. + if (t.what == .body) return Prompt.open(p, pane, Prompt.ofMode(mode)); const text = try edit.flatSurface(p.editor(), t); const got = t.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); @@ -1360,11 +1373,18 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .column, .workspace => tagline.exitHeader(p), .body, .input => tagline.enterTag(p, pane), }, - .pipe_selection => return panes.startPrompt(p, pane, t, .{ .pipe = .replace }), - .pipe_selection_to => return panes.startPrompt(p, pane, t, .{ .pipe = .ignore }), - .insert_output => return panes.startPrompt(p, pane, t, .{ .pipe = .insert }), - .append_output => return panes.startPrompt(p, pane, t, .{ .pipe = .append }), - .keep_pipe => return panes.startPrompt(p, pane, t, .{ .pipe = .keep }), + inline .pipe_selection, .pipe_selection_to, .insert_output, .append_output, .keep_pipe => |role| { + const how: modal.Normal.PipeBehavior = switch (role) { + .pipe_selection => .replace, + .pipe_selection_to => .ignore, + .insert_output => .insert, + .append_output => .append, + else => .keep, + }; + // A body's in its tag (Prompt.zig), else on the notice band. + if (t.what == .body) return Prompt.open(p, pane, Prompt.ofBehavior(how)); + return panes.startPrompt(p, pane, t, .{ .pipe = how }); + }, // `/` in a text is its query pane's line 1 (Query.zig); a // terminal's still asks on the notice band. .search => if (pane.file != null and t.what == .body) @@ -1657,7 +1677,7 @@ test "s and S search a multi-line selection as addr does: ^ and $ per line, . wi }) |c| { pane.sel_snap[0] = .{ .anchor = 0, .head = text.len }; pane.nsel_snap = 1; - applySelRegex(p, pane, &pane.body, c.pat, c.mode); + _ = applySelRegex(p, pane, &pane.body, c.pat, c.mode); var out: [Text.max_selections]modal.Selection = undefined; const got = pane.body.ranges(text, 0, &out); try std.testing.expectEqual(c.want.len, got.n); -- cgit v1.3 From b28053aede910b8714db4f27ef66e59dd96945c0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 21:10:15 -0300 Subject: Prompt: `/` and `?` search a file's body as they are typed (walk UI, pick-ui SPEC.md §7.3, item 2 of line E): its places found from where it began (Places.search, the view's on the key, a frame's budget a tick after), tinted, the current one stronger and selected as soon as it is found (`?`: the last before the caret, once the text between is searched), the gutter marked in the blank after each line number (`▸` the current place's line, `▪` the others'), counted `3/7 · 5 lines` (`3/⋯` while they are found; the lines counted once, at the step they are all found, a pass to the last place), ` · wrapped` past the end from where it began, `0` for no match; a pattern that is no regex keeps the last places, dimmed (Places.paint), the reason in the count; Enter lands, the pattern in `/`, and folds the pane's one search word into its trail (`/dirty/ 3/7`, Trail.setWord: the newest back step, in place of the one it had); n and N write it again in place and echo `/dirty/ 4/7 · line 112` on the message row, ` ↻` after a step round an end, `–/7` with the selection off its places, every step (Prompt.settle beside the places'); the first Esc in normal mode clears a search's tints (SPEC §10.4's first step: C's Esc order when it lands) and the word stays, dimmed (draw.zig, Trail.spanOf); Look on the word lights its places again and selects the one it was on, Exec searches again from the caret (Trail.lookStep, execStep); `*` makes the selection's pattern the search word, no prompt and no query pane; Alt-Enter keeps the places as rows: today's query pane (Query.open .text), the body as it was; `?` is helix's backward search, a new role (modal.zig, config.zig); THE FAULTS THE TESTS MET, both B's and latent until a word step was made: Trail.wordOf returned the text of a COPY of the step (a slice of its stack frame: the word drawn as garbage), now the step's own bytes; Trail.lastCur held `cur` under `n`, though keep() leaves a held pane's at `n` (a search word alone: the dump's load moved it and the digests differed), now `n`; TEST FIRST: Prompt.zig "/ searches the body as it is typed ..." and "? searches back from the caret ..."; Places' `*` test reads the echo, Query's and Stream's `/` tests keep their query pane by Alt-Enter, Query's `*` test reads the word; test/perf.zig --search measures a key in `/` and `s` on a 1 MB file; goldens fsearch, lookrange (the prompt in the tag, the word in the trail); core-test 1016/1182 (166 skipped), shell-test 1229/1232, pane-test 182/184, behaviour-test 274/277 (the failures A's, as on item 1), process-test A's six and none of mine, term-test 17/17, kitty-test 45/45 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/Core.zig | 2 + core/Places.zig | 15 +- core/Prompt.zig | 427 +++++++++++++++++++++++++++++++++++++++- core/Query.zig | 24 ++- core/Stream.zig | 2 + core/Trail.zig | 56 +++++- core/body_layer.zig | 1 + core/config.zig | 1 + core/draw.zig | 2 + core/modal.zig | 2 + core/normal.zig | 33 ++-- test/perf.zig | 32 +++ test/snapshots/fsearch.golden | 137 +++++++------ test/snapshots/lookrange.golden | 33 ++-- 14 files changed, 643 insertions(+), 124 deletions(-) (limited to 'core/normal.zig') diff --git a/core/Core.zig b/core/Core.zig index 987c4f85..95f3a3a8 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1825,6 +1825,8 @@ fn updateInner(p: *Pardes, ev: Event) void { // Each pane's places find a frame's more on a tick, and on any other // step the ones an edit or a scroll left unfound in the view. for (p.panes) |slot| if (slot) |pane| pane.places.settle(p, pane, ev == .tick); + // Their search words and a search prompt with them (Prompt.zig). + pardes.Prompt.settle(p); // Each live query pane's rows, made from what changed this step. pardes.panes.Query.produceAll(p, ev == .tick, ev == .key); _ = p.scratch.reset(.{ .retain_with_limit = memory.limits.scratch_kept }); diff --git a/core/Places.zig b/core/Places.zig index 6c3ac9ab..4c04555b 100644 --- a/core/Places.zig +++ b/core/Places.zig @@ -664,8 +664,13 @@ pub fn paint(set: *Places, p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, if (!set.shows(pane)) return; const f = &pane.file.?; const th = p.theme(); - const strong = p.chromeTheme().search_bg; - const quiet = colors.mix(th.bg orelse th.tag_bg, strong); + var strong = p.chromeTheme().search_bg; + var quiet = colors.mix(th.bg orelse th.tag_bg, strong); + // A search prompt's pattern that is no regex: its last places, dimmed. + if (pardes.Prompt.dims(p, pane)) { + strong = quiet; + quiet = colors.mix(th.bg orelse th.tag_bg, quiet); + } const hits = set.hits.items; if (hits.len == 0) return; const nlines = File.nlines(p.gpa, f); @@ -792,11 +797,11 @@ test "places: * finds them all, n and N step them round the end silently, an edi try std.testing.expectEqual(@as(?usize, 1), set.current); typeKeys(p, "n"); try std.testing.expectEqual(@as(?usize, 2), set.current); - const logged = p.messages.len; typeKeys(p, "n"); try std.testing.expectEqual(@as(?usize, 0), set.current); - // round the end without a word anywhere - try std.testing.expectEqual(logged, p.messages.len); + // round the end: the search word says so, and the message row echoes + // it (Prompt.zig, pick-ui SPEC §7.3) + try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "1/3 ↻ · line 1") != null); typeKeys(p, "N"); try std.testing.expectEqual(@as(?usize, 2), set.current); diff --git a/core/Prompt.zig b/core/Prompt.zig index e5fac06c..53f6cd5c 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -1,5 +1,6 @@ -//! A text prompt in a pane's tag (walk UI, pick-ui SPEC.md §7): `s S K A-K` -//! select by a pattern, `| A-| ! A-! $` pipe through a command, typed in +//! A text prompt in a pane's tag (walk UI, pick-ui SPEC.md §7): `/ ?` +//! search the body, `s S K A-K` select by a pattern, `| A-| ! A-! $` pipe +//! through a command, typed in //! the head's field after the prompt's key cell, the plain word it stands //! for and its count beside it (`s self = Exec Edit x/self/ 4 selections in //! 6 lines`), its effect drawn live in the body on every key; Enter applies @@ -26,6 +27,11 @@ const Field = @import("Field.zig"); const Band = @import("Band.zig"); const TagState = @import("TagState.zig"); const Kind = @import("Kind.zig"); +const Places = @import("Places.zig"); +const Trail = @import("Trail.zig"); +const colors = @import("colors.zig"); +const Surface = @import("surface.zig").Surface; +const File = panes.File; const Pardes = pardes.Pardes; const Pane = panes.Pane; const Text = panes.Text; @@ -47,6 +53,13 @@ bad: bool = false, /// from back), and the lines those it started from cover. found: u16 = 0, scope: u32 = 0, +/// `?` waits for its place: the last before where it began, once the text +/// between them is searched. +seeking: bool = false, +/// The lines a search's places are on, counted once they are all found, at +/// the places' `hits_gen` `counted`. +lines: u32 = 0, +counted: u32 = std.math.maxInt(u32), /// The session's prompt histories: the lines Enter took, newest last. pub const History = struct { @@ -93,8 +106,8 @@ const Info = struct { }; const info = std.EnumArray(Op, Info).init(.{ - .search = .{ .cell = "/", .hint = "⏎ land · C-r history · esc" }, - .search_back = .{ .cell = "?", .hint = "⏎ land · C-r history · esc" }, + .search = .{ .cell = "/", .hint = "⏎ land · M-⏎ as a list · C-r history · esc" }, + .search_back = .{ .cell = "?", .hint = "⏎ land · M-⏎ as a list · C-r history · esc" }, .select = .{ .cell = "s", .hint = "⏎ select · C-r history · esc" }, .split = .{ .cell = "S", .hint = "⏎ select · C-r history · esc" }, .keep = .{ .cell = "K", .hint = "⏎ keep · C-r history · esc" }, @@ -145,6 +158,10 @@ pub fn ofBehavior(how: modal.Normal.PipeBehavior) Op { }; } +fn searches(op: Op) bool { + return op == .search or op == .search_back; +} + fn behavior(op: Op) ?modal.Normal.PipeBehavior { return switch (op) { .pipe => .replace, @@ -158,7 +175,8 @@ fn behavior(op: Op) ?modal.Normal.PipeBehavior { /// Prompt `op` in the tag of `pane`, the pane with the keyboard, for its /// body: its field empty, the body's selections as they are (`s S K A-K` -/// select inside them, Esc puts them back). +/// select inside them, `/ ?` search from the primary one; Esc puts them +/// back). `/ ?` search a file's body only. pub fn open(p: *Pardes, pane: *Pane, op: Op) void { // Nobody is at the keyboard to answer a command a script wrote to a // ctl file: the write fails instead (core/ninep/ctl.zig). @@ -186,6 +204,7 @@ pub fn open(p: *Pardes, pane: *Pane, op: Op) void { scope += @intCast(std.mem.count(u8, text[lo..hi -| @intFromBool(hi > lo and text[hi - 1] == '\n')], "\n") + 1); } } + if (searches(op)) pane.places.remember(p, pane); pane.focus = .body; tagline.exitHeader(p); t.mode = .normal; @@ -210,6 +229,7 @@ pub fn key(p: *Pardes, k: Key) void { const ctrl = k.ctrl and !k.alt; if (hit(k, config.escape)) return cancel(p, pane); if (plain and k.cp == Key.enter) return commit(p, id, pane); + if (k.alt and !k.ctrl and k.cp == Key.enter) return asList(p, id, pane); // Backspace on an empty field: the prompt goes, as Esc. if (plain and k.cp == Key.backspace and s.field.len == 0) return cancel(p, pane); // Up and Down bring its history's lines into the field, the newest first. @@ -236,11 +256,54 @@ pub fn key(p: *Pardes, k: Key) void { /// prompt says why. fn live(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; - const m = mode(s.prompt.op) orelse return; const pat = s.field.text(); + if (mode(s.prompt.op) == null and !searches(s.prompt.op)) return; s.prompt.bad = pat.len > 0 and if (regexp.Regex.compile(pat)) |_| false else |_| true; if (s.prompt.bad) return; - s.prompt.found = @intCast(normal.applySelRegex(p, pane, &pane.body, pat, m)); + if (mode(s.prompt.op)) |m| { + s.prompt.found = @intCast(normal.applySelRegex(p, pane, &pane.body, pat, m)); + return; + } + // A search: its places found from where it began, the view's first, + // the rest a frame's budget a tick (Places.zig); `/` selects the first + // at or after the caret as soon as it is found, `?` the last before it. + const set = &pane.places; + s.prompt.counted = std.math.maxInt(u32); + if (pat.len == 0) { + if (set.from == 0) set.clear(p.gpa); + set.restore(p.settings.tab, pane); + return; + } + const back = s.prompt.op == .search_back; + set.search(p, pane, .typed, 0, pat, false, if (back) null else set.origin(pane), !back); + s.prompt.seeking = back; + if (back) seekBack(p, pane); +} + +/// `?`'s place: the last before where it began once the text between is +/// searched, past the top the last of all once every place is found; +/// none, the body as it was. +fn seekBack(p: *Pardes, pane: *Pane) void { + const s = &p.view().tag_state; + const set = &pane.places; + if (!set.started) return; + const origin = set.origin(pane); + const hits = set.hits.items; + const i = Places.firstFrom(hits, origin); + const whole = set.head_to >= set.lo; + const to: ?usize = if (i > 0 and origin <= set.hi and (whole or hits[i - 1].start >= set.lo)) + i - 1 + else if (set.complete() and hits.len > 0) + hits.len - 1 + else + null; + if (to) |at| { + s.prompt.seeking = false; + return set.goTo(p.settings.tab, pane, at, false); + } + if (!set.complete()) return; + s.prompt.seeking = false; + set.restore(p.settings.tab, pane); } /// Enter: what the field says, applied, and the line kept in its history @@ -258,6 +321,7 @@ fn commit(p: *Pardes, id: usize, pane: *Pane) void { TagState.toRest(p); if (typed.len == 0) return undo(p, pane, op); ring(p, op).add(typed); + if (searches(op)) return land(p, id, pane, op, typed, bad); if (mode(op) != null) { // No regex: the selections it began with, and why. if (bad) { @@ -275,6 +339,44 @@ fn commit(p: *Pardes, id: usize, pane: *Pane) void { } } +/// Enter in `/` or `?`: the place found current, its places tinted from +/// now on for n and N, the pattern in `/`, and the pane's search word +/// `/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; + if (bad) { + undo(p, pane, op); + return p.setMessage(.at(id), regexMessage(pat)); + } + set.fill(p, pane, std.math.maxInt(usize), regexp.Regex.budget); + if (op == .search_back) seekBack(p, pane); + if (set.hits.items.len == 0) { + undo(p, pane, op); + var said: [Field.max + 16]u8 = undefined; + return p.setMessage(.at(id), std.fmt.bufPrint(&said, "/{s}/: no match", .{pat}) catch "no match"); + } + p.registers.put(p.gpa, '/', pat, 0, 1, true) catch |err| return p.reportError(.at(id), "search", err); + p.registers.last_search = '/'; + set.provenance = .search; + set.forget(p.gpa); + word(pane, if (op == .search_back) '?' else '/', pat); +} + +/// Alt-Enter in `/` or `?`: the places kept as a list, a row each, in the +/// pane's query pane (Query.zig), the body as it was before the prompt. +fn asList(p: *Pardes, id: usize, pane: *Pane) void { + const s = &p.view().tag_state; + const op = s.prompt.op; + if (!searches(op)) return; + var kept: [Field.max]u8 = undefined; + const typed = kept[0..s.field.len]; + @memcpy(typed, s.field.text()); + s.kind = .rest; + TagState.toRest(p); + undo(p, pane, op); + panes.Query.open(p, id, .text, typed, true); +} + /// Esc: the prompt goes and the body is as it was before it. fn cancel(p: *Pardes, pane: *Pane) void { const op = p.view().tag_state.prompt.op; @@ -287,6 +389,11 @@ fn cancel(p: *Pardes, pane: *Pane) void { fn undo(p: *Pardes, pane: *Pane, op: Op) void { if (mode(op)) |m| _ = normal.applySelRegex(p, pane, &pane.body, "", m); pane.nsel_snap = 0; + if (!searches(op)) return; + const set = &pane.places; + if (set.from == 0 and set.provenance == .typed) set.clear(p.gpa); + set.restore(p.settings.tab, pane); + set.forget(p.gpa); } /// The prompt left by something else than its keys (TagState.toRest: a @@ -317,6 +424,201 @@ pub fn selecting(p: *const Pardes, pane: *const Pane) ?struct { dim: bool } { return .{ .dim = s.prompt.bad }; } +/// Whether `pane`'s places are a search prompt's last good ones, its field +/// no regex now: tinted dimmed (Places.paint). +pub fn dims(p: *const Pardes, pane: *const Pane) bool { + const s = &p.view().tag_state; + return s.kind == .prompt and searches(s.prompt.op) and s.host == pane.serial and s.prompt.bad; +} + +// ---- the search word (SPEC §7.3) ---- + +/// Pane `pane`'s search word, `/re/ 3/7`: one a pane, its `index` the +/// length of its `/re/` with bit 16 set after n or N went round an end. +fn wordOf(pane: *Pane) ?*Trail.Word { + for (pane.trail.steps[0..pane.trail.n]) |*step| if (step.* == .search) return &step.search; + return null; +} + +fn patternOf(w: *const Trail.Word) []const u8 { + const end: u16 = @truncate(w.index); + return w.text.slice()[1 .. end - 1]; +} + +/// Whether the places the pane's body shows are its search word's. +fn lights(pane: *const Pane, w: *const Trail.Word) bool { + const set = &pane.places; + return set.shows(pane) and set.provenance == .search and !set.literal and std.mem.eql(u8, set.pattern, patternOf(w)); +} + +/// `pat`, delimited by `delim`, as pane `pane`'s one search word, in place +/// of the one it had, written for where its places stand. +fn word(pane: *Pane, delim: u8, pat: []const u8) void { + // Its pattern and its place in the trail's room, else none. + if (pat.len + 2 + 32 > Trail.max_text) return; + var w: Trail.Word = .{}; + _ = w.text.print("{c}{s}{c}", .{ delim, pat, delim }); + w.index = w.text.len; + pane.trail.setWord(.{ .search = w }); + refresh(pane, false); +} + +/// The search word's place among its places written after its pattern: +/// `3/7`, `–/7` with the selection off them, `⋯` for the count while they +/// are found, ` ↻` after n or N went round an end (`wrapped`; null keeps +/// what it said). +fn refresh(pane: *Pane, wrapped: ?bool) void { + const w = wordOf(pane) orelse return; + if (!lights(pane, w)) return; + const set = &pane.places; + const f = &pane.file.?; + const end: u16 = @truncate(w.index); + const round = wrapped orelse (w.index >> 16 != 0); + const hits = set.hits.items; + const sel = pane.body.primaryRange(f.content, 0); + const on: ?usize = if (set.current) |c| (if (c < hits.len and hits[c].start == @min(sel.anchor, sel.head) and hits[c].end == @max(sel.anchor, sel.head)) c else null) else null; + var buf: [48]u8 = undefined; + var out: std.Io.Writer = .fixed(&buf); + out.writeByte(' ') catch return; + if (on) |c| out.print("{d}", .{c + 1}) catch return else out.writeAll("–") catch return; + if (set.complete()) out.print("/{d}", .{hits.len}) catch return else out.writeAll("/⋯") catch return; + if (round) out.writeAll(" ↻") catch return; + const now = w.text.slice(); + if (std.mem.eql(u8, now[end..], out.buffered()) and (w.index >> 16 != 0) == round) return; + var text: [Trail.max_text]u8 = undefined; + @memcpy(text[0..end], now[0..end]); + @memcpy(text[end..][0..out.buffered().len], out.buffered()); + w.text.set(text[0 .. end + out.buffered().len]); + w.index = end | @as(u32, @intFromBool(round)) << 16; +} + +/// After each step (Core.zig, beside the places'): every search word says +/// where its places stand; the live view's search prompt finds `?`'s place +/// and counts the lines its places are on once they are all found. +pub fn settle(p: *Pardes) void { + for (p.panes) |slot| if (slot) |pane| refresh(pane, null); + const s = &p.view().tag_state; + if (s.kind != .prompt or !searches(s.prompt.op)) return; + const pane = p.panes[TagState.hostOf(p) orelse return].?; + if (s.prompt.seeking) seekBack(p, pane); + const set = &pane.places; + if (!set.shows(pane) or !set.complete() or s.prompt.counted == set.hits_gen) return; + // A line holds the next place unless a newline lies between them: a + // pass over the text to the last place, once. + const text = pane.file.?.content; + var lines: u32 = 0; + var prev: ?usize = null; + for (set.hits.items) |h| { + if (prev == null or std.mem.indexOfScalarPos(u8, text[0..h.start], prev.?, '\n') != null) lines += 1; + prev = h.start; + } + s.prompt.lines = lines; + s.prompt.counted = set.hits_gen; +} + +/// n or N stepped pane `id`'s places from place `before`: its search word +/// says where they stand, ` ↻` when the step went round an end, and the +/// message row echoes it with the line: `/dirty/ 4/7 · line 112`. +pub fn stepped(p: *Pardes, id: usize, forward: bool, before: ?usize) void { + const pane = p.panes[id] orelse return; + const w = wordOf(pane) orelse return; + const set = &pane.places; + if (!lights(pane, w)) return; + const after = set.current orelse 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; + p.setMessage(.at(id), std.fmt.bufPrint(&said, "{s} · line {d}", .{ w.text.slice(), Places.lineOf(index, set.hits.items[after].start) + 1 }) catch return); +} + +/// `*`: pattern `pat`, the selection's (in `/` already), lights its places +/// in `pane`'s body and is its search word, no prompt asked (SPEC §7.3). +pub fn star(p: *Pardes, pane: *Pane, pat: []const u8) void { + pane.places.search(p, pane, .search, 0, pat, false, null, false); + word(pane, '/', pat); +} + +/// Look on pane `id`'s search word (SPEC §6.3): its places lit again and +/// the one it was on selected. +pub fn lookWord(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const w = wordOf(pane) orelse return; + const set = &pane.places; + const pat = patternOf(w); + p.registers.put(p.gpa, '/', pat, 0, 1, true) catch |err| return p.reportError(.at(id), "search", err); + p.registers.last_search = '/'; + 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 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); +} + +/// Exec on it: searched again from the caret, as its prompt and Enter +/// would, the next place when they are lit already (n). +pub fn execWord(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const w = wordOf(pane) orelse return; + const set = &pane.places; + const pat = patternOf(w); + p.registers.put(p.gpa, '/', pat, 0, 1, true) catch |err| return p.reportError(.at(id), "search", err); + p.registers.last_search = '/'; + p.view().active = id; + if (lights(pane, w)) { + const before = set.current; + set.step(p, pane, true, 1, false); + return stepped(p, id, true, before); + } + set.search(p, pane, .search, 0, pat, false, set.origin(pane), true); +} + +/// The span of pane `pane`'s tag its search word takes while its places no +/// longer light it (the first Esc cleared them): drawn dimmed (draw.zig). +pub fn unlit(pane: *Pane) ?[2]usize { + const t = &pane.trail; + for (t.steps[0..t.cur], 0..) |*step, i| if (step.* == .search) { + if (lights(pane, &step.search) or t.cur - i > Trail.shown_steps) return null; + return t.spanOf(i); + }; + return null; +} + +/// 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. +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; + const f = &(pane.file orelse 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 y = p.bodyTop(pane, r); + const ink = p.chromeTheme().box; + const nlines = File.nlines(p.gpa, f); + 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 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; + var style = s.at(x, y + vr).style; + style.fg = .{ .rgb = ink }; + s.set(x, y + vr, if (current) "▸" else "▪", style); + } +} + fn regexMessage(pat: []const u8) []const u8 { return if (regexp.Regex.compile(pat)) |_| "" else |err| regexp.Regex.message(err); } @@ -390,9 +692,24 @@ fn styled(arena: std.mem.Allocator, text: *std.ArrayList(u8), spans: *std.ArrayL } /// The prompt's count after its field: what it found (`4 selections in 6 -/// lines`, `keeps 3 of 9`, `0`). -fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *const Pane, pr: *const Prompt) !void { +/// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `3/⋯` while its places are +/// found, `0`). +fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !void { switch (pr.op) { + .search, .search_back => { + const set = &pane.places; + if (!set.shows(pane)) return; + 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, "–"); + 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 origin = set.origin(pane); + if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " · wrapped"); + }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") else @@ -531,3 +848,95 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba p.update(.{ .key = .{ .cp = Key.escape } }); while (p.nextEffect()) |effect| try std.testing.expect(effect != .pipe); } + +const dirty = "dirty one\nclean\ndirty two dirty\nclean\ndirty three\ndirty four dirty\nclean\ndirty five\n"; + +/// The glyph in the blank after the line number of body row `row`. +fn gutterAt(p: *Pardes, arena: std.mem.Allocator, row: u16) ![]const u8 { + p.sync(); + const s = try p.render(arena); + const pane = p.panes[0].?; + const r = p.view().rects[0]; + const x = r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1); + return s.at(x, p.bodyTop(pane, r) + row).grapheme(); +} + +test "/ searches the body as it is typed: the current place selected, the gutter marked, counted 3/7 · 5 lines; Enter folds /dirty/ 3/7 into the trail; n says where it went, round the end too; the first Esc clears the tints and dims the word, Look on it lights them again" { + 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(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 2; + pane.body.cur_col = 6; + typeKeys(p, "/dirty"); + try std.testing.expect(has(try tagOf(p), " / dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); + try std.testing.expectEqual(@as(?usize, 2), pane.places.current); + try std.testing.expectEqualStrings("▪", try gutterAt(p, frame.allocator(), 0)); + try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 1)); + try std.testing.expectEqualStrings("▸", try gutterAt(p, frame.allocator(), 2)); + try std.testing.expectEqualStrings("▪", try gutterAt(p, frame.allocator(), 7)); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 3/7 › /test.txt ")); + try std.testing.expectEqualStrings("dirty", p.registers.value('/', 0).?); + try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 0)); + // n: the word in place, and the line said. + typeKeys(p, "n"); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 4/7 › ")); + try std.testing.expect(has(pane.msg[0..pane.msg_len], "/dirty/ 4/7 · line 5")); + typeKeys(p, "nnnn"); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 1/7 ↻ › ")); + // Esc: the tints go, the word stays, dimmed; Look on it lights them + // again, the place it was on selected. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(!pane.places.started); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ 1/7 ↻ › ")); + try std.testing.expect(unlit(pane) != null); + typeKeys(p, "j"); + Trail.lookStep(p, 0, 0); + try std.testing.expect(pane.places.started and unlit(pane) == null); + try std.testing.expectEqual(@as(?usize, 0), pane.places.current); + const sel = pane.body.primaryRange(pane.file.?.content, 0); + try std.testing.expectEqual(@as(usize, 0), @min(sel.anchor, sel.head)); + // Off a place: –/7. + typeKeys(p, "j"); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/dirty/ –/7")); + // One search word a pane: a new search takes its place. + typeKeys(p, "gg/clean"); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/clean/ 1/3 › /test.txt ")); + try std.testing.expectEqual(@as(u8, 1), pane.trail.n); +} + +test "? searches back from the caret; no match is 0; a pattern that is no regex keeps the last places dimmed; Esc puts the body back; * makes the search word with no prompt; Alt-Enter keeps the places as the query pane's rows" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 2; + pane.body.cur_col = 6; + const was = pane.body.primaryRange(pane.file.?.content, 0); + typeKeys(p, "?dirty"); + try std.testing.expect(has(try tagOf(p), " ? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); + typeKeys(p, "x"); + try std.testing.expect(has(try tagOf(p), "= Look ?dirtyx? 0 ⏎ land")); + try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); + p.update(.{ .key = .{ .cp = Key.backspace } }); + typeKeys(p, "("); + try std.testing.expect(has(try tagOf(p), " bad regular expression · ")); + try std.testing.expect(dims(p, pane) and pane.places.started); + try std.testing.expectEqualStrings("dirty", pane.places.pattern); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(!pane.places.started); + try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); + try std.testing.expectEqual(@as(u8, 0), pane.trail.n); + // `*` on `dirty` at the top: its places, its word, no prompt. + typeKeys(p, "gge*"); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p), "/\\bdirty\\b/ 1/7 › ")); + // Alt-Enter: the query pane, its line `/dirty`. + typeKeys(p, "/dirty"); + p.update(.{ .key = .{ .cp = Key.enter, .alt = true } }); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expect(p.panes[p.view().active].?.query != null); +} diff --git a/core/Query.zig b/core/Query.zig index feebb6cd..3ac87dfb 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -2134,6 +2134,12 @@ pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) // ---- tests ---- +/// `/` and `typed` in the tag's prompt, kept as the query pane (Alt-Enter). +fn slash(p: *Pardes, typed: []const u8) void { + typeKeys(p, typed); + p.update(.{ .key = .{ .cp = Key.enter, .alt = true } }); +} + fn typeKeys(p: *Pardes, text: []const u8) void { for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } @@ -2169,7 +2175,7 @@ test "a / query: keystroke by keystroke, the owner shows the set, rows from its // cursor on line 3 (row 2) pane.body.cur_row = 2; pane.body.cur_col = 0; - typeKeys(p, "/"); + slash(p, "/"); const qid = queryPane(p).?; const qp = p.panes[qid].?; try std.testing.expectEqual(@as(?usize, 0), layout.ownerOf(p, qid)); @@ -2226,7 +2232,7 @@ test "a dump holding a query pane, live or frozen, restores it as it was" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); _ = try p.setTestFile("alpha one\nbeta\nalphabet\n"); - typeKeys(p, "/alpha"); + slash(p, "/alpha"); for (0..2) |_| { const qid = queryPane(p).?; try pardes.dump.dumpState(p); @@ -2245,7 +2251,7 @@ test "a / query: Enter lands and freezes, the register filled, n and N step sile const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); const pane = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma alpha\nalpha two\ndelta\n"); - typeKeys(p, "/alpha"); + slash(p, "/alpha"); const qid = queryPane(p).?; const qp = p.panes[qid].?; const q = qp.query.?; @@ -2274,7 +2280,7 @@ test "a / query: Enter lands and freezes, the register filled, n and N step sile typeKeys(p, "N"); try std.testing.expectEqual(@as(?usize, 3), pane.places.current); // a later `/` reuses the pane: line 1 fresh, the rows the new query's - typeKeys(p, "/"); + slash(p, "/"); try std.testing.expectEqual(qid, queryPane(p).?); try std.testing.expectEqual(qid, p.view().active); try std.testing.expect(!q.frozen); @@ -2419,11 +2425,12 @@ test "* seeds line 1 and keeps the keyboard; SPC b b picks a pane, SPC s s a dec const pane = try p.setTestFile("const alpha = 1;\nfn beta() void {\n _ = alpha;\n}\nfn gamma() void {}\n"); p.gpa.free(pane.file.?.path); pane.file.?.path = try p.gpa.dupe(u8, "/tmp/x.zig"); - // `*` on `const`: line 1 `/\bconst\b`, the keyboard still in the text + // `*` on `const`: its places and the pane's search word, no query + // pane, the keyboard still in the text (Prompt.zig) typeKeys(p, "e*"); - const qid = queryPane(p).?; + try std.testing.expect(queryPane(p) == null); try std.testing.expectEqual(@as(usize, 0), p.view().active); - try std.testing.expect(std.mem.startsWith(u8, lineOne(p.panes[qid].?.file.?.content), "/")); + try std.testing.expect(pane.trail.n == 1 and pane.trail.steps[0] == .search); try std.testing.expect(pane.places.started); typeKeys(p, "n"); try std.testing.expectEqual(@as(?usize, 0), pane.places.current); @@ -2433,7 +2440,6 @@ test "* seeds line 1 and keeps the keyboard; SPC b b picks a pane, SPC s s a dec p.view().active = 0; leader(p, "bb"); const bid = queryPane(p).?; - try std.testing.expectEqual(bid, qid); try std.testing.expectEqual(Source.panes, p.panes[bid].?.query.?.source); try std.testing.expect(p.panes[bid].?.query.?.total >= 1); // SPC s s: the declarations, fuzzy; Enter lands on the line @@ -2512,7 +2518,7 @@ test "B1 on a query pane's rows places the caret and selects, nothing else, the const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); const pane = try p.setTestFile("alpha one\nbeta\nalphabet\ngamma alpha\nalpha two\ndelta\n"); - typeKeys(p, "/alpha"); + slash(p, "/alpha"); const qid = queryPane(p).?; const qp = p.panes[qid].?; const q = qp.query.?; diff --git a/core/Stream.zig b/core/Stream.zig index 2f700cb4..a933c014 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -1343,7 +1343,9 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, p.stream.cancelParse(); shell.q.deinit(); } + // `/` asks in the tag; Alt-Enter keeps it as the query pane (Prompt.zig). for ("/needle") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + p.update(.{ .key = .{ .cp = pardes.Key.enter, .alt = true } }); const qid = for (p.panes, 0..) |slot, i| { if (slot) |qp| if (qp.query != null) break i; } else return error.NoQueryPane; diff --git a/core/Trail.zig b/core/Trail.zig index 7166d810..0ea893e2 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -38,8 +38,10 @@ cur: u8 = 0, /// Kept, or edited since it was shown: it no longer walks (§6.5). held: bool = false, +/// `cur` is at most `n`: a held pane's (keep, a search's or a recipe's +/// word alone) has no current place, past its steps. pub fn lastCur(t: *const Trail) usize { - return t.n -| 1; + return t.n; } /// One step of a walk, as its word says it (§2 "trail word"). @@ -194,6 +196,31 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { return free; } +/// `s`, a search's or a recipe's word, the pane's one of its kind (§7.3, +/// §7.5): the one it had goes, and it is the newest back step. +pub fn setWord(t: *Trail, s: Step) void { + var w: u8 = 0; + for (t.steps[0..t.n], 0..) |old, i| { + if (std.meta.activeTag(old) == std.meta.activeTag(s)) { + if (i < t.cur) t.cur -= 1; + continue; + } + t.steps[w] = old; + w += 1; + } + t.n = w; + if (t.n == max_steps) { + const first: usize = @intFromBool(t.steps[0] == .owner); + std.mem.copyForwards(Step, t.steps[first .. max_steps - 1], t.steps[first + 1 ..]); + t.n -= 1; + t.cur -|= 1; + } + std.mem.copyBackwards(Step, t.steps[t.cur + 1 .. t.n + 1], t.steps[t.cur..t.n]); + t.steps[t.cur] = s; + t.n += 1; + t.cur += 1; +} + /// Pane `id` held (§6.5): Keep, or an edit. It no longer walks, so the /// walk leaves its trail (places, lists, its owner, forward steps); words /// about its own text (a search, a recipe) stay. The next jump from its @@ -246,12 +273,13 @@ pub const Part = enum { back, forward }; /// Step `i`'s word: `‹@p3`, `rank.zig:14`, a list's, search's or recipe's /// own text; `·` when it is folded. fn wordOf(t: *const Trail, i: usize, buf: *[max_text + 16]u8) []const u8 { - const s = t.steps[i]; - if (s != .owner and i < t.cur and t.cur - i > shown_steps) return "·"; - return switch (s) { + const s = &t.steps[i]; + if (s.* != .owner and i < t.cur and t.cur - i > shown_steps) return "·"; + // The step itself, not a copy: a word's text is the step's own bytes. + return switch (s.*) { .owner => |serial| std.fmt.bufPrint(buf, "‹" ++ config.pane_addr ++ "{d}", .{serial}) catch unreachable, // a u32 fits .place => |at| std.fmt.bufPrint(buf, "{s}:{d}", .{ std.Io.Dir.path.basename(at.path.slice()), at.line }) catch unreachable, // a basename and a u32 fit - .list, .search, .recipe => |w| w.text.slice(), + .list, .search, .recipe => |*w| w.text.slice(), }; } @@ -285,6 +313,14 @@ pub fn bytes(t: *const Trail, part: Part) usize { return n; } +/// Where back step `i`'s word is in what `write` writes for the back steps. +pub fn spanOf(t: *const Trail, i: usize) [2]usize { + var buf: [max_text + 16]u8 = undefined; + var x: usize = 0; + for (0..i) |j| x += wordOf(t, j, &buf).len + sep.len; + return .{ x, x + wordOf(t, i, &buf).len }; +} + /// The step whose word in `part` is at byte `off` of what `write` wrote /// for it, null on a separator or past them. pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { @@ -302,8 +338,8 @@ pub fn stepAt(t: *const Trail, part: Part, off: usize) ?u8 { } /// Look on step `i` of pane `id`'s trail (§6.3): a place is gone back or -/// forward to, the owner focused; a list, a search or a recipe is the -/// pickers' and the prompts' to take (not yet: said). +/// forward to, the owner focused, a search lit again (Prompt.zig); a list +/// or a recipe is the pickers' and the prompts' to take (not yet: said). pub fn lookStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { @@ -311,7 +347,8 @@ pub fn lookStep(p: *Pardes, id: usize, i: u8) void { .owner => |serial| if (p.paneBySerial(serial)) |owner| { p.view().active = owner; } else p.reportFailure(id, "walk: its owner is closed"), - .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not walked yet"), + .search => pardes.Prompt.lookWord(p, id), + .list, .recipe => p.setMessage(.at(id), "walk: a list or a recipe is not walked yet"), } } @@ -321,7 +358,8 @@ pub fn execStep(p: *Pardes, id: usize, i: u8) void { const pane = p.panes[id] orelse return; switch (pane.trail.steps[i]) { .place, .owner => lookStep(p, id, i), - .list, .search, .recipe => p.setMessage(.at(id), "walk: a list, a search or a recipe is not run again yet"), + .search => pardes.Prompt.execWord(p, id), + .list, .recipe => p.setMessage(.at(id), "walk: a list or a recipe is not run again yet"), } } diff --git a/core/body_layer.zig b/core/body_layer.zig index 4176514b..de822674 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -347,6 +347,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.Tables.paint(p, s, pane, r, tx, tw, body_h); drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg); pane.places.paint(p, s, pane, r, body_h); + pardes.Prompt.paintGutter(p, s, pane, r, body_h); panes.Query.paint(p, s, pane, r, body_h); } else if (pane.isTerminal()) { ansi.recolor(p, s, pane, r, tx, tw, body_h, body); diff --git a/core/config.zig b/core/config.zig index 6adebe1b..a3d0fb84 100644 --- a/core/config.zig +++ b/core/config.zig @@ -266,6 +266,7 @@ pub const tag_tty = "$"; pub const command_line: []const Chord = &.{.{ .cp = ':' }}; pub const search: []const Chord = &.{.{ .cp = '/' }}; +pub const search_back: []const Chord = &.{.{ .cp = '?' }}; pub const search_next: []const Chord = &.{.{ .cp = 'n' }}; pub const search_prev: []const Chord = &.{.{ .cp = 'N' }}; /// Typing on a query pane's line 1 (Query.zig): the next and the previous diff --git a/core/draw.zig b/core/draw.zig index 3365f0a8..0b6cebcf 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -979,6 +979,8 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, // A walk's forward steps, after the name, dimmed (Trail.zig). const ahead = tagline.forwardSpan(pane); if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), chrome.lineno); + // A search word its places no longer light, dimmed (Prompt.zig). + if (pardes.Prompt.unlit(pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), chrome.lineno); // The head's keys in the key ink, its dimmed, underlined and struck // words (TagState.Span), over whichever rows they wrapped onto. if (head) |h| for (h.spans) |span| { diff --git a/core/modal.zig b/core/modal.zig index fda948a2..41ec6739 100644 --- a/core/modal.zig +++ b/core/modal.zig @@ -160,6 +160,7 @@ pub const Normal = struct { macro_replay, repeat_insert, search, + search_back, search_next, search_prev, search_selection, @@ -305,6 +306,7 @@ pub const Normal = struct { .macro_replay, .repeat_insert, .search, + .search_back, .search_next, .search_prev, .search_selection, diff --git a/core/normal.zig b/core/normal.zig index c59a847c..307f1296 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -1088,7 +1088,7 @@ fn shrinkSelToLineBounds(tab: u16, t: *Text, text: []const u8, range: modal.Sele pub fn textRole(role: modal.Normal.Role, tree: bool) bool { return switch (role) { .all_children, .all_siblings, .expand_node, .goto_class, .goto_comment, .goto_entry, .goto_function, .goto_parameter, .goto_test, .goto_xml_element, .next_sibling, .parent_node_end, .parent_node_start, .prev_sibling, .shrink_node => tree, - .append_output, .command_line, .format, .goto_callstack, .goto_change, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_implementation, .goto_references, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .page_down, .page_up, .pipe_selection, .pipe_selection_to, .remove_regex, .repeat_insert, .search, .search_next, .search_prev, .select_regex, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, + .append_output, .command_line, .format, .goto_callstack, .goto_change, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_implementation, .goto_references, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .page_down, .page_up, .pipe_selection, .pipe_selection_to, .remove_regex, .repeat_insert, .search, .search_back, .search_next, .search_prev, .select_regex, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false, else => true, }; } @@ -1107,6 +1107,12 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { if (comptime Editor.lib) return; // typed again by a macro, Esc is only helix's if (e.p.macro.replaying > 0) return; + // The first Esc clears a search's tints (pick-ui SPEC §7.3, + // §10.4's first step); its word stays in the trail, dimmed. + if (t.what == .body) if (t.pane()) |pane| if (pane.places.started and pane.places.provenance == .search) { + pane.places.clear(e.p.gpa); + return; + }; tagline.exitHeader(e.p); // out of a header too, as Shift-Esc return exec.runBuiltin(e.p, .Last, e.p.view().active, "", null); }, @@ -1253,14 +1259,13 @@ pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void { .decrement => return edit.normalAdjustNumber(e, t, -@as(i64, @intCast(a.count))), .search_selection, .search_selection_word => { try searchSelection(e, t, text, a.role == .search_selection_word); - // The pattern is line 1 of this text's query pane, made or - // reused, the keyboard staying here: n and N step its places. + // The pattern lights its places and is the pane's search word, + // no prompt asked (Prompt.zig): n and N step them. const pane = t.pane() orelse return; if (t.what == .body and pane.file != null and pane.query == null) { const name = if (e.register.* != 0) e.register.* else '/'; const pat = e.registers.value(name, 0) orelse return; - panes.Query.open(e.p, e.p.view().active, .text, pat, false); - pane.places.provenance = .search; + Prompt.star(e.p, pane, pat); } }, // prefix keys: what they begin is the command @@ -1385,10 +1390,11 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! if (t.what == .body) return Prompt.open(p, pane, Prompt.ofBehavior(how)); return panes.startPrompt(p, pane, t, .{ .pipe = how }); }, - // `/` in a text is its query pane's line 1 (Query.zig); a - // terminal's still asks on the notice band. - .search => if (pane.file != null and t.what == .body) - panes.Query.open(p, v.active, .text, "", true) + // `/` and `?` in a file's text search it in its tag (Prompt.zig), + // Alt-Enter keeping the places as its query pane's rows (Query.zig); + // a terminal's still asks on the notice band. + .search, .search_back => if (pane.file != null and t.what == .body) + return Prompt.open(p, pane, if (a.role == .search) .search else .search_back) else return panes.startPrompt(p, pane, &pane.body, .{ .search = config.search_marker }), // In a file's text, n and N step its places while they answer for @@ -1397,9 +1403,12 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! // places the rows hold, never the disk (a motion). .search_next, .search_prev => if (t.what == .body and pane.addon != null) return look.lookWalk(p, if (a.role == .search_next) 1 else -1, false) - else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) - return pane.places.step(p, pane, a.role == .search_next, a.count, t.select) - else + else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane)) { + // The pane's search word says where it went (Prompt.zig). + const was = pane.places.current; + pane.places.step(p, pane, a.role == .search_next, a.count, t.select); + return Prompt.stepped(p, v.active, a.role == .search_next, was); + } else return searchStep(p.editor(), t, text, a.role == .search_next, a.count), .goto_word => return startJump(p, pane, t), .macro_record => { diff --git a/test/perf.zig b/test/perf.zig index f36d64ac..4ba061e1 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -1822,6 +1822,38 @@ fn measureSearch(io: std.Io) !void { } slow.report("fill", pattern, set.hits.items.len); } + + // A key typed into a text prompt in the tag (core/Prompt.zig) on a 1 MB + // file, with its frame: `/` from the middle (its places from the view on, + // a frame's budget a tick after), `s` over the whole text (`%`). + const mb: Fixture = .{ .name = "onemeg", .lines = 17_000, .cols = 60 }; + const mb_path = try std.fmt.allocPrint(gpa, "{s}/{s}.zig", .{ dir, mb.name }); + defer gpa.free(mb_path); + const mb_text = try generate(mb); + try writeFile(mb_path, mb_text); + std.debug.print("prompt keys over {d} bytes\n", .{mb_text.len}); + gpa.free(mb_text); + for ([_][]const u8{ "/", "%s" }) |opener| for ([_][]const u8{ "helper", "qqq" }) |typed| { + const core = try boot(); + defer core.deinit(); + pardes.look.lookAt(core, 0, mb_path); + pump(core); + const id = filePane(core) orelse return error.OpenDidNotOpenFile; + const pane = core.panes[id].?; + seek(pane, mb, 0); + pane.file.?.scroll = mb.lines / 2; + pane.body.cur_row = @intCast(mb.lines / 2); + _ = try frame(core); + for (opener) |c| core.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = try frame(core); + for (typed, 0..) |c, n| { + const was = counter.read(); + const t0 = nowNs(); + core.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = try frame(core); + std.debug.print("prompt {s} \"{s}\": {d} instructions, {d} us (key and frame)\n", .{ opener, typed[0 .. n + 1], counter.read() -| was, (nowNs() -| t0) / 1000 }); + } + }; } /// The longest of a scan's calls. diff --git a/test/snapshots/fsearch.golden b/test/snapshots/fsearch.golden index 71a9900a..6997dbcb 100644 --- a/test/snapshots/fsearch.golden +++ b/test/snapshots/fsearch.golden @@ -19,69 +19,82 @@ | /tmp/pardes-snap/fsearch/cwd Term+bash Save Mode Filter Collapse Del | ls | g.txt -== snap slash grid=180x31 cursor=3,10 -|9: ^ +Search 0 Del -|10: / -|11: -|12: -|13: -|14: -|15: -|16: -== snap typing grid=180x31 cursor=7,10 -|9: ^ +Search 1/3 Del -|10: /MARK -|11: g.txt:2:1 MARK a -|12: g.txt:6:1 MARK b -|13: g.txt:10:1 MARK c +== snap slash grid=180x31 cursor=39,6 +|6: ^ /tmp/pardes-snap/fsearch/cwd/g.txt / ⏎ land · M-⏎ as a list · C-r history · esc +== snap typing grid=180x31 cursor=43,6 +|6: ^ /tmp/pardes-snap/fsearch/cwd/g.txt / MARK = Look /MARK/ 1/3 · 3 lines ⏎ land · M-⏎ +|7: as a list · C-r history · esc +|8: 1 l1 +|9: 2▸MARK a +|10: 3 l3 +|11: 4 l4 +|12: 5 l5 +|13: 6▪MARK b +|14: 7 l7 +|15: 8 l8 +|16: 9 l9 == style typing grid=180x31 |0: 0-1 d,#1d2824, 2-179 #b2c3b8,#1d2824, |1: 0-1 d,#887455, 2-89 #becfc4,#314738, 90-91 #becfc4,#887455, 92-179 #b2c3b8,#1d2824, -|2: 0-1 d,#496656, 2-30 #b2c3b8,#1d2824, 31-40 #d3b0d9,#1d2824, 41-89 #b2c3b8,#1d2824, 90-91 #b2c3b8,#496656, 92-120 #b2c3b8,#1d2824, 121-130 #d3b0d9,#1d2824, 131-179 #b2c3b8,#1d2824, -|3: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|4: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|5: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|6: 0-1 d,#496656, 2-30 #b2c3b8,#1d2824, 31-36 #d3b0d9,#1d2824, 37-89 #b2c3b8,#1d2824, 90-90 #b2c3b8,#608269, 91-179 #b2c3b8,#0d1410, -|7: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|8: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-10 #f5f7ee,#3c614a, 11-11 #f5f7ee,#0d1410, 12-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|9: 0-1 #000000,#add38a, 2-89 #becfc4,#314738, 90-90 #becfc4,#608269, 91-179 #becfc4,#0d1410, -|10: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|11: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#243a2d, 16-19 #f7e4a5,#5e4923, 20-20 #f7e4a5,#243a2d, 21-89 #e5eee5,#243a2d, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|12: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#0d1410, 16-19 #f7e4a5,#352e19, 20-20 #f7e4a5,#0d1410, 21-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|13: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#0d1410, 16-19 #f7e4a5,#352e19, 20-20 #f7e4a5,#0d1410, 21-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|14: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|15: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|16: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|17: 0-1 d,#496656, 2-30 #b2c3b8,#1d2824, 31-40 #d3b0d9,#1d2824, 41-89 #b2c3b8,#1d2824, 90-90 #b2c3b8,#608269, 91-179 #b2c3b8,#0d1410, -|18: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|19: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|20: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|21: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|22: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|23: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|24: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|25: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|26: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|27: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|28: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|29: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|30: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -== snap ctrln grid=180x31 cursor=7,10 -|7: 5 l5 -|8: 6 MARK b -|9: ^ +Search 2/3 Del +|2: 0-1 d,#496656,f 2-30 #b2c3b8,#1d2824,f 31-40 #d3b0d9,#1d2824,f 41-89 #b2c3b8,#1d2824,f 90-91 #b2c3b8,#496656,f 92-120 #b2c3b8,#1d2824,f 121-130 #d3b0d9,#1d2824,f 131-179 #b2c3b8,#1d2824,f +|3: 0-0 d,#608269,f 1-1 d,#0d1410,f 2-89 #e5eee5,#0d1410,f 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|4: 0-0 d,#608269,f 1-1 d,#0d1410,f 2-89 #e5eee5,#0d1410,f 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|5: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|6: 0-1 #000000,#add38a, 2-30 #becfc4,#314738, 31-36 #dfbce5,#314738, 37-37 #add38a,#314738,b 38-38 #add38a,#314738, 39-42 #becfc4,#314738, 43-50 #becfc4,#314738,f 51-76 #becfc4,#314738, 77-89 #becfc4,#314738,f 90-90 #becfc4,#608269,f 91-179 #becfc4,#0d1410,f +|7: 0-1 d,#314738, 2-30 #becfc4,#314738,f 31-89 #becfc4,#314738, 90-90 #becfc4,#608269,f 91-179 #becfc4,#0d1410,f +|8: 0-0 d,#e5eee5, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|9: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-5 #616f64,#0d1410,b 6-6 #add38a,#0d1410,b 7-10 #f5f7ee,#3c614a, 11-11 #f5f7ee,#0d1410, 12-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|10: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|11: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|12: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|13: 0-0 d,#608269, 1-4 d,#0d1410, 5-5 #59675c,#0d1410, 6-6 #add38a,#0d1410, 7-10 #e5eee5,#352e19, 11-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|14: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|15: 0-0 d,#152019, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|16: 0-0 d,#152019, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|17: 0-1 d,#496656,f 2-30 #b2c3b8,#1d2824,f 31-40 #d3b0d9,#1d2824,f 41-89 #b2c3b8,#1d2824,f 90-90 #b2c3b8,#608269,f 91-179 #b2c3b8,#0d1410,f +|18: 0-0 d,#608269,f 1-1 d,#0d1410,f 2-89 #e5eee5,#0d1410,f 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|19: 0-0 d,#608269,f 1-1 d,#0d1410,f 2-89 #e5eee5,#0d1410,f 90-90 #e5eee5,#608269,f 91-179 #e5eee5,#0d1410,f +|20: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|21: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|22: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|23: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|24: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|25: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|26: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|27: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|28: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|29: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +|30: 0-0 d,#608269,f 1-89 d,#0d1410,f 90-90 d,#608269,f 91-179 d,#0d1410,f +== snap ctrln grid=180x31 cursor=43,6 == snap landed grid=180x31 cursor=10,8 -|9: +Search 2/3 Save Undock Del -== snap n grid=180x31 cursor=10,8 -|7: 9 l9 -|8: 10 MARK c -== snap N grid=180x31 cursor=10,7 -|7: 6 MARK b -|8: 7 l7 -== snap cancel grid=180x31 cursor=10,7 -== snap nomatch grid=180x31 cursor=7,10 -|9: ^ +Search 0 Del -|10: /zzqq -|11: -|12: -|13: +|6: /MARK/ 1/3 › /tmp/pardes-snap/fsearch/cwd/g.txt Save Term Collapse Follow Del +|7: 1 l1 +|8: 2 MARK a +|9: 3 l3 +|10: 4 l4 +|11: 5 l5 +|12: 6 MARK b +|13: 7 l7 +|14: 8 l8 +|15: 9 l9 +|16: 10 MARK c +== snap n grid=180x31 cursor=10,12 +|6: /MARK/ 2/3 › /tmp/pardes-snap/fsearch/cwd/g.txt Save Term Collapse Follow Del +|7: 1 l1 /MARK/ 2/3 · line 6 +== snap N grid=180x31 cursor=10,8 +|6: /MARK/ 1/3 › /tmp/pardes-snap/fsearch/cwd/g.txt Save Term Collapse Follow Del +|7: 1 l1 /MARK/ 1/3 · line 2 +== snap cancel grid=180x31 cursor=10,8 +|7: 1 l1 +== snap nomatch grid=180x31 cursor=56,6 +|6: ^ /MARK/ 1/3 › /tmp/pardes-snap/fsearch/cwd/g.txt / zzqq = Look /zzqq/ 0 ⏎ land · M-⏎ +|7: as a list · C-r history · esc +|8: 1 l1 +|9: 2 MARK a +|10: 3 l3 +|11: 4 l4 +|12: 5 l5 +|13: 6 MARK b +|14: 7 l7 +|15: 8 l8 +|16: 9 l9 diff --git a/test/snapshots/lookrange.golden b/test/snapshots/lookrange.golden index c677fb8f..6c414ea9 100644 --- a/test/snapshots/lookrange.golden +++ b/test/snapshots/lookrange.golden @@ -69,32 +69,29 @@ |15: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-11 #e5eee5,#0d1410, 12-149 #f5f7ee,#3c614a, |16: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-10 #f5f7ee,#3c614a, 11-149 #f5f7ee,#0d1410, == snap hit1 grid=150x31 cursor=17,15 -|21: +Search 2/2 Save Undock Del -|22: /NEEDLE -|23: r.txt:7:4 gg NEEDLE gg -|24: r.txt:8:6 hhhh NEEDLE +|7: /NEEDLE/ 2/2 › /tmp/pardes-snap/lookrange/cwd/r.txt Save Term Collapse Follow Del == style hit1 grid=150x31 +|7: 0-1 d,#add38a, 2-47 #becfc4,#314738, 48-53 #dfbce5,#314738, 54-149 #becfc4,#314738, |14: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-9 #e5eee5,#0d1410, 10-15 #e5eee5,#352e19, 16-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-11 #e5eee5,#0d1410, 12-17 #f5f7ee,#3c614a, 18-149 #f5f7ee,#0d1410, -|16: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, -|18: 0-0 d,#e5eee5, 1-149 d,#0d1410, -|21: 0-1 d,#496656, 2-149 #b2c3b8,#1d2824, -|22: 0-0 d,#608269, 1-1 d,#0d1410, 2-149 #e5eee5,#0d1410, -|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#352e19, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#5e4923, 26-149 #f7e4a5,#243a2d, +|16: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, +|23: 0-0 d,#5e4923, 1-149 d,#0d1410, +|25: 0-0 d,#e5eee5, 1-149 d,#0d1410, == snap hit2 grid=150x31 cursor=15,14 +|7: /NEEDLE/ 1/2 ↻ › /tmp/pardes-snap/lookrange/cwd/r.txt Save Term Collapse Follow Del +|8: 1 aaaa /NEEDLE/ 1/2 ↻ · line 7 == style hit2 grid=150x31 +|7: 0-1 d,#add38a, 2-49 #becfc4,#314738, 50-55 #dfbce5,#314738, 56-149 #becfc4,#314738, +|8: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-124 #e5eee5,#0d1410, 125-125 #e5eee5,#1d2824, 126-149 #b2c3b8,#1d2824, |14: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-9 #e5eee5,#0d1410, 10-15 #f5f7ee,#3c614a, 16-16 #f5f7ee,#0d1410, 17-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-11 #e5eee5,#0d1410, 12-17 #e5eee5,#352e19, 18-149 #e5eee5,#0d1410, -|16: 0-0 d,#e5eee5, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, -|18: 0-0 d,#5e4923, 1-149 d,#0d1410, -|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#5e4923, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#352e19, 26-149 #f7e4a5,#243a2d, +|23: 0-0 d,#e5eee5, 1-149 d,#0d1410, +|25: 0-0 d,#5e4923, 1-149 d,#0d1410, == snap back grid=150x31 cursor=17,15 +|7: /NEEDLE/ 2/2 ↻ › /tmp/pardes-snap/lookrange/cwd/r.txt Save Term Collapse Follow Del +|8: 1 aaaa /NEEDLE/ 2/2 ↻ · line 8 == style back grid=150x31 |14: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-9 #e5eee5,#0d1410, 10-15 #e5eee5,#352e19, 16-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-11 #e5eee5,#0d1410, 12-17 #f5f7ee,#3c614a, 18-149 #f5f7ee,#0d1410, -|16: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, -|18: 0-0 d,#e5eee5, 1-149 d,#0d1410, -|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#352e19, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#5e4923, 26-149 #f7e4a5,#243a2d, +|23: 0-0 d,#5e4923, 1-149 d,#0d1410, +|25: 0-0 d,#e5eee5, 1-149 d,#0d1410, -- cgit v1.3 From 89b3e7365bac28c4cd7d52d40c1786c01db5af52 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 22:24:29 -0300 Subject: body_layer: a motion root's level paints its targets in the gutter while it shows: `g` its first and last lines, the window's top and bottom and the last edit (`g e t b .`); `]` the next and `[` the previous function, paragraph, test, comment, change, diagnostic, type and argument (`f p T c g d t a`); browsing `m` unrolled on `a` or `i` tints the brackets' region around the caret (pick-ui SPEC §4.4, walk UI line F item 2): the keys are the level's own (bindings.level, each leaf's role), each target looked for in the rows shown alone and a target off them not painted (the tag's arrows of §15 not built): the tree objects through syntax.objectNext given the shown bytes (QueryCursor.setByteRange; the motion passes null), `]g` through jj.nextChange bounded to the shown lines (`most`; the motion unbounded), `]d` a binary search of the fed diagnostics, `]p` the shown lines, `g t`/`g b` normal.gotoWindow's rows; drawn into the body as the `gw` labels are (R3), never written; at rest one test of the view's tag state (TagState.hostOf); the tree is the one the motion reads (normal.fileTree, made once a revision), not painted while the Changes layer's lines are in the buffer; TEST: body_layer.zig "a motion root's level paints its targets in the gutter ..." (`]` p, `g` g e, `m` on i tinted, rest as before); core-test 1025/1192 (167 skipped), 0 failed, load 9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/body_layer.zig | 263 ++++++++++++++++++++++++++++++++++++++++++++++++++++ core/jj.zig | 8 +- core/normal.zig | 4 +- core/syntax.zig | 5 +- 4 files changed, 274 insertions(+), 6 deletions(-) (limited to 'core/normal.zig') diff --git a/core/body_layer.zig b/core/body_layer.zig index 9c4a4c73..13fc7a85 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -25,6 +25,9 @@ const tagline = @import("tagline.zig"); const Text = panes.Text; const Layer = @import("Layer.zig"); +const TagState = @import("TagState.zig"); +const bindings = @import("bindings.zig"); +const File = panes.File; // ---- drawing a pane body into the surface ---- @@ -468,6 +471,8 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } + if (pane.file != null) paintTargets(p, s, arena, pane, r, tx, tw, body_y, body_h); + // `gw`'s labels, two letters over the start of each word if (pane.jump.items.len > 0) labels: { const flat = edit.flatSurface(p.editor(), &pane.body) catch break :labels; @@ -548,6 +553,207 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane }; } +/// Painted targets (SPEC §4.4), while a motion root's level shows in this +/// body's tag: each of its keys at the line it would go to, in the gutter +/// (`g e t b .` for `g`; for `]` the next and for `[` the previous +/// function, paragraph, test, comment, change, diagnostic, type, argument: +/// `f p T c g d t a`); browsing `m a` or `m i` unrolled, the brackets' +/// region around the caret tinted. Only the rows shown are looked at: a +/// target off them is not painted (the tag's arrows, SPEC §15, are not +/// built). At rest it costs one test of the view's tag state. +fn paintTargets(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, tx: u16, tw: u16, body_y: u16, body_h: u16) void { + const host = TagState.hostOf(p) orelse return; + if (p.panes[host] != pane) return; + const v = p.view(); + const cur = TagState.current(p, v) orelse return; + if (!TagState.shown(p, v)) return; + const f = &pane.file.?; + if (f.line_starts.len == 0) return; + const tab = p.settings.tab; + const held = pane.context_rows; + if (body_h <= held) return; + // The lines the rows show, past the context rows held at the top. + const lo: usize = @intCast(@max(0, pane.wrapAt(tab, held).line)); + var hi = lo; + for (held..body_h) |row| { + const line = pane.wrapAt(tab, @intCast(row)).line; + if (line >= 0 and line < f.line_starts.len) hi = @max(hi, @as(usize, @intCast(line))); + } + var children: [bindings.max_children]bindings.Child = undefined; + const level = bindings.level(p, cur.root, cur.path, &children); + if (cur.root == .match) { + const st = &v.tag_state; + if (!st.unrolled) return; + const on = for (level) |c| { + if (c.key == st.hi) break c; + } else return; + const leaf = (on.node orelse return).leaf orelse return; + if (leaf != .role or (leaf.role != .match_inside and leaf.role != .match_around)) return; + const role = leaf.role; + const caret = lineStart(f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col))); + const span = enclosing(f.content, @min(caret, f.content.len), lineStart(f, lo), lineStart(f, hi + 1)) orelse return; + const from = if (role == .match_inside) span[0] + 1 else span[0]; + const to = if (role == .match_inside) span[1] else span[1] + 1; + const first = lineOf(f, from); + const last = lineOf(f, to -| 1); + const rows = arena.alloc(Pane.PointerRow, last - first + 1) catch return; + for (rows, first..) |*row, line| row.* = .{ + .row = @intCast(line), + .lo = if (line == first) from - lineStart(f, line) else 0, + .hi = if (line == last) to - lineStart(f, line) else File.sourceLine(pane, @intCast(line)).len, + .to_edge = line != last, + }; + const th = p.theme(); + const page = th.bg orelse th.tag_bg; + paintSourceSelection(p, s, pane, r, rows, colors.mix(page, colors.mix(page, th.sel_bg)), null); + return; + } + if (cur.root != .goto and cur.root != .next and cur.root != .prev) return; + const th = p.theme(); + const style: CellStyle = .{ .fg = .{ .rgb = p.chromeTheme().box }, .bg = if (th.bg) |c| .{ .rgb = c } else .default, .bold = true }; + // The rows painted so far: a second key on a row goes after the first. + var painted: [bindings.max_children]u16 = undefined; + var n: usize = 0; + for (level) |c| { + const leaf = (c.node orelse continue).leaf orelse continue; + if (leaf != .role) continue; + const line = targetLine(p, pane, f, leaf.role, cur.root != .prev, lo, hi) orelse continue; + if (line < lo or line > hi) continue; + var row: u16 = held; + while (row < body_h) : (row += 1) { + const w = pane.wrapAt(tab, row); + if (w.line == line and w.at == 0) break; + } else continue; + const x: u16 = @intCast(std.mem.count(u16, painted[0..n], &.{row})); + if (x >= tw) continue; + s.set(tx + x, body_y + row, bindings.keyName(c.key), style); + painted[n] = row; + n += 1; + } +} + +/// The line a motion role would go to from the caret of `pane`, looked for +/// in lines `lo` to `hi` (the rows shown) alone; null for none there, or a +/// role that is no line's. +fn targetLine(p: *Pardes, pane: *Pane, f: *panes.File.State, role: modal.Normal.Role, forward: bool, lo: usize, hi: usize) ?usize { + const tab = p.settings.tab; + const caret: usize = @intCast(@max(0, pane.body.cur_row)); + const nlines = f.line_starts.len; + switch (role) { + .goto_file_start => return 0, + .goto_last_line => return if (nlines > 1 and f.line_starts[nlines - 1] == f.content.len) nlines - 2 else nlines - 1, + .goto_last_modification => return if (pane.body.last_edit) |at| @intCast(@max(0, at.row)) else null, + // helix's goto_window, as normal.gotoWindow counts its rows. + .goto_view_top, .goto_view_bottom => { + const shown: i32 = File.shownRows(tab, pane); + const held: i32 = pane.context_rows; + var last: i32 = shown - 1; + while (last > 0 and pane.wrapAt(tab, held + last).line >= @as(i32, @intCast(nlines))) last -= 1; + const margin: i32 = @min(config.scroll_off, @divTrunc(shown - 1, 2)); + const vr = std.math.clamp(@min(@max(if (role == .goto_view_top) margin else last - margin, margin), last - margin), 0, last); + const line = pane.wrapAt(tab, held + vr).line; + return if (line < 0) null else @intCast(line); + }, + // helix's paragraph: past the lines with text, then past the blank + // ones (modal.moveParagraph). + .goto_paragraph => { + var l = caret; + if (forward) { + while (l <= hi and !blank(pane, l)) l += 1; + while (l <= hi and blank(pane, l)) l += 1; + return if (l <= hi and l != caret) l else null; + } + while (l > lo and blank(pane, l - 1)) l -= 1; + while (l > lo and !blank(pane, l - 1)) l -= 1; + return if (l == caret or (l == lo and lo > 0)) null else l; + }, + .goto_change => return @import("jj.zig").nextChange(p, pane, caret, forward, hi - lo + 1), + // Once a Diagnostics list fed them, the pane's places (normal.zig's + // `]d`): the first after the caret's line, or the last before it. + .goto_diagnostic => { + const set = &pane.places; + const fed = set.started and set.provenance == .list and if (p.paneBySerial(set.from)) |qid| + (if (p.panes[qid].?.query) |q| q.source == .diagnostics else false) + else + false; + if (!fed) return null; + const hits = set.hits.items; + const edge = if (forward) lineStart(f, caret + 1) else lineStart(f, caret); + var a: usize = 0; + var b: usize = hits.len; + while (a < b) { + const m = (a + b) / 2; + if (hits[m].start < edge) a = m + 1 else b = m; + } + if (forward) return if (a < hits.len) lineOf(f, hits[a].start) else null; + return if (a > 0) lineOf(f, hits[a - 1].start) else null; + }, + inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |r| { + // The tree as the motion reads it, made once a revision; with + // the Changes layer's lines in the buffer its offsets are not the + // buffer's: not painted then. + if (f.layer.lines.len > 0) return null; + const tree = normal.fileTree(p, f) orelse return null; + const object = @field(modal.Normal.Object, @tagName(r)["goto_".len..]); + const at = lineStart(f, caret) + @as(usize, @intCast(@max(0, pane.body.cur_col))); + const span = @import("syntax.zig").objectNext(f.path, tree, f.content, object, forward, @min(at, f.content.len), .{ lineStart(f, lo), lineStart(f, hi + 1) }) orelse return null; + return lineOf(f, span.from); + }, + else => return null, + } +} + +fn blank(pane: *const Pane, line: usize) bool { + return std.mem.trim(u8, File.sourceLine(pane, @intCast(line)), " \t\r").len == 0; +} + +/// Where line `line` starts in a file's buffer; past its last, its end. +fn lineStart(f: *const panes.File.State, line: usize) usize { + return if (line < f.line_starts.len) f.line_starts[line] else f.content.len; +} + +/// The line holding byte `off` of a file's buffer. +fn lineOf(f: *const panes.File.State, off: usize) usize { + var a: usize = 0; + var b = f.line_starts.len; + while (a + 1 < b) { + const m = (a + b) / 2; + if (f.line_starts[m] <= off) a = m else b = m; + } + return a; +} + +/// The innermost bracket pair around byte `at` of `text`, both in bytes +/// `lo` to `hi` (the rows shown): its opening and its closing byte. +fn enclosing(text: []const u8, at: usize, lo: usize, hi: usize) ?[2]usize { + var depth: usize = 0; + var i = at; + const open = while (i > lo) { + i -= 1; + switch (text[i]) { + ')', ']', '}' => depth += 1, + '(', '[', '{' => if (depth == 0) break i else { + depth -= 1; + }, + else => {}, + } + } else return null; + const close: u8 = switch (text[open]) { + '(' => ')', + '[' => ']', + else => '}', + }; + depth = 0; + i = at; + while (i < @min(hi, text.len)) : (i += 1) { + if (text[i] == text[open]) depth += 1 else if (text[i] == close) { + if (depth == 0) return .{ open, i }; + depth -= 1; + } + } + return null; +} + pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { if (pane.addon != null) { const buf = try arena.alloc(u8, pane.rows -| 1); @@ -656,3 +862,60 @@ fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize { } return written; } + +test "a motion root's level paints its targets in the gutter: `]` the next paragraph's p, `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 }); + defer p.deinit(); + const pane = try p.setTestFile("fn a() {\n one;\n}\n\nfn b() {\n two;\n}\n"); + const id = p.view().active; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + p.clock.now_ns = 5 * std.time.ns_per_s; + p.clock.stepped_ns = p.clock.now_ns; + pane.focus = .body; + pane.body.cur_row = 1; + pane.body.cur_col = 4; + p.sync(); + var s = try p.render(a); + const body = s.region(.body, id).?.rect; + const gutter = body.x; + const at_rest = [_][]const u8{ s.at(gutter, body.y).grapheme(), s.at(gutter, body.y + 4).grapheme(), s.at(gutter, body.y + 6).grapheme() }; + for (at_rest) |g| try std.testing.expect(!std.mem.eql(u8, g, "p") and !std.mem.eql(u8, g, "g") and !std.mem.eql(u8, g, "e")); + const plain_bg = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg; + // `]`: nothing until its level shows; then `p` on the next paragraph. + p.update(.{ .key = .{ .cp = ']' } }); + p.advance(p.clock.now_ns); + _ = frame.reset(.retain_capacity); + s = try p.render(a); + try std.testing.expect(!std.mem.eql(u8, s.at(gutter, body.y + 4).grapheme(), "p")); + p.clock.now_ns = p.view().tag_state.due_ns; + _ = frame.reset(.retain_capacity); + s = try p.render(a); + try std.testing.expectEqualStrings("p", s.at(gutter, body.y + 4).grapheme()); + p.update(.{ .key = .{ .cp = Key.escape } }); + // `g`: its first and last lines. + p.update(.{ .key = .{ .cp = 'g' } }); + p.advance(p.clock.now_ns); + p.clock.now_ns = p.view().tag_state.due_ns; + _ = frame.reset(.retain_capacity); + s = try p.render(a); + try std.testing.expectEqualStrings("g", s.at(gutter, body.y).grapheme()); + try std.testing.expectEqualStrings("e", s.at(gutter, body.y + 6).grapheme()); + p.update(.{ .key = .{ .cp = Key.escape } }); + // `m` unrolled, its cursor on `i`: the inside of the braces around the + // caret tinted. + p.update(.{ .key = .{ .cp = 'm' } }); + p.update(.{ .key = .{ .cp = Key.down } }); + p.view().tag_state.hi = bindings.nodeAt(.match, "i").?.key; + _ = frame.reset(.retain_capacity); + s = try p.render(a); + const lit = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg; + try std.testing.expect(!std.meta.eql(plain_bg, lit)); + // At rest again: the gutter as it was. + p.update(.{ .key = .{ .cp = Key.escape } }); + _ = frame.reset(.retain_capacity); + s = try p.render(a); + try std.testing.expectEqualStrings(at_rest[1], s.at(gutter, body.y + 4).grapheme()); +} diff --git a/core/jj.zig b/core/jj.zig index c1d07662..7f1e5623 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -2030,14 +2030,15 @@ fn ownerOf(ch: *const Changes, f: *const File.State, row: usize) PinIndex { /// The line `]g` (`forward`) or `[g` goes to from buffer line `from` of /// the pane: the next or the previous change's first line (the first of a /// run of lines one pin wrote or took away: a ghost line is a line); null -/// with none shown, or none that way. -pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?usize { +/// with none shown, or none that way, or none in the `most` lines looked +/// at (a body's painted targets look at the rows shown alone). +pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool, most: usize) ?usize { if (pane.layers.lanes == 0) return null; const f = if (pane.file) |*file| file else return null; const ch = answered(p, f.layer.owner) orelse return null; const n = File.lineCount(f.content); var row = from; - while (true) { + for (0..most) |_| { if (forward) { row += 1; if (row >= n) return null; @@ -2048,6 +2049,7 @@ pub fn nextChange(p: *Pardes, pane: *const Pane, from: usize, forward: bool) ?us const owner = ownerOf(ch, f, row); if (owner != .none and (row == 0 or ownerOf(ch, f, row - 1) != owner)) return row; } + return null; } /// A Look (B3) at column `col` of body row `vr` of pane `id` on the id of diff --git a/core/normal.zig b/core/normal.zig index 2ec8a4da..ea2bbc31 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -249,7 +249,7 @@ fn objectJump(e: Editor, t: *Text, text: []const u8, range: modal.Selection, obj const tree = nodeTree(e, t, text) orelse return; var last = range; for (0..count) |_| { - const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last))) orelse break; + const s = syntax.objectNext(tree.path, tree.root, tree.text, object, forward, tree.in(modal.selectionCursor(text, last)), null) orelse break; const next: modal.Selection = .{ .anchor = tree.out(s.from, false), .head = tree.out(s.to, s.to > s.from) }; if (std.meta.eql(next, last)) break; last = next; @@ -1317,7 +1317,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error! .view_scroll_down => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, 1), .view_scroll_up => if (t.what == .body) return t.scrollViewMove(p.settings.tab, text, range, -1), // The next and the previous of the changes this view shows (jj.zig). - .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward)) |line| + .goto_change => if (@import("jj.zig").nextChange(p, pane, @intCast(@max(0, t.cur_row)), a.forward, std.math.maxInt(usize))) |line| return pointMove(p.settings.tab, t, text, range, modal.lineStartOffset(text, line)), .goto_diagnostic, .goto_diagnostic_end => { // Once a Diagnostics query fed them, the diagnostics are this diff --git a/core/syntax.zig b/core/syntax.zig index 08ea1fb4..a388573b 100644 --- a/core/syntax.zig +++ b/core/syntax.zig @@ -566,7 +566,7 @@ pub fn objectAt(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object /// `]f`/`[f` and kin: the next object starting after byte `at`, or the /// previous one ending before it (helix goto_treesitter_object), from the /// first of `.movement`, `.around`, `.inside` the query names. -pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize) ?Span { +pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, object: Object, forward: bool, at: usize, within: ?[2]usize) ?Span { const q = objectQuery(path) orelse return null; var buf: [32]u8 = undefined; const capture = for ([_][]const u8{ "movement", "around", "inside" }) |kind| { @@ -576,6 +576,9 @@ pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, obje const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr)); const cursor = ts.QueryCursor.create(); defer cursor.destroy(); + // Only the objects these bytes hold (a body's rows shown): its painted + // targets (body_layer.paintTargets). + if (within) |w| cursor.setByteRange(@intCast(w[0]), @intCast(w[1])) catch return null; cursor.exec(q, tree.rootNode()); var best: ?Span = null; while (cursor.nextMatch()) |match| { -- cgit v1.3