From 7e7d9569a008124cb7ea01b6720a8d5963b0a42f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 18:54:18 -0300 Subject: walk (pick-ui SPEC §2, §6.1, §6.5, item 1 of the Walk UI line B): a pane is held or reading: Pane.trail (core/Trail.zig: a fixed walk, max_steps 16 of max_text 256 bytes, its steps a union of place, owner, list, search and recipe, `cur` held by walk.zig, in the digest and the dumps) and `held`, set by Keep (a new builtin, `SPC j k`) and by any edit of the file (File.edit, Trail.edited, twins too); a reading pane (a file not held: no output, listing, pager, query or kind) shows Keep after its path in its tag and its tag in the reading tint (a new theme role tag_reading_bg, derived from the page and the search ground, acme the spec's #f3f0cc), under the new setting `Jumps walk|new-pane`, whose default is new-pane until the walk's pickers are in, so every tag, frame and golden is as before (a walk default changes 74 of 102 process goldens and 21 behaviour tests by the Keep word alone); TEST FIRST: Trail.zig "a file's pane is reading ..." and "Keep holds a reading pane ..."; draw.zig's three-row tag test reads its band through tagGround; docs/typ/builtins-tui.json refreshed; core-test 993/1159 (166 skipped), pane-test 183/184, behaviour-test 277/277 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/draw.zig | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'core/draw.zig') diff --git a/core/draw.zig b/core/draw.zig index ff76aa75..f00a7583 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -454,7 +454,7 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // its ring. A pixel shell draws the ring (gui.zig gripDecor); a grid, // which cannot, marks dirty in the grip's second cell with a bold `*` // on the dirty colour, a cue that survives any palette. - const band = if (active) chrome.tag_active_bg else chrome.tag_bg; + const band = tagGround(p, pane, active); const dirty = tagline.dirty(pane); const box_bg = gripBg(p, active); const box_style: CellStyle = .{ @@ -776,7 +776,7 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r layer.cols = columns; layer.rows = rows; layer.context_rows = 0; - layer.bg = if (v.window_pick == id) p.theme().sel_bg else if (active) chrome.tag_active_bg else chrome.tag_bg; + layer.bg = if (v.window_pick == id) p.theme().sel_bg else tagGround(p, pane, active); layer.cursor = if (v.header != null) null else if (temporary.cursor) |cursor| .{ .x = cursor.x - TAG_TEXT_INSET, .y = cursor.y, .bar = cursor.bar } else @@ -793,6 +793,14 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r } } +/// A pane's tag's ground: a reading pane's tint (its focus is its grip's), +/// else the focused tag's or the tag's. +fn tagGround(p: *Pardes, pane: *const Pane, active: bool) [3]u8 { + const chrome = p.chromeTheme(); + if (pardes.Trail.reading(p, pane)) return chrome.tag_reading_bg; + return if (active) chrome.tag_active_bg else chrome.tag_bg; +} + fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void { const th = p.theme(); const chrome = p.chromeTheme(); @@ -802,7 +810,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const rows = pane.tag_rows; const page_rgb = th.bg orelse th.tag_bg; // Focus is visible along the tag without changing its live text grid. - const tag_bg = if (active) chrome.tag_active_bg else chrome.tag_bg; + const tag_bg = tagGround(p, pane, active); const tag_fg = if (active) chrome.tag_active_fg else chrome.tag_fg; const tag_style: CellStyle = .{ .fg = .{ .rgb = tag_fg }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline }; // Padding belongs to the same font row but keeps its default ink: a @@ -1046,7 +1054,7 @@ test "a tag of three rows has its grip on the first, and band under it that stil const top = p.tagTop(pane, r); try std.testing.expect(pane.tag_rows >= 3); const chrome = p.chromeTheme(); - const band = chrome.tag_active_bg; + const band = tagGround(p, pane, true); for (0..config.GUTTER) |dx| { const x = r.x + @as(u16, @intCast(dx)); try std.testing.expectEqual(Color{ .rgb = chrome.box }, s.at(x, top).style.bg); -- cgit v1.3 From 1c4aea3b0298f6f59ffec9b49f4d5e5f38c8af0f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 19:24:44 -0300 Subject: walk (pick-ui SPEC §6.3, §6.4, item 3 of the Walk UI line B): a reader's trail is words of its tag: its back steps before its path (`‹@p3 › rank.zig:14 › `, the owner's word written so that its Look is `@p3`, the newest shown_steps 3 whole and older ones folded to `·`, still their steps), its forward steps after it, dimmed in the theme's lineno ink (draw.zig inkTag, the name's tint loop made one with it), then Keep; the prefix is made in the room Trail.bytes measures (tagline.pathPrefix, no allocation past it), and the name's place after the words is tagline.nameAt (onName, typedIntoName, tagTintRange, the drafted name's caret); a click on a step's word is that step's Look (B1 with no drag, the one place it does more than select, and B3) or Exec (B2): a place gone back to (the later steps kept as forward steps) or forward to, the owner focused; a list, a search or a recipe word says it is not walked yet (their makers are D's and E's: Trail.lookStep, Trail.execStep); TEST FIRST: Trail.zig "the session of pick-ui §6.7: four jumps across three files, back by a click on a trail word, the later steps kept dimmed after the path, forward again, Keep, and a new reader on the next jump" and "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step"; core-test 998/1164 (166 skipped), pane-test 183/184, behaviour-test 277/277 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/Trail.zig | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/draw.zig | 44 ++++++++------ core/mouse.zig | 16 +++++- core/tagline.zig | 70 +++++++++++++++++++---- 4 files changed, 270 insertions(+), 30 deletions(-) (limited to 'core/draw.zig') diff --git a/core/Trail.zig b/core/Trail.zig index 63a57c2c..a198cae5 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -222,6 +222,92 @@ pub fn edited(p: *Pardes, f: *const panes.File.State) void { } +// ---- the words ---- + +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) { + .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(), + }; +} + +const sep = " › "; + +/// The steps a part writes. +fn range(t: *const Trail, part: Part) [2]usize { + return switch (part) { + .back => .{ 0, t.cur }, + .forward => .{ @min(t.n, t.cur + 1), t.n }, + }; +} + +/// The back steps' words, each with the separator after it (before the +/// path), or the forward steps', each with one before it (after the path). +pub fn write(t: *const Trail, part: Part, w: *std.Io.Writer) std.Io.Writer.Error!void { + var buf: [max_text + 16]u8 = undefined; + const r = range(t, part); + for (r[0]..r[1]) |i| switch (part) { + .back => try w.print("{s}" ++ sep, .{wordOf(t, i, &buf)}), + .forward => try w.print(sep ++ "{s}", .{wordOf(t, i, &buf)}), + }; +} + +/// The bytes `write` writes for `part`. +pub fn bytes(t: *const Trail, part: Part) usize { + var buf: [max_text + 16]u8 = undefined; + var n: usize = 0; + const r = range(t, part); + for (r[0]..r[1]) |i| n += wordOf(t, i, &buf).len + sep.len; + return n; +} + +/// 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 { + var buf: [max_text + 16]u8 = undefined; + var x: usize = 0; + const r = range(t, part); + for (r[0]..r[1]) |i| { + if (part == .forward) x += sep.len; + const n = wordOf(t, i, &buf).len; + if (off >= x and off < x + n) return @intCast(i); + x += n; + if (part == .back) x += sep.len; + } + return null; +} + +/// 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). +pub fn lookStep(p: *Pardes, id: usize, i: u8) void { + const pane = p.panes[id] orelse return; + switch (pane.trail.steps[i]) { + .place => _ = go(p, id, i) catch |err| p.reportError(.at(id), "walk", err), + .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"), + } +} + +/// Exec on step `i` (§6.3): a place and the owner as Look; a list runs its +/// source again, a search searches again, a recipe replays (not yet). +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"), + } +} + fn typeKeys(p: *Pardes, keys: []const u8) void { for (keys) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } @@ -416,3 +502,87 @@ test "the reader of a held pane too short to halve goes to the bottom of the nex const reader_id = f.p.view().active; try std.testing.expectEqual(col + 1, layout.findPane(f.p, reader_id).?.col); } + +/// Where `word` starts in pane `id`'s tag, on the screen. +fn tagCell(p: *Pardes, id: usize, word: []const u8) ![2]u16 { + const pane = p.panes[id].?; + const tag = try tagOf(p, pane); + const i = std.mem.indexOf(u8, tag, word) orelse return error.NotInTag; + const r = p.view().rects[id]; + return .{ r.x + pardes.TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, tag[0..i]))), p.tagTop(pane, r) }; +} + +/// A click with `button` on `word` in pane `id`'s tag: a press and a +/// release on its first cell. +fn clickTag(p: *Pardes, id: usize, word: []const u8, button: pardes.Mouse.Button) !void { + const at = try tagCell(p, id, word); + for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| p.update(.{ .mouse = .{ .button = button, .kind = kind, .col = at[0], .row = at[1] } }); + p.sync(); +} + +test "the session of pick-ui §6.7: four jumps across three files, back by a click on a trail word, the later steps kept dimmed after the path, forward again, Keep, and a new reader on the next jump" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + var buf: [4096 + 128]u8 = undefined; + var at: [64]u8 = undefined; + // 1 · g d on rank from the held pane: its reader, made below it. + f.look(f.held, "rank.zig", 12); + // 2 · Look at Place.of from line 14 of rank.zig: the reader walks. + typeKeys(p, "2j"); + f.look(p.view().active, "place.zig", 8); + // 3 · g d on Span from line 9: one more step in the same file. + typeKeys(p, "j"); + f.look(p.view().active, "place.zig", 40); + var r = p.view().active; + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › place.zig:9 › {s}/place.zig Keep ", .{ owner, f.dir }))); + // 4 · a click on rank.zig:14: back, the later steps after the path, + // dimmed. + try clickTag(p, r, "rank.zig:14", .left); + r = p.view().active; + try std.testing.expectEqualStrings("rank.zig:14", f.at(&at)); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › {s}/rank.zig › place.zig:9 › place.zig:40 Keep ", .{ owner, f.dir }))); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const dim = try tagCell(p, r, "place.zig:40"); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().lineno }, s.at(dim[0], dim[1]).style.fg); + // A click on a forward step goes forward to it. + try clickTag(p, r, "place.zig:9", .left); + r = p.view().active; + try std.testing.expectEqualStrings("place.zig:9", f.at(&at)); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › rank.zig:14 › {s}/place.zig › place.zig:40 Keep ", .{ owner, f.dir }))); + // Exec (B2) on the owner's word: the owner has the keyboard. + try clickTag(p, r, "‹@p", .middle); + try std.testing.expectEqual(f.held, p.view().active); + // A new step from the reader drops the forward steps. + f.look(r, "rank.zig", 50); + r = p.view().active; + try std.testing.expectEqual(p.panes[r].?.trail.cur + 1, p.panes[r].?.trail.n); + // 5 · Keep: the walk leaves its tag; the next jump from the owner + // makes a new reader. + pardes.exec.runBuiltin(p, .Keep, r, "Keep", null); + p.sync(); + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "{s}/rank.zig Save", .{f.dir}))); + f.look(f.held, "place.zig", 20); + const fresh = p.view().active; + try std.testing.expect(fresh != r and fresh != f.held and p.panes[r] != null); + try std.testing.expectEqual(owner, p.panes[fresh].?.trail.steps[0].owner); +} + +test "a trail keeps its newest three back steps whole, older ones fold to ·, and a click on a · goes back to its step" { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + const owner = p.panes[f.held].?.serial; + for ([_]usize{ 10, 20, 30, 40, 50 }) |line| f.look(f.held, "rank.zig", line); + const r = p.view().active; + var buf: [4096 + 128]u8 = undefined; + var at: [64]u8 = undefined; + try std.testing.expect(std.mem.startsWith(u8, try tagOf(p, p.panes[r].?), try std.fmt.bufPrint(&buf, "‹@p{d} › · › rank.zig:20 › rank.zig:30 › rank.zig:40 › {s}/rank.zig Keep ", .{ owner, f.dir }))); + try clickTag(p, r, "·", .right); + try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); +} diff --git a/core/draw.zig b/core/draw.zig index f00a7583..91a14c98 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -619,7 +619,7 @@ fn tagTintRange(pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { } return null; } - var start: usize = 0; + var start: usize = tagline.nameAt(pane); const name = if (pane.file) |file| if (pane.prompt == .name) pane.input.own orelse file.path else file.path else if (pane.addon) |k| blk: { @@ -793,6 +793,25 @@ fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r } } +/// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its +/// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w` +/// wide) they wrapped onto. +fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8) void { + const a = modal.positionAt(shown.text, shown.fromReal(lo)); + const b = modal.positionAt(shown.text, shown.fromReal(hi)); + var row = @max(a.row, top); + while (row <= b.row and row < top + lines.len) : (row += 1) { + const line = lines[row - top]; + const from = if (row == a.row) a.col else 0; + const to = if (row == b.row) b.col else line.len; + var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); + const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); + const row_y = y + @as(u16, @intCast(row - top)); + while (col < end and col < w) : (col += 1) + s.at(x + @as(u16, @intCast(col)), row_y).style.fg = .{ .rgb = ink }; + } +} + /// A pane's tag's ground: a reading pane's tint (its focus is its grip's), /// else the focused tag's or the tag's. fn tagGround(p: *Pardes, pane: *const Pane, active: bool) [3]u8 { @@ -840,7 +859,7 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, else if (pane.prompt == .name) caret: { // A drafted name is the prefix: its caret is on row 0 of the tag, // the rows it wrapped onto included. - const at = modal.positionAt(shown.text, shown.fromReal(@intCast(@max(0, pane.input.cur_col)))); + const at = modal.positionAt(shown.text, shown.fromReal(tagline.nameAt(pane) + @as(usize, @intCast(@max(0, pane.input.cur_col))))); if (at.row < top or at.row >= top + rows) break :caret null; break :caret .{ .row = at.row - top, .at = @min(lines[at.row - top].len, at.col) }; } else if (pane.focus == .tag and pane.prompt == .none) caret: { @@ -860,22 +879,11 @@ fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, // The name's tint, in the tag's first line, over whichever rows it // wrapped onto. const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len]; - if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) { - const name_fg = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg; - const a = modal.positionAt(shown.text, shown.fromReal(name.lo)); - const b = modal.positionAt(shown.text, shown.fromReal(@min(name.hi, first_line.len))); - var row = @max(a.row, top); - while (row <= b.row and row < top + rows) : (row += 1) { - const line = lines[row - top]; - const from = if (row == a.row) a.col else 0; - const to = if (row == b.row) b.col else line.len; - var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len)); - const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len)); - const y = tag_y + @as(u16, @intCast(row - top)); - while (col < end and col < tw) : (col += 1) - s.at(tx + @as(u16, @intCast(col)), y).style.fg = .{ .rgb = name_fg }; - } - }; + if (tagTintRange(pane, first_line, prefix_len)) |name| if (name.lo < name.hi) + inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg); + // 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); // The tag's own selections, while it has the keyboard, a line at a // time. A range of one grapheme is the block cursor, not a selection. if (pane.focus == .tag) { diff --git a/core/mouse.zig b/core/mouse.zig index 14700995..1356ace4 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1211,8 +1211,12 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { // click on the path, if it can be renamed, drafts a name. const row: usize = @intCast(@max(0, sl.r1)); const col: usize = @intCast(@max(0, sl.c1)); - if (!dragged and tagline.onName(p.settings.tab, pane, row, col)) { - tagline.draftName(p, pane, col); + // A plain click on a word of its walk is its Look + // (pick-ui SPEC §6.3): the one place B1 does more than + // select. + if (!dragged and tagline.trailClick(p, s.id, row, col, false)) return; + if (if (dragged) null else tagline.onName(p.settings.tab, pane, row, col)) |at| { + tagline.draftName(p, pane, at); } else if (tagline.tagOffsetAt(p, pane, row, col)) |cursor| { pane.tag.cur_row = @intCast(cursor.row); pane.tag.cur_col = @intCast(cursor.col); @@ -1278,6 +1282,14 @@ fn dragRelease(p: *Pardes, button: Mouse.Button) void { return; } }; + // On a word of the tag's walk: that step's Look or Exec + // (Trail.zig), not the word's. + if (clk.r0 >= 0 and clk.r0 < pane.tag_rows and clk.r0 == clk.r1 and clk.c0 == clk.c1 and clk.c0 >= 0 and + tagline.trailClick(p, s.id, @intCast(clk.r0), @intCast(clk.c0), s.button == config.exec_button)) + { + pane.sel[b].state = .none; + return; + } var operand = exec.operandAt(p, pane, clk, true, s.button == config.look_button); if (operand.expanded) |expanded| pane.sel[b] = expanded diff --git a/core/tagline.zig b/core/tagline.zig index 1cab2166..451198fd 100644 --- a/core/tagline.zig +++ b/core/tagline.zig @@ -157,8 +157,18 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co // while a jump would step it (Trail.zig). if (pane.file) |f| { const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; - if (!pardes.Trail.reading(p, pane)) return name; - return std.mem.concat(arena, u8, &.{ name, " Keep" }); + const keep = pardes.Trail.reading(p, pane); + if (pane.trail.n == 0 and !keep) return name; + // Its walk's back steps before the name, its forward steps after + // it (Trail.zig), in the room `bytes` measures them. + const keep_word = if (keep) " Keep" else ""; + const room = try arena.alloc(u8, pane.trail.bytes(.back) + name.len + pane.trail.bytes(.forward) + keep_word.len); + var w: std.Io.Writer = .fixed(room); + pane.trail.write(.back, &w) catch return error.OutOfMemory; + w.writeAll(name) catch return error.OutOfMemory; + pane.trail.write(.forward, &w) catch return error.OutOfMemory; + w.writeAll(keep_word) catch return error.OutOfMemory; + return w.buffered(); } // A terminal showing kitty graphics says how it draws them, as an image // pane does, and so does one drawing them as glyph art by choice. @@ -171,6 +181,37 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co return (try shownDir(arena, pane)); } +/// Where a file's name starts in its tag: after its walk's back steps. +pub fn nameAt(pane: *const Pane) usize { + return if (pane.file != null) pane.trail.bytes(.back) else 0; +} + +/// The bytes of a file's tag its walk's forward steps take, after its name. +pub fn forwardSpan(pane: *const Pane) [2]usize { + const f = pane.file orelse return .{ 0, 0 }; + const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; + const lo = pane.trail.bytes(.back) + name.len; + return .{ lo, lo + pane.trail.bytes(.forward) }; +} + +/// A click at shown row `row`, display column `col` of pane `id`'s tag on +/// a word of its walk: that step's Look, or with `run` its Exec +/// (Trail.zig, pick-ui SPEC §6.3). True when it was on one. +pub fn trailClick(p: *Pardes, id: usize, row: usize, col: usize, run: bool) bool { + const pane = p.panes[id] orelse return false; + if (pane.trail.n == 0 or pane.file == null) return false; + const at = tagOffsetAt(p, pane, row, col) orelse return false; + if (at.row != 0) return false; + const lead = nameAt(pane); + const ahead = forwardSpan(pane); + const step = if (at.col < lead) + pane.trail.stepAt(.back, at.col) + else if (at.col >= ahead[0] and at.col < ahead[1]) pane.trail.stepAt(.forward, at.col - ahead[0]) else null; + const i = step orelse return false; + if (run) pardes.Trail.execStep(p, id, i) else pardes.Trail.lookStep(p, id, i); + return true; +} + pub fn tagPrefix(p: *Pardes, pane: *Pane) ![]u8 { return prefixIn(p, p.scratch.allocator(), pane); } @@ -347,14 +388,20 @@ pub fn tagOffsetAt(p: *Pardes, pane: *Pane, row: usize, col: usize) ?modal.Curso return modal.positionAt(whole, @min(shown.toReal(off), whole.len)); } -/// Whether display column `col` of the tag's shown row `row` is on a name -/// a click drafts: a saving file's path. -pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) bool { - if (row + pane.tag_top != 0) return false; - const f = pane.file orelse return false; - if (!panes.Output.fileTraits(f.output).saves) return false; +/// The display column in the name a click drafts at display column `col` +/// of the tag's shown row `row`: on a saving file's path, after its walk's +/// words; null off it. +pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) ?usize { + if (row + pane.tag_top != 0) return null; + const f = pane.file orelse return null; + if (!panes.Output.fileTraits(f.output).saves) return null; const name = if (pane.prompt == .name) pane.input.own orelse f.path else f.path; - return col < panes.File.displayWidth(tab, name); + var room: [pardes.Trail.max_steps * (pardes.Trail.max_text + 32)]u8 = undefined; + var w: std.Io.Writer = .fixed(&room); + pane.trail.write(.back, &w) catch return null; + const lead = panes.File.displayWidth(tab, w.buffered()); + if (col < lead or col >= lead + panes.File.displayWidth(tab, name)) return null; + return col - lead; } /// A key typed into a saving file's path, with the tag's cursor there, @@ -364,7 +411,10 @@ pub fn onName(tab: u16, pane: *Pane, row: usize, col: usize) bool { pub fn typedIntoName(p: *Pardes, pane: *Pane, key: pardes.Key) bool { const f = pane.file orelse return false; if (!panes.Output.fileTraits(f.output).saves or pane.tag.cur_row != 0) return false; - const at: usize = @intCast(@max(0, pane.tag.cur_col)); + // The name starts after its walk's words. + const col: usize = @intCast(@max(0, pane.tag.cur_col)); + if (col < nameAt(pane)) return false; + const at = col - nameAt(pane); const back = key.cp == pardes.Key.backspace or pardes.hit(key, config.insert_backspace_alias); const forward = key.cp == pardes.Key.delete or pardes.hit(key, config.insert_delete_alias); const typed = key.text.len > 0 and !key.ctrl and !key.alt; -- cgit v1.3 From b77eac2511cb247ebae07494bbb13b2e6a1139ab Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 24 Nov 2026 20:13:20 -0300 Subject: Pick: a picker unrolls in the tag of the pane its Enter lands in (walk UI, pick-ui SPEC.md §3.5, §5.1-§5.3, item 1 of line D): core/Pick.zig, a picker the session's (Pardes.picks, a ring of max_lists 8, each named by a serial from the panes' counter so the disk engine's target names it as it names a pane, Stream.receive giving it its batches through Query.take, now taking either), live in a view's tag as TagState kind pick (TagState.pick its serial, host its landing pane: the asking pane when it reads, else its reader, else a pane made for it where its reader would go, an empty +Preview until the preview fills it, taken away by Esc); its sources Query.zig's producers re-presented: Find's listing from the disk engine's worker and Recent, Panes, Symbols (Query.gather, the name gathering startSource did, now shared), the jump list (Output.jumpsText, what +Jumps lists) and a list's rows handed over (a language server's answer, titled by its word), all scored by Names a frame's count at a time; Grep's lines from the worker after its pause, All the places each open text finds (as Query's All); the head ` ␣ f Find pick 3/6 ⋯` (key cells from the source's leader keys, the word, the field, cursor/total, ⋯ while scoring or searching) and a second row of the other sources' words under spines continued from the key cells (TagState.Head.cells counts the second row's spines, which Band.findSpines hangs the band from); the band's rows eleven around the cursor, made in the frame's arena for the rows shown only (O(rows shown): a Find of 50k paths makes 11 rows a frame), a place prefix in the name's tint, a path's directory dimmed, matches in the match tint; the preview is the landing pane's body: the current row's file at its line (a Grep's matches lit as its places), taken from an open pane's text or read, on the frame after the cursor stood (never on the key that moved it: Pick.busy asks the frame), a reading pane's own text set aside meanwhile (Pick.aside) and put back when it rolls up, a twinned text never previewed in; TagState.toRest rolls a live picker up first, whoever puts the tag to rest; A's API: TagState.Kind pick and its pick field, Band.takes/of's pick arm, Band.Row mark/pre/faint (drawn in draw.zig joinBand), Band.place holding a step to the band's width, TagState.point on a picker's row; TEST FIRST: Pick.zig "a picker from a held pane unrolls in a reader made for it ..." and "a picker from a reading pane unrolls in that pane, its own text set aside while it previews and back on Esc"; core-test 1014/1180 (166 skipped), 0 failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- core/Band.zig | 16 +- core/Core.zig | 7 +- core/Output.zig | 18 +- core/Pick.zig | 785 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/Query.zig | 33 ++- core/Stream.zig | 2 + core/TagState.zig | 14 +- core/draw.zig | 3 +- core/keys.zig | 1 + core/panes.zig | 1 + core/root.zig | 2 + 11 files changed, 860 insertions(+), 22 deletions(-) create mode 100644 core/Pick.zig (limited to 'core/draw.zig') diff --git a/core/Band.zig b/core/Band.zig index 079a1bcc..b5e90125 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -46,6 +46,12 @@ pub const Row = struct { /// Its place in the list its step shows (a level's child, a hit): what /// a click on it moves the cursor to. index: u16 = 0, + /// Marked in a picker's list: `•` before it. + mark: bool = false, + /// Its first `pre` bytes are a place (`file:line`, `@p3`): the name's + /// tint; the `faint` bytes after them dimmed (a path's directory). + pre: u16 = 0, + faint: u16 = 0, }; /// A column of rows. A level of more than 9 rows flows into two columns @@ -89,6 +95,7 @@ pub fn takes(p: *Pardes, id: usize) u16 { .rest => {}, .help => return @max(doc_rows, @max(1, @min(Cursor.shown, s.nhits))), .describe => return 0, + .pick => return @import("Pick.zig").bandRows(p), } if (!s.unrolled) return 0; const cur = TagState.current(p, v) orelse return 0; @@ -117,6 +124,11 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { return band; }, .describe => return null, + .pick => { + for (try @import("Pick.zig").steps(p, arena)) |step| band.add(step); + try band.place(p, arena, pane, id); + return band; + }, } const cur = TagState.current(p, v).?; // The level, its cursor on the child it names, else the leaf its group @@ -263,7 +275,7 @@ fn place(band: *Band, p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usi if (step.doc) continue; var widest: usize = 0; for (step.rows) |row| widest = @max(widest, File.displayWidth(tab, row.text)); - const column: u16 = @intCast(@min(widest + 2, std.math.maxInt(u16) / 4)); + const column: u16 = @intCast(@min(widest + 2, std.math.maxInt(u16) / 4, @max(min_width, band.columns -| x))); step.width = if (step.two) 2 * column + 1 else column; need += step.width + 2; } @@ -368,7 +380,7 @@ pub fn line(band: *const Band, arena: std.mem.Allocator, tab: u16, y: u16) !Line while (col < start) : (col += 1) try text.append(arena, ' '); if (y >= shown or k >= step.rows.len) continue; const row = step.rows[k]; - const lead: []const u8 = if (step.doc) "" else if (row.on) "▶ " else " "; + const lead: []const u8 = if (step.doc) "" else if (row.on) "▶ " else if (row.mark) "• " else " "; try text.appendSlice(arena, lead); col += @intCast(File.displayWidth(tab, lead)); try parts.append(arena, .{ .col = col, .width = per -| @as(u16, @intCast(File.displayWidth(tab, lead))), .row = row }); diff --git a/core/Core.zig b/core/Core.zig index 11f254ab..6ee82370 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -396,6 +396,9 @@ lsp_ask: Ask = .{}, pipe: Pipe = .{}, /// A query pane's Grep, and its Find's listing, on a worker (Stream.zig). stream: Stream = .{}, +/// The pickers: the one live in a view's tag, and the last ones rolled +/// up, which their list words and Resume reopen (Pick.zig). +picks: [pardes.Pick.max_lists]?*pardes.Pick = @splat(null), /// The panes' `Changes` (jj.zig): what was asked of jj, and what it /// answered for each pane, by its serial: the session's, not a field of /// every pane (a digest and a rollback copy walk every pane's fields at @@ -926,6 +929,7 @@ pub fn deinit(p: *Pardes) void { look.cancelLookHover(p); if (p.repl_ask) |ask| p.gpa.free(ask.text); for (0..MAX_PANES) |id| p.dropPendingWrite(id); + pardes.Pick.deinitAll(p); // The panes, then those retired these last two frames. inline for (.{ &p.panes, &p.retired_panes.previous, &p.retired_panes.current }) |slots| for (slots) |*slot| if (slot.*) |pane| { pardes.panes.teardownPane(p, pane); @@ -1824,6 +1828,7 @@ fn updateInner(p: *Pardes, ev: Event) void { for (p.panes) |slot| if (slot) |pane| pane.places.settle(p, pane, ev == .tick); // Each live query pane's rows, made from what changed this step. pardes.panes.Query.produceAll(p, ev == .tick, ev == .key); + pardes.Pick.produceAll(p, ev == .tick); _ = p.scratch.reset(.{ .retain_with_limit = memory.limits.scratch_kept }); p.sync(); p.fsReport(); @@ -2162,7 +2167,7 @@ pub fn nextWake(p: *const Pardes) ?u64 { for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next; const kinds = pardes.Kind.wake(p); if (kinds) |due| if (due <= next) return next; - if (pardes.panes.Query.busy(p)) return next; + if (pardes.panes.Query.busy(p) or pardes.Pick.busy(p)) return next; // A typed Grep waiting out its pause. const streaming = p.stream.wake(next); if (streaming) |due| if (due <= next) return next; diff --git a/core/Output.zig b/core/Output.zig index 380fadb1..b64d2ff3 100644 --- a/core/Output.zig +++ b/core/Output.zig @@ -491,16 +491,23 @@ pub fn fillRows(p: *Pardes, id: usize, dir: []const u8, from: Origin, name: ?[]c pub fn openJumps(p: *Pardes, id: usize) !void { var out: std.Io.Writer.Allocating = .init(p.gpa); errdefer out.deinit(); + try jumpsText(p, &out.writer); + try refillOrOpen(p, id, .{ .cmd = .Jumplist }, try out.toOwnedSlice()); +} + +/// The live view's jump list as a +Jumps lists it, a place a row, the +/// oldest first (its picker's rows too, Pick.zig). +pub fn jumpsText(p: *Pardes, w: *std.Io.Writer) !void { for (p.view().jumps.list[0..p.view().jumps.n]) |j| { // A closed file's entry: marked, and a look at it opens it again. if (j.serial == 0) { const e = p.recentByHash(j.path_hash) orelse continue; // A kind's place has no Look's column. if (j.place.len > 0) { - try out.writer.print("{s}:{d} (closed)\n", .{ e.path, @max(1, j.line) }); + try w.print("{s}:{d} (closed)\n", .{ e.path, @max(1, j.line) }); continue; } - try out.writer.print("{s}:{d}:{d} (closed)\n", .{ e.path, @max(1, j.line), @max(1, j.col) }); + try w.print("{s}:{d}:{d} (closed)\n", .{ e.path, @max(1, j.line), @max(1, j.col) }); continue; } const jp = p.panes[j.pane] orelse continue; @@ -520,13 +527,12 @@ pub fn openJumps(p: *Pardes, id: usize) !void { var cut = @min(what.len, 120); while (cut > 0 and cut < what.len and what[cut] & 0xc0 == 0x80) cut -= 1; if (j.line == 0) - try out.writer.print("{s} {s}\n", .{ loc, what[0..cut] }) + try w.print("{s} {s}\n", .{ loc, what[0..cut] }) else if (j.place.len > 0) - try out.writer.print("{s}:{d} {s}\n", .{ loc, j.line, what[0..cut] }) + try w.print("{s}:{d} {s}\n", .{ loc, j.line, what[0..cut] }) else - try out.writer.print("{s}:{d}:{d} {s}\n", .{ loc, j.line, j.col, what[0..cut] }); + try w.print("{s}:{d}:{d} {s}\n", .{ loc, j.line, j.col, what[0..cut] }); } - try refillOrOpen(p, id, .{ .cmd = .Jumplist }, try out.toOwnedSlice()); } /// The state file's dumps (Db.listDumps: label, date, size, pane count, diff --git a/core/Pick.zig b/core/Pick.zig new file mode 100644 index 00000000..30cf74ab --- /dev/null +++ b/core/Pick.zig @@ -0,0 +1,785 @@ +//! A picker (pick-ui SPEC.md §5, direction E): a list of places, panes or +//! names unrolled in the tag of the pane its Enter lands in (§3.5): the +//! asking pane when it reads, else its reader, made for the picker when it +//! has none and taken away again by Esc. Its query is typed in that tag's +//! field (TagState.field), its rows hang in the band under it (Band.zig) +//! eleven at a time around the cursor, and that pane's body is the preview: +//! the current row's file at its line, its own text set aside meanwhile and +//! put back when the picker rolls up. +//! +//! The rows are Query.zig's producers', re-presented: names scored by +//! Names.zig a frame's count at a time (Find's listing from the disk +//! engine's worker, Recent, Panes, the asking text's declarations, the +//! rows a language server answered, the jump list), Grep's lines from the +//! disk engine's worker (Stream.zig: its pause, its cap). A frame makes +//! only the rows it shows, in its arena. +//! +//! A picker is the session's, named by a serial drawn from the panes' +//! counter (`id`), so the disk engine's `target` names it as it names a +//! pane; one is live in a view's tag (TagState.pick), and the last +//! `max_lists` are kept after they roll up. +const Pick = @This(); + +const std = @import("std"); +const pardes = @import("root.zig"); +const config = @import("config.zig"); +const layout = @import("layout.zig"); +const look = @import("look.zig"); +const panes = @import("panes.zig"); +const filesystem = @import("fs.zig"); +const exec = @import("exec.zig"); +const bindings = @import("bindings.zig"); +const Kind = @import("Kind.zig"); +const Names = @import("Names.zig"); +const Stream = @import("Stream.zig"); +const Trail = @import("Trail.zig"); +const TagState = @import("TagState.zig"); +const Band = @import("Band.zig"); +const Query = panes.Query; +const File = panes.File; +const Output = panes.Output; +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const Key = pardes.Key; + +/// What a picker lists (SPEC §5.1, the sources pardes has): the files +/// under the asking pane's directory, the lines of them holding the +/// query, the files opened lately, the panes, the asking text's +/// declarations, the lines holding the query in every open pane (All, its +/// places in each), the jump list, and rows handed over (a language +/// server's answer), titled by their word. +pub const Source = enum { find, grep, recent, panes, symbols, all, jumplist, list }; + +/// Each source's word in the tag and the leader keys of the builtin that +/// opens it, its key cells (`␣ f Find`). +const words = std.EnumArray(Source, struct { word: []const u8, keys: []const u8 }).init(.{ + .find = .{ .word = "Find", .keys = "f" }, + .grep = .{ .word = "Grep", .keys = "f" }, + .recent = .{ .word = "Recent", .keys = "f" }, + .panes = .{ .word = "Panes", .keys = "b" }, + .symbols = .{ .word = "Symbols", .keys = "s" }, + .all = .{ .word = "All", .keys = "" }, + .jumplist = .{ .word = "Jumplist", .keys = "j" }, + .list = .{ .word = "List", .keys = "" }, +}); + +/// The pickers kept after they roll up, for their list words and Resume. +pub const max_lists = 8; + +/// Its serial: the disk engine's target, a list word's index. +id: u32, +source: Source, +/// Its word, `Find`, or a list's own (`Refs`). +word: Kind.Buf(24) = .{}, +/// The leader keys under which its builtin is, drawn as key cells. +keys: Kind.Buf(4) = .{}, +/// Where Find and Grep look; what the rows' paths are relative to. +dir: Kind.Buf(4096) = .{}, +/// The pane that asked, and the one its Enter lands in, by serial. +owner: u32 = 0, +landing: u32 = 0, +/// The landing pane was made for it: Esc takes it away. +made: bool = false, +/// Unrolled in a view's tag now. +unrolled: bool = false, +names: Names = .{}, +/// A Grep's lines, as the disk engine sends them (Query.take). +found: std.ArrayList(Query.Found) = .empty, +found_bytes: std.ArrayList(u8) = .empty, +/// A list's rows, as they were handed over (`path:line:col text`). +rows: []u8 = &.{}, +/// The query and the cursor as it rolled up (live, they are its view's +/// field and cursor). +query: Kind.Buf(256) = .{}, +at: u32 = 0, +/// The landing pane's own text while its body previews (a reading pane: +/// put back when the picker rolls up), and its caret then. +aside: ?File.State = null, +aside_row: i32 = 0, +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, + +const no_row = std.math.maxInt(u32); + +pub fn deinit(pk: *Pick, gpa: std.mem.Allocator) void { + pk.names.deinit(gpa); + pk.found.deinit(gpa); + pk.found_bytes.deinit(gpa); + gpa.free(pk.rows); +} + +/// Picker `id`, kept or live. +pub fn byId(p: *const Pardes, id: u32) ?*Pick { + if (id == 0) return null; + for (p.picks) |slot| if (slot) |pk| if (pk.id == id) return pk; + return null; +} + +/// The picker live in the live view's tag. +pub fn live(p: *const Pardes) ?*Pick { + const s = &p.view().tag_state; + if (s.kind != .pick) return null; + const pk = byId(p, s.pick) orelse return null; + return if (pk.unrolled) pk else null; +} + +/// A picker of `source` asked from pane `from`, unrolled in the tag of the +/// pane its Enter will land in (SPEC §3.5), its field `query`. `rows` are +/// a list's (`source` `list`, titled `word`). +pub fn open(p: *Pardes, from: usize, source: Source, word: []const u8, query: []const u8, rows: []const u8) void { + const asker = p.panes[from] orelse return; + TagState.toRest(p); + const pk = make(p, source) catch |err| return p.reportError(.at(from), "pick", err); + pk.word.set(if (word.len > 0) word else words.get(source).word); + pk.keys.set(words.get(source).keys); + pk.dir.set(Pardes.paneDir(asker)); + pk.owner = asker.serial; + pk.rows = p.gpa.dupe(u8, rows) catch |err| return p.reportError(.at(from), "pick", err); + const landing = landingFor(p, from) catch |err| return p.reportError(.at(from), "pick", err); + // The keyboard stays where it was: the landing pane is the tag's host. + p.view().active = from; + pk.landing = p.panes[landing].?.serial; + pk.made = landing != from and Trail.readerOf(p, asker) != landing and !Trail.reading(p, asker); + unroll(p, pk, query); + start(p, pk); + filter(p, pk); +} + +/// A new picker in the ring, the oldest kept one dropped for it. +fn make(p: *Pardes, source: Source) !*Pick { + var slot: ?usize = null; + for (p.picks, 0..) |s, i| if (s == null) { + slot = i; + break; + }; + if (slot == null) { + // The oldest not live. + var oldest: ?usize = null; + for (p.picks, 0..) |s, i| if (s) |pk| if (!pk.unrolled and (oldest == null or pk.id < p.picks[oldest.?].?.id)) { + oldest = i; + }; + const i = oldest orelse return error.NoPickerSlots; + p.picks[i].?.deinit(p.gpa); + p.gpa.destroy(p.picks[i].?); + p.picks[i] = null; + slot = i; + } + const pk = try p.gpa.create(Pick); + p.next_serial += 1; + pk.* = .{ .id = p.next_serial, .source = source }; + p.picks[slot.?] = pk; + return pk; +} + +/// The pane a picker from pane `from` lands in: `from` when it reads; its +/// reader when it is held and has one; else a pane made under it as its +/// reader would be, empty until the preview fills it. +fn landingFor(p: *Pardes, from: usize) !usize { + const asker = p.panes[from].?; + if (Trail.reading(p, asker)) return from; + if (Trail.readerOf(p, asker)) |r| return r; + const free = p.freeSlot() orelse return error.NoPaneSlots; + const content = try p.gpa.dupe(u8, ""); + errdefer p.gpa.free(content); + _ = try Output.open(p, free, Pardes.paneDir(asker), .preview, "", content); + layout.placeNew(p, from, from, free, .reader); + layout.compute(p); + return free; +} + +/// Picker `pk` live in the live view's tag: its host its landing pane, its +/// field `query`, the cursor where it was. +fn unroll(p: *Pardes, pk: *Pick, query: []const u8) void { + const v = p.view(); + const s = &v.tag_state; + s.kind = .pick; + s.pick = pk.id; + s.host = pk.landing; + s.field.set(query); + s.rows = .{ .at = @intCast(@min(pk.at, std.math.maxInt(u16))) }; + pk.unrolled = true; + pk.shown = no_row; + if (p.paneBySerial(pk.landing)) |id| s.previewing.set(id); +} + +/// Its source begun: the names gathered, a listing started. +fn start(p: *Pardes, pk: *Pick) void { + pk.names.reset(); + pk.found.clearRetainingCapacity(); + pk.found_bytes.clearRetainingCapacity(); + const dir = pk.dir.slice(); + switch (pk.source) { + .find => { + Stream.query(p, pk.id, .files, "", dir, .now) catch |err| return p.reportError(.at(p.view().active), "pick", err); + if (p.fs.serving) Stream.step(p); + }, + .grep, .all => {}, + .recent => Query.gather(p, &pk.names, .recent, dir, null, null), + .panes => Query.gather(p, &pk.names, .panes, dir, null, null), + .symbols => Query.gather(p, &pk.names, .symbols, dir, null, p.paneBySerial(pk.owner)), + .jumplist => { + var out: std.Io.Writer.Allocating = .init(p.gpa); + defer out.deinit(); + Output.jumpsText(p, &out.writer) catch return; + addRows(p, pk, out.written()); + }, + .list => addRows(p, pk, pk.rows), + } +} + +/// Rows of text as names, a row each, matched whole. +fn addRows(p: *Pardes, pk: *Pick, text: []const u8) void { + var lines = std.mem.splitScalar(u8, text, '\n'); + var i: u32 = 0; + while (lines.next()) |ln| : (i += 1) { + if (ln.len == 0) continue; + pk.names.add(p.gpa, ln, 0, ln.len, 0, i) catch return; + } +} + +/// 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. +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 (pk.source == .grep) { + pk.found.clearRetainingCapacity(); + pk.found_bytes.clearRetainingCapacity(); + if (q.len > 0) { + Stream.query(p, pk.id, .grep, q, pk.dir.slice(), if (p.fs.serving) .now else .typed) catch |err| return p.reportError(.at(p.view().active), "pick", err); + if (p.fs.serving) Stream.step(p); + } 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. + for (p.panes) |slot| { + const tp = slot orelse continue; + if (!searchable(tp)) continue; + if (q.len == 0) { + 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 +/// pane's nor a preview's. +fn searchable(tp: *const Pane) bool { + const f = tp.file orelse return false; + if (tp.query != null) return false; + if (f.output) |o| if (o.from == .preview) return false; + return true; +} + +/// The panes All's places are in, in the order of their serials. +fn allPanes(p: *const Pardes, pk: *const Pick, out: *[pardes.MAX_PANES]usize) []usize { + var n: usize = 0; + for (p.panes, 0..) |slot, i| if (slot) |tp| if (tp.places.from == pk.id and tp.places.shows(tp) and tp.places.hits.items.len > 0) { + out[n] = i; + n += 1; + }; + std.mem.sort(usize, out[0..n], p, struct { + fn lt(pp: *const Pardes, a: usize, b: usize) bool { + return pp.panes[a].?.serial < pp.panes[b].?.serial; + } + }.lt); + return out[0..n]; +} + +/// The disk engine's rows for picker `target` (Stream.receive). +pub fn take(p: *Pardes, target: u32, kind: Stream.Kind, rows: []const u8) bool { + const pk = byId(p, target) orelse return false; + Query.take(p, pk, kind, rows); + // A listing's names, scored as they come. + if (kind == .files) pk.names.rescore(p.gpa); + return true; +} + +/// Pane `serial`'s declarations came (Stream.receiveParse): a Symbols +/// picker asked from it gathers them. +pub fn declsArrived(p: *Pardes, serial: u32) void { + for (p.picks) |slot| if (slot) |pk| if (pk.unrolled and pk.source == .symbols and pk.owner == serial and pk.names.cands.items.len == 0) { + Query.gather(p, &pk.names, .symbols, pk.dir.slice(), null, p.paneBySerial(serial)); + pk.names.typed(p.gpa, pk.names.queryText()); + pk.names.rescore(p.gpa); + }; +} + +/// After every step: each live picker's names scored on (a frame's count), +/// and on a frame of its own after the cursor stood, the preview of the +/// current row. +pub fn produceAll(p: *Pardes, tick: bool) void { + for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { + if (pk.names.busy()) pk.names.rescore(p.gpa); + if (tick) preview(p, pk); + }; +} + +/// The frames a live picker still owes: scoring, a preview (nextWake's). +pub fn busy(p: *const Pardes) bool { + for (p.picks) |slot| if (slot) |pk| if (pk.unrolled) { + if (pk.names.busy() or pk.shown != p.view().tag_state.rows.at) return true; + }; + return false; +} + +// ---- the rows ---- + +/// The rows the list has now. +pub fn count(p: *const Pardes, pk: *const Pick) usize { + switch (pk.source) { + .grep => return pk.found.items.len, + .all => { + var order: [pardes.MAX_PANES]usize = undefined; + var n: usize = 0; + for (allPanes(p, pk, &order)) |i| n += p.panes[i].?.places.hits.items.len; + return n; + }, + else => return pk.names.hits.items.len, + } +} + +/// 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 }; + +/// Row `i` as the band shows it and what it points at, its text in +/// `arena`. +const Row = struct { text: []const u8, pre: u16 = 0, faint: u16 = 0, dim: bool = false, marks: []const u16 = &.{}, target: Target = .none }; + +fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { + const dir = pk.dir.slice(); + if (pk.source == .all) { + var order: [pardes.MAX_PANES]usize = undefined; + var k = i; + for (allPanes(p, pk, &order)) |id| { + const tp = p.panes[id].?; + const hits = tp.places.hits.items; + if (k >= hits.len) { + k -= hits.len; + continue; + } + const f = &tp.file.?; + const h = hits[k]; + const line = @import("Places.zig").lineOf(try File.lineIndex(p.gpa, f), h.start); + const ls = File.lineStart(p.gpa, f, line); + const le = std.mem.indexOfScalarPos(u8, f.content, h.start, '\n') orelse f.content.len; + const whole = f.content[ls..le]; + const lead = whole.len - std.mem.trimStart(u8, whole, " \t").len; + const name = if (f.output == null) std.Io.Dir.path.basename(f.path) else ""; + const pre = try std.fmt.allocPrint(arena, config.pane_addr ++ "{d} {s}:{d}", .{ tp.serial, name, line + 1 }); + const text = try std.fmt.allocPrint(arena, "{s} {s}", .{ pre, whole[lead..@min(whole.len, lead + 240)] }); + var marks: std.ArrayList(u16) = .empty; + var c = h.start - ls; + while (c < h.end - ls and c >= lead and pre.len + 1 + c - lead < text.len) : (c += 1) try marks.append(arena, @intCast(pre.len + 1 + c - lead)); + const target: Target = if (f.output == null) .{ .place = .{ .path = f.path, .at = .{ .line = line + 1, .col = h.start - ls + 1 } } } else .{ .pane = tp.serial }; + return .{ .text = text, .pre = @intCast(pre.len), .marks = marks.items, .target = target }; + } + return .{ .text = "" }; + } + if (pk.source == .grep) { + 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]; + const lead = code.len - std.mem.trimStart(u8, code, " \t").len; + const pre = try std.fmt.allocPrint(arena, "{f}:{d}", .{ look.quoted(name), fd.line }); + const text = try std.fmt.allocPrint(arena, "{s} {s}", .{ pre, code[lead..] }); + var marks: std.ArrayList(u16) = .empty; + var c = fd.col; + while (c < fd.end and c >= lead and pre.len + 1 + c - lead < text.len) : (c += 1) try marks.append(arena, @intCast(pre.len + 1 + c - lead)); + const path = try std.Io.Dir.path.join(arena, &.{ dir, name }); + return .{ .text = text, .pre = @intCast(pre.len), .marks = marks.items, .target = .{ .place = .{ .path = path, .at = .{ .line = fd.line, .col = fd.col + 1 } } } }; + } + var marks: [Names.max_query * 4]u16 = undefined; + var nmarks: usize = 0; + const text = pk.names.row(i, &marks, &nmarks); + const cand = pk.names.cands.items[pk.names.hits.items[i].cand]; + var row: Row = .{ .text = text, .marks = try arena.dupe(u16, marks[0..nmarks]) }; + switch (pk.source) { + .find => { + 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 = .{} } }; + }, + .panes => { + row.pre = cand.key -| 1; + row.dim = p.paneBySerial(cand.ref) == null; + row.target = .{ .pane = cand.ref }; + }, + .symbols => { + row.pre = 6; + const owner = p.panes[p.paneBySerial(pk.owner) orelse return row] orelse return row; + const f = owner.file orelse return row; + row.target = .{ .place = .{ .path = f.path, .at = .{ .line = cand.ref } } }; + }, + .recent, .jumplist, .list, .grep, .all => { + const t = look.parsePathLine(text); + row.pre = @intCast(t.end); + if (t.path.len == 0) return row; + const path = if (t.path[0] == '/' or t.path[0] == '@') t.path else try std.Io.Dir.path.join(arena, &.{ dir, t.path }); + row.target = .{ .place = .{ .path = path, .at = t.at } }; + }, + } + return row; +} + +// ---- the tag ---- + +/// The head of the landing pane's tag (SPEC §5.3): the key cells, the +/// source's word, the query, the count `cursor/total` and `•marked`; on a +/// second row the other sources' words, under spines from the key cells. +pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { + const pk = live(p) orelse return null; + const s = &p.view().tag_state; + var text: std.ArrayList(u8) = .empty; + var spans: std.ArrayList(TagState.Span) = .empty; + try text.append(arena, ' '); + var cells: [4][2]u32 = undefined; + var ncells: usize = 0; + // SPC's cell, then its keys': `␣ f`. + if (pk.keys.len > 0) for (0..pk.keys.len + 1) |k| { + const name = if (k == 0) "␣" else bindings.keyName(pk.keys.slice()[k - 1]); + cells[ncells] = .{ @intCast(text.items.len), @intCast(text.items.len + name.len) }; + ncells += 1; + try text.appendSlice(arena, name); + try text.append(arena, ' '); + }; + try text.appendSlice(arena, pk.word.slice()); + try text.append(arena, ' '); + 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.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. + try text.append(arena, '\n'); + const prefix = try pardes.tagline.prefixIn(p, arena, pane); + const width = pardes.tagline.tagWidth(p, panes.paneIdOf(p, pane) orelse 0); + const lead = File.displayWidth(p.settings.tab, prefix) % @max(1, width); + var col: usize = 0; + for (cells[0..ncells]) |c| { + const at = lead + File.displayWidth(p.settings.tab, text.items[0..c[0]]); + while (col < at) : (col += 1) try text.append(arena, ' '); + try spans.append(arena, .{ .lo = @intCast(text.items.len), .hi = @intCast(text.items.len + "│".len), .style = .key }); + try text.appendSlice(arena, "│"); + col += 1; + } + const nspines: u8 = @intCast(spans.items.len); + try text.appendSlice(arena, if (ncells == 0) "" else " "); + var first = true; + inline for (comptime std.enums.values(Source)) |src| if (src != .list) { + if (src != pk.source) { + if (!first) try text.append(arena, ' '); + first = false; + try text.appendSlice(arena, words.get(src).word); + } + }; + for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); + return .{ .text = text.items, .spans = spans.items, .cells = nspines, .caret = caret }; +} + +/// The band's rows: as many as the list will show, eleven at most, from +/// what the source has (not what the query keeps: typing never moves the +/// 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; + return @intCast(@max(1, @min(Band.Cursor.shown, have))); +} + +/// The band's steps: the rows around the cursor. +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); + 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 }; + out[0] = .{ .rows = none }; + return out; + } + const w = s.rows.window(@intCast(@min(n, std.math.maxInt(u16)))); + 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) }; + } + out[0] = .{ .rows = rows }; + return out; +} + +// ---- the preview ---- + +/// The landing pane's body showing the current row's place (SPEC §5.3): +/// a file at its line, the query's matches lit for a Grep. Its own text is +/// set aside the first time, put back when the picker rolls up. A file an +/// 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; + pk.shown = s.rows.at; + if (count(p, pk) == 0) return; + var frame = std.heap.ArenaAllocator.init(p.gpa); + defer frame.deinit(); + const row = rowAt(p, pk, frame.allocator(), s.rows.at) catch return; + const place = switch (row.target) { + .place => |t| t, + else => return, + }; + const id = p.paneBySerial(pk.landing) orelse return; + const pane = p.panes[id].?; + const f = if (pane.file) |*file| file else return; + if (f.twin != 0) return; + if (!std.mem.eql(u8, f.path, place.path)) { + if (exec.kindOf(p.tape, place.path)) |kind| if (kind != .file) return; + const bytes = openText(p, place.path) orelse (p.tape.site(.preview, p.gpa, filesystem.read, .{ p, place.path }) catch return); + if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) { + p.gpa.free(bytes); + return; + } + const path = p.gpa.dupe(u8, place.path) catch { + p.gpa.free(bytes); + return; + }; + if (pk.aside == null and !pk.made) { + const history = File.History.create(p.gpa) catch { + p.gpa.free(bytes); + p.gpa.free(path); + return; + }; + pk.aside = pane.file; + pk.aside_row = pane.body.cur_row; + pk.aside_col = pane.body.cur_col; + pane.file = .{ .path = path, .content = bytes, .output = .{ .from = .preview }, .history = history }; + } else { + const pf = &pane.file.?; + p.gpa.free(pf.path); + pf.path = path; + File.setContent(p, pf, bytes); + pf.saved_revision = pf.revision; + pf.scroll = 0; + pf.scroll_row = 0; + pf.syntax_dirty = true; + pane.places.clear(p.gpa); + } + pane.hscroll = 0; + p.sync(); + } + const keyboard = p.view().active; + defer p.view().active = keyboard; + look.clearNavigationSelection(p, pane); + const pf = &pane.file.?; + const at = place.at; + if (at.line == 0 or at.line > File.nlines(p.gpa, pf)) { + pf.scroll = 0; + pf.scroll_row = 0; + pane.body.cur_row = 0; + pane.body.cur_col = 0; + return; + } + look.focusPaneLine(p, id, at, .center); + // A Grep's matches in the file lit, as its own pane would have them. + if (pk.source == .grep) pane.places.search(p, pane, .list, pk.id, std.mem.trim(u8, s.field.text(), " "), true, null, false); +} + +/// The text of `path` as an open pane has it, copied: no read for it. +fn openText(p: *Pardes, path: []const u8) ?[]u8 { + for (p.panes) |slot| { + const tp = slot orelse continue; + const tf = tp.file orelse continue; + if (tf.output != null or !std.mem.eql(u8, tf.path, path)) continue; + return p.gpa.dupe(u8, tf.content) catch null; + } + return null; +} + +/// 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). +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; + } + const own = pk.aside orelse return; + pk.aside = null; + File.deinit(p, pane, &pane.file.?); + pane.file = own; + pane.body.cur_row = pk.aside_row; + pane.body.cur_col = pk.aside_col; + pane.body.vsel.active = false; + p.sync(); +} + +/// The live picker rolled up (Esc, or the tag put to rest by anything): +/// its query and cursor kept for its list word and Resume, every body as +/// it was (TagState.toRest). +pub fn rollUp(p: *Pardes) void { + const pk = live(p) orelse return; + const s = &p.view().tag_state; + pk.query.set(s.field.text()); + pk.at = s.rows.at; + pk.unrolled = false; + if (p.stream.target == pk.id) _ = p.stream.stop(); + restore(p, pk); +} + +/// Every picker freed, a live one's landing pane put back first (the +/// session's end). +pub fn deinitAll(p: *Pardes) void { + for (&p.picks) |*slot| if (slot.*) |pk| { + if (pk.unrolled) { + pk.unrolled = false; + if (pk.aside) |own| if (p.paneBySerial(pk.landing)) |id| { + const pane = p.panes[id].?; + File.deinit(p, pane, &pane.file.?); + pane.file = own; + pk.aside = null; + }; + } + pk.deinit(p.gpa); + p.gpa.destroy(pk); + slot.* = null; + }; +} + +// ---- 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). +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))); + if (s.rows.key(k, n)) return; + if (s.field.key(k)) filter(p, pk); +} + +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; + const dir = base_buf[0..try tmp.dir.realPath(std.testing.io, &base_buf)]; + return std.Io.Dir.path.join(p.gpa, &.{ dir, name }); +} + +fn tagOf(p: *Pardes, arena: std.mem.Allocator, id: usize) ![]const u8 { + return pardes.tagline.tagText(p, arena, p.panes[id].?); +} + +fn paneCount(p: *const Pardes) usize { + var n: usize = 0; + for (p.panes) |slot| n += @intFromBool(slot != null); + return n; +} + +fn settle(p: *Pardes) void { + var n: usize = 0; + while (n < 50 and (busy(p) or p.stream.busyFor(live(p).?.id))) : (n += 1) { + Stream.step(p); + p.update(.tick); + } +} + +test "a picker from a held pane unrolls in a reader made for it: its rows in the band, the body the current row's file, Esc takes the reader away" { + 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, "pick_state.zig", "//! The state a picker keeps\nconst x = 2;\n"); + defer p.gpa.free(b_path); + const held = try p.setTestFile("held\n"); + _ = held; + const asker = p.view().active; + // Held: Keep. + Trail.keep(p.panes[asker].?); + const dir = std.Io.Dir.path.dirname(a_path).?; + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ dir, "held.txt" }); + p.stream.engine = .walk; + const npanes = paneCount(p); + open(p, asker, .find, "", "", ""); + const pk = live(p).?; + // A reader made for it, under the asker; its tag is the picker's. + try std.testing.expectEqual(npanes + 1, paneCount(p)); + try std.testing.expect(pk.made); + const reader = p.paneBySerial(pk.landing).?; + try std.testing.expectEqual(@as(?usize, reader), TagState.hostOf(p)); + try std.testing.expectEqual(asker, p.view().active); + settle(p); + try std.testing.expectEqual(@as(usize, 2), count(p, pk)); + for ("pick") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, pk)); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const tag = try tagOf(p, a, reader); + try std.testing.expect(std.mem.indexOf(u8, tag, " ␣ f Find pick 1/1\n") != null); + try std.testing.expect(std.mem.indexOf(u8, tag, "│ Grep Recent Panes Symbols All Jumplist") != null); + // The body previews the row's file, after a frame of its own. + p.update(.tick); + try std.testing.expectEqualStrings(b_path, p.panes[reader].?.file.?.path); + _ = try p.render(a); + const r = p.view().rects[reader]; + try std.testing.expectEqualStrings("^", p.surface.at(r.x, r.y).grapheme()); + try std.testing.expect(p.panes[reader].?.band_rows >= 1); + // Esc: the reader made for it goes, the asker as it was. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expect(live(p) == null); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expectEqualStrings("pick", pk.query.slice()); +} + +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(); + 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; + try std.testing.expect(Trail.reading(p, p.panes[id].?)); + const npanes = paneCount(p); + open(p, id, .list, "Refs", "", "beta.zig:2:1 two\nalpha.zig:1:7 alpha\n"); + const pk = live(p).?; + try std.testing.expect(!pk.made); + try std.testing.expectEqual(npanes, paneCount(p)); + try std.testing.expectEqual(@as(?usize, id), TagState.hostOf(p)); + settle(p); + try std.testing.expectEqual(@as(usize, 2), count(p, pk)); + try std.testing.expectEqualStrings(b_path, p.panes[id].?.file.?.path); + try std.testing.expectEqual(@as(i32, 1), p.panes[id].?.body.cur_row); + try std.testing.expect(pk.aside != null); + // Down: the next row's file, on the frame after. + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expectEqualStrings(b_path, p.panes[id].?.file.?.path); + p.update(.tick); + try std.testing.expectEqualStrings(a_path, p.panes[id].?.file.?.path); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(pk.aside == null); + try std.testing.expectEqualStrings(a_path, p.panes[id].?.file.?.path); + try std.testing.expect(p.panes[id].?.file.?.output == null); + try std.testing.expectEqual(npanes, paneCount(p)); +} diff --git a/core/Query.zig b/core/Query.zig index feebb6cd..4fbd07e0 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -713,7 +713,6 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz q.found.clearRetainingCapacity(); q.found_bytes.clearRetainingCapacity(); const dir = std.Io.Dir.path.dirname(rp.file.?.path) orelse "/"; - const gpa = p.gpa; switch (source) { // The files under the directory, listed once on the disk engine's // worker (rg --files, else the walk); each keystroke scores them. @@ -721,6 +720,19 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz Stream.query(p, rp.serial, .files, "", dir, .now) catch |err| return p.reportError(.at(id), "search", err); if (p.fs.serving) Stream.step(p); }, + .recent, .panes, .symbols => gather(p, &q.names, source, dir, rp, owner_id), + else => {}, + } +} + +/// The names a Recent, Panes or Symbols query scores, into `names`: the +/// files opened lately, the panes but `self` (then the ones this session +/// closed), the declarations of pane `owner_id`'s text (asked of the +/// worker when it has none yet: declsArrived gathers them). Rows relative +/// to `dir`. A query pane's, and a picker's (Pick.zig). +pub fn gather(p: *Pardes, names: *Names, source: Source, dir: []const u8, self: ?*const Pane, owner_id: ?usize) void { + const gpa = p.gpa; + switch (source) { .recent => { var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); @@ -729,19 +741,19 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz if (ln.len == 0) continue; const rel = relative(ln, dir); const path_end = std.mem.indexOfAny(u8, rel, ": ") orelse rel.len; - q.names.add(gpa, rel, 0, path_end, Names.baseOf(rel[0..path_end]), 0) catch break; + names.add(gpa, rel, 0, path_end, Names.baseOf(rel[0..path_end]), 0) catch break; } }, .panes => { for (p.panes) |slot| { const other = slot orelse continue; - if (other == rp or other.query != null) continue; + if (other == self or other.query != null) continue; var buf: [4096 + 32]u8 = undefined; const what: []const u8 = if (other.file) |f| f.path else if (other.command) |c| c else other.cwdSlice(); const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}", .{ other.serial, relative(what, dir) }) catch continue; const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; const ln = row[0 .. std.mem.indexOfAny(u8, row, "\r\n") orelse row.len]; - q.names.add(gpa, ln, key_at, ln.len -| key_at, Names.baseOf(ln[@min(key_at, ln.len)..]), other.serial) catch break; + names.add(gpa, ln, key_at, ln.len -| key_at, Names.baseOf(ln[@min(key_at, ln.len)..]), other.serial) catch break; } // Then the panes this session closed, the latest first, each // `(closed)` and dimmer (makeNameRows): Enter brings one back @@ -752,7 +764,7 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, relative(c.name, dir) }) catch continue; const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; const key_len = row.len - closed_word.len - key_at; - q.names.add(gpa, row, key_at, key_len, Names.baseOf(row[key_at..][0..key_len]), c.serial) catch break; + names.add(gpa, row, key_at, key_len, Names.baseOf(row[key_at..][0..key_len]), c.serial) catch break; } }, .symbols => { @@ -769,7 +781,7 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz const first = std.mem.trim(u8, header[0 .. std.mem.indexOfScalar(u8, header, '\n') orelse header.len], " \t\r"); var buf: [256 + 16]u8 = undefined; const row = std.fmt.bufPrint(&buf, "{d: >6} {s}", .{ d.start_line + 1, first[0..@min(first.len, 240)] }) catch continue; - q.names.add(gpa, row, 8, row.len - 8, 0, @intCast(d.start_line + 1)) catch break; + names.add(gpa, row, 8, row.len - 8, 0, @intCast(d.start_line + 1)) catch break; } }, else => {}, @@ -1253,10 +1265,11 @@ fn countGrepCurrent(q: *Query) void { if (c < q.items.items.len and q.items.items[c].kind == .hit) q.k = q.items.items[c].hit + 1; } -/// The disk engine's rows for query pane `q` (Stream.receive): a Grep's -/// lines (`path:line:col-end code`, relative to the pane's directory) kept -/// as found, a listing's names added to what Find scores. -pub fn take(p: *Pardes, q: *Query, kind: Stream.Kind, rows: []const u8) void { +/// The disk engine's rows for query pane `q` (Stream.receive), or for a +/// picker (Pick.zig: the same fields): a Grep's lines (`path:line:col-end +/// code`, relative to the pane's directory) kept as found, a listing's +/// names added to what Find scores. +pub fn take(p: *Pardes, q: anytype, kind: Stream.Kind, rows: []const u8) void { var lines = std.mem.splitScalar(u8, rows, '\n'); while (lines.next()) |ln| { if (ln.len == 0) continue; diff --git a/core/Stream.zig b/core/Stream.zig index 2f700cb4..49329361 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -713,6 +713,7 @@ pub fn receive(p: *Pardes, b: *const Batch) void { } } p.needs_frame = true; + if (panes.Pick.take(p, s.target, s.kind, b.rows)) return; const id = p.paneBySerial(s.target) orelse return; const q = p.panes[id].?.query orelse return; q.capped = s.state == .capped; @@ -735,6 +736,7 @@ fn receiveParse(p: *Pardes, b: *const Batch, serial: u32, revision: u32) void { if (p.tree_sitter_gpa.dupe(syntax.ContextDeclaration, b.decls)) |copy| { f.context.replace(p.tree_sitter_gpa, copy, revision); panes.Query.declsArrived(p, serial); + panes.Pick.declsArrived(p, serial); } else |_| {} }; p.needs_frame = true; diff --git a/core/TagState.zig b/core/TagState.zig index 56c715af..42d13726 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -27,8 +27,9 @@ const Names = @import("Names.zig"); /// The tag's states besides a root's level (which `kind` rest with a root /// current is): `help`, a field over every leaf (SPEC §4.5); `describe`, -/// a sequence replayed in the tree (item 5). -pub const Kind = enum(u8) { rest, help, describe }; +/// a sequence replayed in the tree (item 5); `pick`, a picker (Pick.zig, +/// SPEC §5). +pub const Kind = enum(u8) { rest, help, describe, pick }; pub const replica = .{ .len = .{ .{ "scope", "scope_n" }, .{ "hits", "nhits" } } }; @@ -52,6 +53,8 @@ nhits: u16 = 0, ntotal: u16 = 0, /// The rows' cursor (Band.Cursor). rows: Band.Cursor = .{}, +/// The picker unrolled in the host's tag, by its serial (Pick.zig). +pick: u32 = 0, /// The root the last key left current, whose delay `due_ns` counts. root: ?Root = null, /// When its level shows, on the session's clock: `asked` from its key @@ -147,6 +150,8 @@ pub fn follow(p: *Pardes) void { /// Back to rest in the live view: no root waits, no typing state, nothing /// unrolled (a word run from the band, Esc in a typing state). pub fn toRest(p: *Pardes) void { + // A picker rolls up first: every body it previewed in as it was. + @import("Pick.zig").rollUp(p); const v = p.view(); v.prefix = .none; v.window_pick = null; @@ -206,6 +211,10 @@ pub fn lastUsed(p: *const Pardes, v: *const View, buf: []u8) ?[]const u8 { pub fn point(p: *Pardes, index: u16) void { const v = p.view(); const s = &v.tag_state; + if (s.kind == .pick) { + s.rows.at = index; + return; + } const cur = current(p, v) orelse return; var children: [bindings.max_children]bindings.Child = undefined; const level = bindings.level(p, cur.root, cur.path, &children); @@ -378,6 +387,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { return h; }, .describe => return null, + .pick => return @import("Pick.zig").head(p, arena, pane), } const cur = current(p, v) orelse return null; try b.put(" "); diff --git a/core/draw.zig b/core/draw.zig index bc606f02..5282b1c9 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -861,7 +861,8 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R style.fg = .{ .rgb = chrome.box }; style.bold = true; } - if (part.row.dim) style.dim = true; + if (b < part.row.pre) style.fg = .{ .rgb = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg }; + if (part.row.dim or (b >= part.row.pre and b < part.row.pre + part.row.faint)) style.dim = true; if (part.row.under) style.ul = .single; for (part.row.marks) |m| if (m == b) { style.bg = .{ .rgb = chrome.search_bg }; diff --git a/core/keys.zig b/core/keys.zig index 6f43e166..cd7b89b1 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -553,6 +553,7 @@ fn tagStateKey(p: *Pardes, key: Key) void { switch (s.kind) { .rest => unreachable, .describe => return TagState.toRest(p), + .pick => return @import("Pick.zig").key(p, key), .help => { const plain = !key.ctrl and !key.alt; if (plain and key.cp == Key.enter) { diff --git a/core/panes.zig b/core/panes.zig index 115323c4..6fd5c679 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1140,6 +1140,7 @@ pub const Places = @import("Places.zig"); pub const Tables = @import("Tables.zig"); pub const Query = @import("Query.zig"); +pub const Pick = @import("Pick.zig"); pub const Dir = @import("Dir.zig"); diff --git a/core/root.zig b/core/root.zig index 36d940b2..a4adf7b1 100644 --- a/core/root.zig +++ b/core/root.zig @@ -92,6 +92,7 @@ pub const keys = @import("keys.zig"); pub const bindings = @import("bindings.zig"); pub const TagState = @import("TagState.zig"); pub const Band = @import("Band.zig"); +pub const Pick = @import("Pick.zig"); pub const Addon = @import("Addon.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -414,6 +415,7 @@ test { _ = @import("bindings.zig"); _ = @import("TagState.zig"); _ = @import("Band.zig"); + _ = @import("Pick.zig"); _ = @import("Field.zig"); _ = @import("Effect.zig"); _ = @import("tape.zig"); -- cgit v1.3