From 89f51118eb241fdb5673e91eb09e5abe5c6a0bcb Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 03:03:28 -0300 Subject: Pick, Query, Stream: Find and Grep search the work tree, the nearest directory at or above the asking pane's that holds a `.jj` or a `.git`, or the pane's directory outside one, and a leading directory narrows a Grep (the comparison video's finding, VIDEO2/STORY.md §3.1 and §9: from src/torque in V8 `SPC f f` listed its 67 files where Helix, LazyVim and Doom list 18,441, and `SPC f g` found 11 hits in src/torque alone; SPEC §5.1's "scoped to a directory if the query starts with one" found nothing; the coordinator's W8, the user asked, the default yes); THE CAUSE: Pick.open set every source's directory to the asking pane's (Pardes.paneDir), and Pick.filter sent a Grep's whole query to the disk engine as its pattern; THE FIX: Pick.workTree (Find and Grep only: Recent, Panes and Symbols as they were), a stat of `.jj` and `.git` per directory up from the pane's through the tape's kind site, once per picker opened; the rows' paths are from the root (Find's `src/torque/torque-parser.cc`), so Names' subsequence match over the whole path makes `torque/pars` narrow Find with no more code; Pick.grepScope: a query whose first word, then a blank, is a directory under the root (or absolute) searches that directory for the rest (`src/torque "UpperCamelCase"`); its rows' paths stay from the root (Query.take's `under`, the narrowed directory before each path; the query pane passes none), and the preview lights the pattern alone; no directory added to the head (the coordinator's rule: none was shown); RUN (TEST FIRST): Pick.zig "Find and Grep search the work tree (W8) ..." makes a tree with `.git` at its root and opens Find from src/deep/x.txt: its directory the root (on W7's tree src/deep), three rows, `sub/name` one, `sub/name.txt`; Grep `needle` two hits, `sub needle` one, `sub/name.txt:1 needle one`, its place the root's sub/name.txt; from `/held.txt` (no repository above /) the directory `/`; the tests whose Find or Grep pickers list a temporary directory make it a work tree of its own (Pick.testFile, and four tests in behaviour/addons.zig, addons_pdf.zig and other_files.zig): under the repository's .zig-cache it is in the pardes tree; MEASURED on a reflink copy of VIDEO2's V8 (18,441 files) with this change's Debug gui at 26 px, from src/torque/torque.cc, after the last key (V4's probe in brackets, the installed build): SPC f f 18,414 rows, the first at 0.10-0.11 s and all at 0.14-0.16 s [0.02 s, 67 files: src/torque alone]; `torque/pars` 7 rows at 0.09 s, torque-parser.cc among them [0 rows]; Grep `"UpperCamelCase"` 11 hits from the root at 0.26 s, its typing pause included [0.11 s, 11 hits, src/torque alone]; `src/torque "UpperCamelCase"` the same 11 at 0.15 s [nothing]; this harness on the installed build: 67 files at 0.05 s, the grep 0.11-0.13 s; keys typed through the frontend (the S kit's pty): a key written to 9P's kbdin runs the listing and the search on the loop for its write's answer (Pick.start and filter's `p.fs.serving` step), so a script's `SPC f g` over V8 holds the session for seconds (seen: the 9P client timed out), as before this change over a smaller directory; the find golden (test/snapshots/find.golden) re-recorded: from sub/ZQMARK-beta.txt `SPC f f` lists the root's five files, a band row more; replica.zig's may_wait lists `Find *` and `Grep *` stopping at `kind` (the root asked of the session's disk, as their listing and search are); gates: core-test 1129/1302 (173 skipped: +1), shell-test 1350/1353 (3 skipped: +1), pane-test 183/184, behaviour-test 285/285 and --replay 285/285, process-test 0 of 147 failed (3 skipped; find re-recorded), term-test 17/17, kitty-test 45/45, the gui unit-test 1958/1962 (4 skipped; its perf check's BaselineHarnessMismatch, every base's) on this change, W7 under it; no dump change; shots .scratch/walkui/W78/png v1-v3; no frame or perf gate (the user's rule of 2026-11-26); load 3.7 to 15.9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Jump.zig | 2 + core/Pick.zig | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- core/Query.zig | 9 +++-- core/Stream.zig | 2 +- core/replica.zig | 2 + 5 files changed, 121 insertions(+), 11 deletions(-) (limited to 'core') diff --git a/core/Jump.zig b/core/Jump.zig index 3632a039..143aa9b5 100644 --- a/core/Jump.zig +++ b/core/Jump.zig @@ -472,6 +472,8 @@ test "Ctrl-o pops the walk one word at a time (UX L'1): after SPC f f words, Ent defer p.deinit(); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); + // A work tree of its own: Find searches it, not the one the test runs in. + try tmp.dir.createDir(std.testing.io, ".git", .default_dir); var text: [60 * 16]u8 = undefined; var w: std.Io.Writer = .fixed(&text); for (1..61) |n| try w.print("{s} {d}\n", .{ if (n % 12 == 4) "in_word" else "line", n }); diff --git a/core/Pick.zig b/core/Pick.zig index f13278af..cfef2baf 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -226,7 +226,8 @@ pub fn open(p: *Pardes, from: usize, source: Source, word: []const u8, query: [] 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)); + // Find and Grep from the work tree's root (W8), as other finders search. + pk.dir.set(if (source == .find or source == .grep) workTree(p, Pardes.paneDir(asker)) else Pardes.paneDir(asker)); pk.rows = p.gpa.dupe(u8, rows) catch |err| return p.reportError(.at(from), "pick", err); hostIn(p, pk, from) catch |err| return p.reportError(.at(from), "pick", err); if (p.paneBySerial(pk.landing)) |host| pk.cap = p.view().rects[host].h * 2 / 5; @@ -237,6 +238,35 @@ pub fn open(p: *Pardes, from: usize, source: Source, word: []const u8, query: [] if (source == .themes and query.len == 0 and p.custom_theme == null) p.view().tag_state.rows.at = @intCast(p.settings.theme); } +/// The work tree `dir` is in: the nearest directory at or above it that +/// holds a `.jj` or a `.git` (a git worktree's is a file); `dir` itself +/// when none does. Find and Grep search from it, their rows' paths +/// relative to it, wherever in the tree the asking pane is. +fn workTree(p: *Pardes, dir: []const u8) []const u8 { + var d = dir; + while (true) { + var buf: [4096]u8 = undefined; + for ([_][]const u8{ ".jj", ".git" }) |name| { + const marker = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, d, "/"), name }) catch return dir; + if (exec.kindOf(p.tape, marker) != null) return d; + } + d = std.Io.Dir.path.dirname(d) orelse return dir; + } +} + +/// Where a Grep's query `q` searches and for what (SPEC §5.1): a leading +/// directory, the root's or absolute, then a blank, narrows it to that +/// directory (`src/torque Macro`), the rest the pattern; else all of it +/// from the root. The directory joined in `buf`. +fn grepScope(p: *Pardes, pk: *const Pick, q: []const u8, buf: *[4096]u8) struct { dir: []const u8, pat: []const u8 } { + const root = pk.dir.slice(); + const sp = std.mem.indexOfScalar(u8, q, ' ') orelse return .{ .dir = root, .pat = q }; + const lead = q[0..sp]; + const dir = if (lead[0] == '/') lead else std.fmt.bufPrint(buf, "{s}/{s}", .{ std.mem.trimEnd(u8, root, "/"), lead }) catch return .{ .dir = root, .pat = q }; + if (exec.kindOf(p.tape, dir) != .directory) return .{ .dir = root, .pat = q }; + return .{ .dir = dir, .pat = std.mem.trimStart(u8, q[sp + 1 ..], " ") }; +} + /// A new picker in the ring, the oldest kept one dropped for it. fn make(p: *Pardes, source: Source) !*Pick { var slot: ?usize = null; @@ -456,8 +486,10 @@ pub fn filter(p: *Pardes, pk: *Pick) void { 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); + var buf: [4096]u8 = undefined; + const at = grepScope(p, pk, q, &buf); + if (at.pat.len > 0) { + Stream.query(p, pk.id, .grep, at.pat, at.dir, 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; @@ -501,7 +533,12 @@ fn allPanes(p: *const Pardes, pk: *const Pick, out: *[pardes.MAX_PANES]usize) [] /// 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 Grep narrowed to a directory (grepScope): its rows' paths from the + // root, as an unnarrowed one's are. + var buf: [4096]u8 = undefined; + const searched = p.stream.dir.items; + const under = if (kind == .grep and !std.mem.eql(u8, searched, pk.dir.slice())) std.fmt.bufPrint(&buf, "{s}/", .{relative(searched, pk.dir.slice())}) catch "" else ""; + Query.take(p, pk, kind, rows, under); // A listing's names, scored as they come. if (kind == .files) pk.names.rescore(p.gpa); return true; @@ -626,7 +663,8 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) std.me 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 }); + // (Under an absolute leading directory, grepScope's, an absolute path.) + const path = if (name.len > 0 and name[0] == '/') name else 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; @@ -987,7 +1025,10 @@ fn preview(p: *Pardes, pk: *Pick) void { } look.focusPaneLine(p, id, at, .third); // 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); + if (pk.source == .grep) { + var buf: [4096]u8 = undefined; + pane.places.search(p, pane, .list, pk.id, grepScope(p, pk, std.mem.trim(u8, s.field.text(), " "), &buf).pat, true, null, false); + } } /// Landing pane `id`'s text `bytes` (taken) as `path`: its own text set @@ -2067,6 +2108,9 @@ pub fn execWord(p: *Pardes, txt: []const u8) bool { } fn testFile(p: *Pardes, tmp: *std.testing.TmpDir, name: []const u8, text: []const u8) ![]u8 { + // The directory a work tree of its own, as a project is: Find and Grep + // search it, not the tree the test runs in. + tmp.dir.createDir(std.testing.io, ".git", .default_dir) catch |err| if (err != error.PathAlreadyExists) return err; 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)]; @@ -3351,3 +3395,64 @@ test "Ctrl-r in a picker unrolls its source's earlier queries (UX L2's last line p.update(.{ .key = .{ .cp = Key.escape } }); p.update(.{ .key = .{ .cp = Key.escape } }); } + +test "Find and Grep search the work tree (W8): from a pane deep in a repo Find lists the files from its root and `sub/name` narrows it, a leading directory narrows Grep, and outside a repo they search the pane's directory" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + p.settings.jumps = .walk; + p.stream.engine = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + for ([_][]const u8{ "src/deep", "sub", "other" }) |d| try tmp.dir.createDirPath(std.testing.io, d); + const deep = try testFile(p, &tmp, "src/deep/x.txt", "deep\n"); + defer p.gpa.free(deep); + for ([_][2][]const u8{ .{ "sub/name.txt", "needle one\n" }, .{ "other/name.txt", "needle two\n" } }) |f| p.gpa.free(try testFile(p, &tmp, f[0], f[1])); + const root = std.Io.Dir.path.dirname(std.Io.Dir.path.dirname(std.Io.Dir.path.dirname(deep).?).?).?; + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try p.gpa.dupe(u8, deep); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + // Find from src/deep: the root's files, by their paths from it. + open(p, asker, .find, "", "", ""); + var pk = live(p).?; + try std.testing.expectEqualStrings(root, pk.dir.slice()); + settle(p); + try std.testing.expectEqual(@as(usize, 3), count(p, pk)); + for ("sub/name") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, pk)); + try std.testing.expectEqualStrings("sub/name.txt", (try rowAt(p, pk, a, 0)).text); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Grep: the whole tree, then `sub needle` in sub/ alone, its row's + // path from the root, landing on the file there. + open(p, asker, .grep, "", "", ""); + pk = live(p).?; + for ("needle") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + // (Its typing paused: the search starts.) + p.clock.now_ns = p.stream.due_ns.?; + settle(p); + try std.testing.expectEqual(@as(usize, 2), count(p, pk)); + p.update(.{ .key = .{ .cp = Key.escape } }); + open(p, asker, .grep, "", "", ""); + pk = live(p).?; + for ("sub needle") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + // (Its typing paused: the search starts.) + p.clock.now_ns = p.stream.due_ns.?; + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, pk)); + const row = try rowAt(p, pk, a, 0); + try std.testing.expect(std.mem.startsWith(u8, row.text, "sub/name.txt:1 needle one")); + try std.testing.expectEqualStrings(try std.Io.Dir.path.join(a, &.{ root, "sub/name.txt" }), row.target.place.path); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Outside a repo (`/`, the test text's directory, holds no .jj nor + // .git): the pane's directory, as before. + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try p.gpa.dupe(u8, "/held.txt"); + open(p, asker, .find, "", "", ""); + try std.testing.expectEqualStrings("/", live(p).?.dir.slice()); + p.update(.{ .key = .{ .cp = Key.escape } }); +} diff --git a/core/Query.zig b/core/Query.zig index 2b2abebb..fff6065c 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -1269,7 +1269,7 @@ fn countGrepCurrent(q: *Query) void { /// 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 { +pub fn take(p: *Pardes, q: anytype, kind: Stream.Kind, rows: []const u8, under: []const u8) void { var lines = std.mem.splitScalar(u8, rows, '\n'); while (lines.next()) |ln| { if (ln.len == 0) continue; @@ -1286,13 +1286,14 @@ pub fn take(p: *Pardes, q: anytype, kind: Stream.Kind, rows: []const u8) void { const name = if (ln[0] == '"') look.unquoted(&namebuf, t.path) else t.path; const found: Found = .{ .path = std.math.cast(u32, q.found_bytes.items.len) orelse return, - .path_len = std.math.cast(u16, name.len) orelse continue, + .path_len = std.math.cast(u16, under.len + name.len) orelse continue, .line = std.math.cast(u32, t.at.line) orelse continue, .col = std.math.cast(u32, t.at.col -| 1) orelse continue, .end = std.math.cast(u32, if (t.at.end_col > 0) t.at.end_col else t.at.col) orelse continue, - .code = std.math.cast(u32, q.found_bytes.items.len + name.len) orelse return, + .code = std.math.cast(u32, q.found_bytes.items.len + under.len + name.len) orelse return, .code_len = std.math.cast(u32, code.len) orelse continue, }; + q.found_bytes.appendSlice(p.gpa, under) catch return; q.found_bytes.appendSlice(p.gpa, name) catch return; q.found_bytes.appendSlice(p.gpa, code) catch return; q.found.append(p.gpa, found) catch return; @@ -2811,7 +2812,7 @@ test "Grep: a row whose numbers a Found cannot hold (a column past u32) is passe defer p.deinit(); var q: Query = .{ .owner = 0 }; defer q.deinit(p.gpa); - take(p, &q, .grep, "a:5000000000:b:3:1-4 hit\nok.txt:2:3-5 hit\nb:7:4294967297 hit\n"); + take(p, &q, .grep, "a:5000000000:b:3:1-4 hit\nok.txt:2:3-5 hit\nb:7:4294967297 hit\n", ""); // Taken or passed over, each: never cast (main: Query.zig:1267). try std.testing.expect(q.found.items.len <= 3); } diff --git a/core/Stream.zig b/core/Stream.zig index e9e7793b..93be5c1f 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -717,7 +717,7 @@ pub fn receive(p: *Pardes, b: *const Batch) void { const id = p.paneBySerial(s.target) orelse return; const q = p.panes[id].?.query orelse return; q.capped = s.state == .capped; - panes.Query.take(p, q, s.kind, b.rows); + panes.Query.take(p, q, s.kind, b.rows, ""); } /// A parse's answer: its task joined; the declarations kept as the pane's diff --git a/core/replica.zig b/core/replica.zig index 97cbaeee..d07ce9d3 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7351,6 +7351,8 @@ const may_wait = [_]Wait{ .{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" }, // A program run, a shell opened or closed: the session's processes. .{ .what = "Exec *", .on = .any, .stop = "kind", .why = "a program run: its name asked of the disk first" }, + .{ .what = "Find *", .on = .any, .stop = "kind", .why = "the work tree it lists from, a `.jj` or `.git` above the pane's directory asked of the session's disk (Pick.workTree), as the listing itself is the session's" }, + .{ .what = "Grep *", .on = .any, .stop = "kind", .why = "the work tree it searches, a `.jj` or `.git` above the pane's directory asked of the session's disk (Pick.workTree), as the search itself is the session's" }, .{ .what = "Exec *", .on = .any, .stop = "tty_taken", .why = "a program run in a shell: whether one has it, the session's" }, .{ .what = "Term", .on = .any, .stop = "kind", .why = "a shell opened: its name asked of the disk first" }, .{ .what = "A-n", .on = .any, .stop = "open a terminal", .why = "a shell opened" }, -- cgit v1.3