diff options
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 427 |
1 files changed, 418 insertions, 9 deletions
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); +} |
