diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Jump.zig | 2 | ||||
| -rw-r--r-- | core/Pick.zig | 117 | ||||
| -rw-r--r-- | core/Query.zig | 9 | ||||
| -rw-r--r-- | core/Stream.zig | 2 | ||||
| -rw-r--r-- | core/replica.zig | 2 |
5 files changed, 121 insertions, 11 deletions
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" }, |
