diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-24 20:29:22 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-24 20:29:22 -0300 |
| commit | 93160a4d7c6151fba394fd5ab51dea3b7468b937 (patch) | |
| tree | 9044306c2fff2ac1f3f5db6507e159abfde396c1 | |
| parent | b77eac2511cb247ebae07494bbb13b2e6a1139ab (diff) | |
| download | pardes-93160a4d7c6151fba394fd5ab51dea3b7468b937.tar.gz pardes-93160a4d7c6151fba394fd5ab51dea3b7468b937.zip | |
Pick: a picker's keys (walk UI, pick-ui SPEC.md §5.4, item 2 of line D): Enter lands through the walk's open from the pane it lands in (a reading pane steps in place, a held one's reader does; the reader made for the picker is taken away first and the place opens as the held pane's reader, look.open's own), its Spot a Find row's first line, the jump list forced so Ctrl-o goes back to where it was asked; S-Enter and C-s open each picked row in a pane below; C-v in a new column (layout.newColumn, the place put there by exec_column as a column tag's New is); marked rows land the first as asked and the others below; Tab and S-Tab mark and move (Pick.marks, what each row is: a name's candidate, a Grep's line, sorted, so a frame finds a row's mark by a search over them, never a walk); M-a marks all or none; M-h the source spine as a step in the band (f g r b s / ?: Find Grep Recent Panes Symbols All, Help over the binding tree with the query, which A's TagState.openHelp takes), the query kept; C-SPC refines: the rows the query keeps become the list (a new Names of them, the old one stacked with its query, max_refine 3; a Grep's lines and All's become names, `path:line:col text`), the head `Refs alpha | w`; Backspace on an empty field undoes a refinement, else goes back to the leader level the picker's builtin is under, unrolled (as Help's Backspace); C-q keeps the rows (the marked, else all, Query.max_rows) as a +Search of text under the asking pane, its first line the word and query, each row an address a Look opens; M-p turns the preview off (the landing pane's own text back) and on; Esc rolls everything up (the tag's: TagState.toRest); a landing pane that previewed its own file has its caret and top put back; TEST FIRST: Pick.zig "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and Backspace undoes it, M-h switches source keeping the query" and "Enter lands as a walk step in the reading pane; S-Enter opens below; from a held pane the reader made for the picker becomes its reader"; core-test 1016/1182 (166 skipped), 0 failed
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| -rw-r--r-- | core/Pick.zig | 437 |
1 files changed, 420 insertions, 17 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 30cf74ab..e3aec3c5 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -65,6 +65,10 @@ const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 /// The pickers kept after they roll up, for their list words and Resume. pub const max_lists = 8; +/// Refinements a picker stacks (C-SPC). +pub const max_refine = 3; + +pub const replica = .{ .len = .{.{ "refined", "nrefined" }} }; /// Its serial: the disk engine's target, a list word's index. id: u32, @@ -100,11 +104,24 @@ aside_col: i32 = 0, /// The row the preview shows (`no_row`: none yet); asked again on the /// frame after the cursor moves, never on the key that moved it. shown: u32 = no_row, -fed: u64 = 0, +/// The preview on (M-p). +previews: bool = true, +/// Marked rows (Tab), by what each is (`idOf`), in order. +marks: std.ArrayList(u32) = .empty, +/// The queries refined from (C-SPC), each with the names it listed then: +/// Backspace on an empty field goes back to them. +refined: [max_refine]Refined = undefined, +nrefined: u8 = 0, +/// The band's second step: the sources to switch to (M-h). +spine: enum(u8) { rows, sources } = .rows, + +const Refined = struct { query: Kind.Buf(64) = .{}, names: Names = .{} }; const no_row = std.math.maxInt(u32); pub fn deinit(pk: *Pick, gpa: std.mem.Allocator) void { + for (pk.refined[0..pk.nrefined]) |*r| r.names.deinit(gpa); + pk.marks.deinit(gpa); pk.names.deinit(gpa); pk.found.deinit(gpa); pk.found_bytes.deinit(gpa); @@ -240,6 +257,12 @@ fn addRows(p: *Pardes, pk: *Pick, text: []const u8) void { } } +/// Its rows are names scored by the query: any source's but a live Grep's +/// or All's, and theirs once refined. +fn fuzzy(pk: *const Pick) bool { + return pk.nrefined > 0 or (pk.source != .grep and pk.source != .all); +} + /// The field typed: the rows again for it. A Grep's lines are the disk /// engine's, once the typing pauses; the names are scored a frame's count /// at a time from here. @@ -247,6 +270,7 @@ pub fn filter(p: *Pardes, pk: *Pick) void { const q = std.mem.trim(u8, p.view().tag_state.field.text(), " "); p.view().tag_state.rows.at = 0; pk.shown = no_row; + if (fuzzy(pk)) return pk.names.typed(p.gpa, q); if (pk.source == .grep) { pk.found.clearRetainingCapacity(); pk.found_bytes.clearRetainingCapacity(); @@ -256,8 +280,8 @@ pub fn filter(p: *Pardes, pk: *Pick) void { } else if (p.stream.target == pk.id) _ = p.stream.stop(); return; } - if (pk.source == .all) { - // Each open text's places, found a frame's more at a time. + { + // All: each open text's places, found a frame's more at a time. for (p.panes) |slot| { const tp = slot orelse continue; if (!searchable(tp)) continue; @@ -265,9 +289,7 @@ pub fn filter(p: *Pardes, pk: *Pick) void { if (tp.places.from == pk.id) tp.places.clear(p.gpa); } else tp.places.search(p, tp, .list, pk.id, q, false, null, false); } - return; } - pk.names.typed(p.gpa, q); } /// A pane whose text All searches: a file's or a list's, not a query @@ -335,6 +357,7 @@ pub fn busy(p: *const Pardes) bool { /// The rows the list has now. pub fn count(p: *const Pardes, pk: *const Pick) usize { + if (fuzzy(pk)) return pk.names.hits.items.len; switch (pk.source) { .grep => return pk.found.items.len, .all => { @@ -347,6 +370,21 @@ pub fn count(p: *const Pardes, pk: *const Pick) usize { } } +/// What row `i` is, kept by a mark across a new query: a name's candidate, +/// a line's place in its source. +fn idOf(pk: *const Pick, i: usize) u32 { + return if (fuzzy(pk)) pk.names.hits.items[i].cand else @intCast(i); +} + +fn marked(pk: *const Pick, id: u32) ?usize { + const at = std.sort.lowerBound(u32, pk.marks.items, id, struct { + fn order(a: u32, b: u32) std.math.Order { + return std.math.order(a, b); + } + }.order); + return if (at < pk.marks.items.len and pk.marks.items[at] == id) at else null; +} + /// Where a row points: a place in a file, or a pane by its serial. const Target = union(enum) { none, place: struct { path: []const u8, at: look.Spot }, pane: u32 }; @@ -356,7 +394,7 @@ const Row = struct { text: []const u8, pre: u16 = 0, faint: u16 = 0, dim: bool = fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { const dir = pk.dir.slice(); - if (pk.source == .all) { + if (!fuzzy(pk) and pk.source == .all) { var order: [pardes.MAX_PANES]usize = undefined; var k = i; for (allPanes(p, pk, &order)) |id| { @@ -384,7 +422,7 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { } return .{ .text = "" }; } - if (pk.source == .grep) { + if (!fuzzy(pk)) { const fd = pk.found.items[i]; const name = pk.found_bytes.items[fd.path..][0..fd.path_len]; const code = pk.found_bytes.items[fd.code..][0..fd.code_len]; @@ -407,7 +445,7 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { var namebuf: [4096]u8 = undefined; const name = if (text.len > 1 and text[0] == '"' and text[text.len - 1] == '"') look.unquoted(&namebuf, text[1 .. text.len - 1]) else text; row.faint = @intCast(if (std.mem.lastIndexOfScalar(u8, text, '/')) |k| k + 1 else 0); - row.target = .{ .place = .{ .path = try std.Io.Dir.path.join(arena, &.{ dir, name }), .at = .{} } }; + row.target = .{ .place = .{ .path = try std.Io.Dir.path.join(arena, &.{ dir, name }), .at = .{ .line = 1, .col = 1 } } }; }, .panes => { row.pre = cand.key -| 1; @@ -454,10 +492,12 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { }; try text.appendSlice(arena, pk.word.slice()); try text.append(arena, ' '); + for (pk.refined[0..pk.nrefined]) |r| try text.print(arena, "{s} | ", .{r.query.slice()}); const caret: u32 = @intCast(text.items.len + s.field.caret); try text.appendSlice(arena, s.field.text()); const n = count(p, pk); try text.print(arena, " {d}/{d}", .{ if (n == 0) 0 else s.rows.at + 1, n }); + if (pk.marks.items.len > 0) try text.print(arena, " •{d}", .{pk.marks.items.len}); if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); // The second row: the spines go on through it from the key cells, // then the other sources' words, each switching to it. @@ -492,7 +532,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { /// body). pub fn bandRows(p: *const Pardes) u16 { const pk = live(p) orelse return 0; - const have = if (pk.source == .grep or pk.source == .all) Band.Cursor.shown else pk.names.cands.items.len; + const have = if (!fuzzy(pk)) Band.Cursor.shown else pk.names.cands.items.len; return @intCast(@max(1, @min(Band.Cursor.shown, have))); } @@ -501,7 +541,12 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const pk = live(p) orelse return &.{}; const s = &p.view().tag_state; const n = count(p, pk); - const out = try arena.alloc(Band.Step, 1); + const out = try arena.alloc(Band.Step, if (pk.spine == .sources) 2 else 1); + if (pk.spine == .sources) { + const rows = try arena.alloc(Band.Row, spine_rows.len); + for (spine_rows, rows, 0..) |src, *row, i| row.* = .{ .text = try std.fmt.allocPrint(arena, "{c} {s}", .{ src.key, src.word }), .key = 1, .index = @intCast(i), .dim = src.source == pk.source }; + out[1] = .{ .rows = rows }; + } if (n == 0) { const none = try arena.alloc(Band.Row, 1); none[0] = .{ .text = if (pk.names.busy() or p.stream.busyFor(pk.id)) "⋯" else if (s.field.len == 0 and pk.source == .grep) "type to search" else "nothing matches", .dim = true }; @@ -512,7 +557,7 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const rows = try arena.alloc(Band.Row, w.hi - w.lo); for (rows, w.lo..) |*row, i| { const r = try rowAt(p, pk, arena, i); - row.* = .{ .text = r.text, .on = i == s.rows.at, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i) }; + row.* = .{ .text = r.text, .on = i == s.rows.at, .mark = marked(pk, idOf(pk, i)) != null, .dim = r.dim, .pre = r.pre, .faint = r.faint, .marks = r.marks, .index = @intCast(i) }; } out[0] = .{ .rows = rows }; return out; @@ -526,7 +571,7 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { /// open pane shows is taken from it; any other is read. fn preview(p: *Pardes, pk: *Pick) void { const s = &p.view().tag_state; - if (pk.shown == s.rows.at) return; + if (pk.shown == s.rows.at or !pk.previews) return; pk.shown = s.rows.at; if (count(p, pk) == 0) return; var frame = std.heap.ArenaAllocator.init(p.gpa); @@ -604,17 +649,22 @@ fn openText(p: *Pardes, path: []const u8) ?[]u8 { } /// The landing pane as the picker found it: its own text back, or the -/// pane made for the picker taken away (`keep`: it stays, the place -/// landed on is its). +/// pane made for the picker taken away; the places it lit put out. fn restore(p: *Pardes, pk: *Pick) void { const id = p.paneBySerial(pk.landing) orelse return; - const pane = p.panes[id].?; for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pk.id) tp.places.clear(p.gpa); if (pk.made) { pk.made = false; p.removePane(id, p.paneBySerial(pk.owner)) catch |err| p.reportError(.at(id), "pick", err); return; } + putBack(p, pk); +} + +/// The landing pane's own text back in it, the preview's freed. +fn putBack(p: *Pardes, pk: *Pick) void { + const id = p.paneBySerial(pk.landing) orelse return; + const pane = p.panes[id].?; const own = pk.aside orelse return; pk.aside = null; File.deinit(p, pane, &pane.file.?); @@ -659,16 +709,264 @@ pub fn deinitAll(p: *Pardes) void { // ---- keys ---- -/// A key in the live picker (keys.zig tagStateKey): the rows' cursor, the -/// field. Esc is the tag's (TagState.toRest rolls it up). +/// A key in the live picker (keys.zig tagStateKey, SPEC §5.4). Esc is +/// the tag's: TagState.toRest rolls it up. pub fn key(p: *Pardes, k: Key) void { const pk = live(p) orelse return TagState.toRest(p); const s = &p.view().tag_state; const n: u16 = @intCast(@min(count(p, pk), std.math.maxInt(u16))); + const plain = !k.ctrl and !k.alt; + const ctrl = k.ctrl and !k.alt; + const alt = k.alt and !k.ctrl; + if (pk.spine == .sources) { + pk.spine = .rows; + for (spine_rows) |src| if (plain and k.cp == src.key) return switchTo(p, pk, src.source); + return; + } + if (plain and k.cp == Key.enter) return land(p, if (k.shift) .below else .walk); + if (ctrl and k.cp == 's') return land(p, .below); + if (ctrl and k.cp == 'v') return land(p, .column); + if (plain and k.cp == Key.tab) { + if (n == 0) return; + toggle(p, pk, idOf(pk, s.rows.at)); + _ = s.rows.key(.{ .cp = if (k.shift) Key.up else Key.down }, n); + return; + } + if (alt and k.cp == 'a') return markAll(p, pk); + if (alt and k.cp == 'h') { + pk.spine = .sources; + return; + } + if (ctrl and (k.cp == ' ' or k.cp == '@')) return refine(p, pk); + if (ctrl and k.cp == 'q') return keep(p, pk); + if (alt and k.cp == 'p') { + pk.previews = !pk.previews; + pk.shown = no_row; + if (!pk.previews) putBack(p, pk); + return; + } + if (plain and k.cp == Key.backspace and s.field.len == 0) return back(p, pk); if (s.rows.key(k, n)) return; if (s.field.key(k)) filter(p, pk); } +/// The source spine (M-h): a key a source, the query kept. +const spine_rows = [_]struct { key: u8, word: []const u8, source: ?Source }{ + .{ .key = 'f', .word = "Find", .source = .find }, + .{ .key = 'g', .word = "Grep", .source = .grep }, + .{ .key = 'r', .word = "Recent", .source = .recent }, + .{ .key = 'b', .word = "Panes", .source = .panes }, + .{ .key = 's', .word = "Symbols", .source = .symbols }, + .{ .key = '/', .word = "All", .source = .all }, + .{ .key = '?', .word = "Help", .source = null }, +}; + +/// The live picker's source another (`null`: Help over the binding tree), +/// its query kept, its landing pane the same. +fn switchTo(p: *Pardes, pk: *Pick, source: ?Source) void { + const s = &p.view().tag_state; + const to = source orelse { + var query: Kind.Buf(256) = .{}; + query.set(s.field.text()); + TagState.toRest(p); + TagState.openHelp(p, null, ""); + p.view().tag_state.field.set(query.slice()); + return TagState.filter(p); + }; + if (p.stream.target == pk.id) _ = p.stream.stop(); + for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pk.id) tp.places.clear(p.gpa); + for (pk.refined[0..pk.nrefined]) |*r| r.names.deinit(p.gpa); + pk.nrefined = 0; + pk.marks.clearRetainingCapacity(); + pk.source = to; + pk.word.set(words.get(to).word); + pk.keys.set(words.get(to).keys); + start(p, pk); + filter(p, pk); +} + +fn toggle(p: *Pardes, pk: *Pick, id: u32) void { + if (marked(pk, id)) |at| { + _ = pk.marks.orderedRemove(at); + return; + } + const at = std.sort.lowerBound(u32, pk.marks.items, id, struct { + fn order(a: u32, b: u32) std.math.Order { + return std.math.order(a, b); + } + }.order); + pk.marks.insert(p.gpa, at, id) catch |err| p.reportError(.at(p.view().active), "pick", err); +} + +/// Every row marked, or none when every one is. +fn markAll(p: *Pardes, pk: *Pick) void { + const n = count(p, pk); + const all = pk.marks.items.len == n; + pk.marks.clearRetainingCapacity(); + if (all) return; + pk.marks.ensureTotalCapacity(p.gpa, n) catch |err| return p.reportError(.at(p.view().active), "pick", err); + for (0..n) |i| pk.marks.appendAssumeCapacity(idOf(pk, i)); + std.mem.sort(u32, pk.marks.items, {}, std.sort.asc(u32)); +} + +/// C-SPC: the rows the query keeps become the list, the query starts +/// empty; a Grep's lines and All's become names to score from here. +fn refine(p: *Pardes, pk: *Pick) void { + const s = &p.view().tag_state; + const q = std.mem.trim(u8, s.field.text(), " "); + if (q.len == 0 or pk.nrefined == max_refine) return; + var next: Names = .{}; + if (fuzzy(pk)) { + for (pk.names.hits.items) |h| { + const c = pk.names.cands.items[h.cand]; + next.add(p.gpa, pk.names.bytes.items[c.start..][0..c.len], c.key, c.key_len, c.base, c.ref) catch break; + } + } else { + var arena = std.heap.ArenaAllocator.init(p.gpa); + defer arena.deinit(); + for (0..count(p, pk)) |i| { + _ = arena.reset(.retain_capacity); + const row = rowAt(p, pk, arena.allocator(), i) catch break; + const at = switch (row.target) { + .place => |t| t, + else => continue, + }; + const ln = std.fmt.allocPrint(arena.allocator(), "{s}:{d}:{d} {s}", .{ relative(at.path, pk.dir.slice()), at.at.line, at.at.col, row.text[@min(row.text.len, row.pre + 1)..] }) catch break; + next.add(p.gpa, ln, 0, ln.len, 0, @intCast(i)) catch break; + } + if (p.stream.target == pk.id) _ = p.stream.stop(); + for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pk.id) tp.places.clear(p.gpa); + } + pk.refined[pk.nrefined] = .{ .names = pk.names }; + pk.refined[pk.nrefined].query.set(q); + pk.nrefined += 1; + pk.names = next; + pk.marks.clearRetainingCapacity(); + s.field.clear(); + filter(p, pk); +} + +fn relative(path: []const u8, dir: []const u8) []const u8 { + const d = std.mem.trimEnd(u8, dir, "/"); + if (d.len > 0 and path.len > d.len + 1 and std.mem.startsWith(u8, path, d) and path[d.len] == '/') return path[d.len + 1 ..]; + return path; +} + +/// Backspace on an empty field: the last refinement undone, its query +/// back; else back a step, to the leader's level the picker was run from. +fn back(p: *Pardes, pk: *Pick) void { + const v = p.view(); + const s = &v.tag_state; + if (pk.nrefined > 0) { + pk.nrefined -= 1; + const r = &pk.refined[pk.nrefined]; + pk.names.deinit(p.gpa); + pk.names = r.names; + pk.marks.clearRetainingCapacity(); + s.field.set(r.query.slice()); + s.rows.at = 0; + pk.shown = no_row; + // A live source's own rows again: All's places are found anew. + if (!fuzzy(pk) and pk.source == .all) filter(p, pk); + return; + } + const keys = pk.keys; + TagState.toRest(p); + if (keys.len == 0) return; + v.prefix = .leader; + @memcpy(v.leader_keys[0..keys.len], keys.slice()); + v.leader_n = @intCast(keys.len); + s.root = .leader; + s.unrolled = true; +} + +/// C-q: the rows (the marked, else all, `Query.max_rows` at most) kept as +/// text in a +Search under the asking pane, the source's word and query +/// its first line, each row an address a Look opens. +fn keep(p: *Pardes, pk: *Pick) void { + const s = &p.view().tag_state; + const from = p.paneBySerial(pk.owner) orelse p.view().active; + var out: std.Io.Writer.Allocating = .init(p.gpa); + defer out.deinit(); + var arena = std.heap.ArenaAllocator.init(p.gpa); + defer arena.deinit(); + out.writer.print("{s} ", .{pk.word.slice()}) catch return; + for (pk.refined[0..pk.nrefined]) |r| out.writer.print("{s} | ", .{r.query.slice()}) catch return; + out.writer.print("{s}\n", .{s.field.text()}) catch return; + var kept: usize = 0; + for (0..count(p, pk)) |i| { + if (kept == Query.max_rows) break; + if (pk.marks.items.len > 0 and marked(pk, idOf(pk, i)) == null) continue; + _ = arena.reset(.retain_capacity); + const row = rowAt(p, pk, arena.allocator(), i) catch break; + switch (row.target) { + .place => |t| if (pk.source == .symbols) + out.writer.print("{s}:{d} {s}\n", .{ std.Io.Dir.path.basename(t.path), t.at.line, std.mem.trimStart(u8, row.text, " 0123456789") }) catch break + else + out.writer.print("{s}\n", .{row.text}) catch break, + else => out.writer.print("{s}\n", .{row.text}) catch break, + } + kept += 1; + } + TagState.toRest(p); + const free = p.freeSlot() orelse return p.reportError(.at(from), "pick", error.NoPaneSlots); + const content = out.toOwnedSlice() catch |err| return p.reportError(.at(from), "pick", err); + _ = Output.open(p, free, pk.dir.slice(), .search, "", content) catch |err| { + p.gpa.free(content); + return p.reportError(.at(from), "pick", err); + }; + layout.placeNew(p, from, from, free, .doc); +} + +/// How Enter lands: a walk step, a new pane below, a new column. +const How = enum { walk, below, column }; + +/// Enter: the current row, or the marked ones, looked at from the asking +/// pane (look.open, the walk's way: a reading pane steps, a held one's +/// reader does), the first as `how` says, the others each in a pane below. +/// The list rolls into the landing pane's trail as its word (item 4). +fn land(p: *Pardes, how: How) void { + const pk = live(p) orelse return; + const s = &p.view().tag_state; + const n = count(p, pk); + if (n == 0) return; + var arena = std.heap.ArenaAllocator.init(p.gpa); + defer arena.deinit(); + const a = arena.allocator(); + var picked: std.ArrayList(Target) = .empty; + if (pk.marks.items.len == 0) { + picked.append(a, (rowAt(p, pk, a, s.rows.at) catch return).target) catch return; + } else for (0..n) |i| if (marked(pk, idOf(pk, i)) != null) { + picked.append(a, (rowAt(p, pk, a, i) catch return).target) catch return; + }; + TagState.toRest(p); + const v = p.view(); + const from = p.paneBySerial(pk.owner) orelse v.active; + v.active = from; + v.jumps.force = true; + for (picked.items, 0..) |t, k| switch (t) { + .none => {}, + .pane => |serial| if (p.paneBySerial(serial)) |tid| { + v.active = tid; + if (layout.findPane(p, tid)) |at| p.setActiveColumn(at.col); + } else { + _ = p.store.reopen(p, serial, from); + }, + .place => |at| if (k == 0 and how == .walk) { + _ = look.open(p, from, at.path, at.at, .replace); + } else if (k == 0 and how == .column) { + layout.newColumn(p, from); + const c = layout.activeColumn(p) orelse continue; + p.exec_column = c; + defer p.exec_column = null; + pardes.tagline.exitHeader(p); + _ = look.open(p, from, at.path, at.at, .below); + } else { + _ = look.open(p, from, at.path, at.at, .below); + }, + }; +} + fn testFile(p: *Pardes, tmp: *std.testing.TmpDir, name: []const u8, text: []const u8) ![]u8 { try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = text }); var base_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; @@ -748,6 +1046,111 @@ test "a picker from a held pane unrolls in a reader made for it: its rows in the try std.testing.expectEqualStrings("pick", pk.query.slice()); } +test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and Backspace undoes it, M-h switches source keeping the query" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + look.lookAt(p, p.view().active, a_path); + const id = p.view().active; + open(p, id, .list, "Refs", "", "alpha.zig:1:1 one\nalpha.zig:1:2 two\nbeta.zig:1:1 tree\nalpha.zig:1:4 four\n"); + const pk = live(p).?; + settle(p); + const s = &p.view().tag_state; + try std.testing.expectEqual(@as(usize, 4), count(p, pk)); + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqual(@as(u16, 1), s.rows.at); + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqual(@as(usize, 2), pk.marks.items.len); + p.update(.{ .key = .{ .cp = Key.up } }); + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqual(@as(usize, 1), pk.marks.items.len); + try std.testing.expectEqual(@as(u16, 2), s.rows.at); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs 3/4 •1\n") != null); + p.update(.{ .key = .{ .cp = 'a', .alt = true } }); + try std.testing.expectEqual(@as(usize, 4), pk.marks.items.len); + p.update(.{ .key = .{ .cp = 'a', .alt = true } }); + try std.testing.expectEqual(@as(usize, 0), pk.marks.items.len); + // C-SPC: what `alpha` keeps becomes the list; `w` then narrows it. + for ("alpha") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + try std.testing.expectEqual(@as(usize, 3), count(p, pk)); + p.update(.{ .key = .{ .cp = ' ', .ctrl = true } }); + try std.testing.expectEqual(@as(u8, 1), pk.nrefined); + try std.testing.expectEqual(@as(u16, 0), s.field.len); + settle(p); + try std.testing.expectEqual(@as(usize, 3), count(p, pk)); + for ("w") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, pk)); + _ = frame.reset(.retain_capacity); + try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs alpha | w 1/1\n") != null); + // Backspace twice: the `w` goes, then the refinement, its query back. + p.update(.{ .key = .{ .cp = Key.backspace } }); + p.update(.{ .key = .{ .cp = Key.backspace } }); + try std.testing.expectEqual(@as(u8, 0), pk.nrefined); + try std.testing.expectEqualStrings("alpha", s.field.text()); + settle(p); + try std.testing.expectEqual(@as(usize, 3), count(p, pk)); + // M-h r: Recent, the query kept. + p.update(.{ .key = .{ .cp = 'h', .alt = true } }); + try std.testing.expect(pk.spine == .sources); + p.update(.{ .key = .{ .cp = 'r', .text = "r" } }); + try std.testing.expectEqual(Source.recent, pk.source); + try std.testing.expectEqualStrings("alpha", s.field.text()); + p.update(.{ .key = .{ .cp = Key.escape } }); +} + +test "Enter lands as a walk step in the reading pane; S-Enter opens below; from a held pane the reader made for the picker becomes its reader" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + const b_path = try testFile(p, &tmp, "beta.zig", "one\ntwo\nthree\n"); + defer p.gpa.free(b_path); + look.lookAt(p, p.view().active, a_path); + const id = p.view().active; + const npanes = paneCount(p); + open(p, id, .list, "Refs", "", "beta.zig:3:1 three\n"); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(live(p) == null); + const here = p.panes[p.view().active].?; + try std.testing.expectEqualStrings(b_path, here.file.?.path); + try std.testing.expectEqual(@as(i32, 2), here.body.cur_row); + try std.testing.expect(here.file.?.output == null); + try std.testing.expectEqual(npanes, paneCount(p)); + // Its trail: where it was, then the place. + try std.testing.expect(here.trail.n >= 2); + // S-Enter: a pane below, the reading pane as it was. + open(p, p.view().active, .list, "Refs", "", "alpha.zig:1:7 alpha\n"); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter, .shift = true } }); + try std.testing.expectEqual(npanes + 1, paneCount(p)); + try std.testing.expectEqualStrings(a_path, p.panes[p.view().active].?.file.?.path); + + // From a held pane: the reader made for the picker is taken away and + // the place opens as the held pane's reader, its trail its owner's. + const held = p.view().active; + Trail.keep(p.panes[held].?); + open(p, held, .list, "Refs", "", "beta.zig:2:1 two\n"); + const pk = live(p).?; + try std.testing.expect(pk.made); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter } }); + const reader = Trail.readerOf(p, p.panes[held].?).?; + try std.testing.expectEqualStrings(b_path, p.panes[reader].?.file.?.path); + try std.testing.expectEqual(npanes + 2, paneCount(p)); +} + test "a picker from a reading pane unrolls in that pane, its own text set aside while it previews and back on Esc" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); defer p.deinit(); |
