diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 11:57:35 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-24 12:21:18 -0300 |
| commit | 8ac6db5662dd2db0f082e1651835f1db36e235f8 (patch) | |
| tree | ae8a8e928ca774b4347e4d19a8bf083c397e909b /src/Query.zig | |
| parent | 51903dd947f90381d1a908c488a019289a1c8413 (diff) | |
| download | pardes-8ac6db5662dd2db0f082e1651835f1db36e235f8.tar.gz pardes-8ac6db5662dd2db0f082e1651835f1db36e235f8.zip | |
Search converge 4/6: the disk engine, Grep and Find's listing on a worker (rg when there, else a sorted walk), generation-numbered batches, a debounce, a 2000-line cap and More, and the declarations parse off the loop
Diffstat (limited to 'src/Query.zig')
| -rw-r--r-- | src/Query.zig | 190 |
1 files changed, 111 insertions, 79 deletions
diff --git a/src/Query.zig b/src/Query.zig index fbce95b2..48378a18 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -46,6 +46,7 @@ const colors = @import("colors.zig"); const memory = @import("memory.zig"); const Names = @import("Names.zig"); const Places = @import("Places.zig"); +const Stream = @import("Stream.zig"); const Surface = @import("surface.zig").Surface; const Pardes = pardes.Pardes; const Pane = panes.Pane; @@ -152,16 +153,17 @@ window: struct { lo: usize = 0, hi: usize = 0 } = .{}, /// made every third, the frame's time going to the finding. waited: u8 = 0, names: Names = .{}, -/// A Grep's lines, found a slice a frame (Query.grepSlice). +/// A Grep's lines, as the disk engine sends them (Stream.zig, `take`). found: std.ArrayList(Found) = .empty, found_bytes: std.ArrayList(u8) = .empty, -grep_next: usize = 0, grep_gen: u32 = 0, /// The language server's rows for Refs, Def, Impl, Diagnostics, as it gave /// them (`path:line:col text`, gpa), filtered by what follows the word. lsp_rows: []u8 = &.{}, /// The +Preview under this pane, by serial; 0, none. preview: u32 = 0, +/// Its disk search waits at its cap: the tag says More. +capped: bool = false, /// A reused pane's frozen rows when a new query began in it: Esc brings /// them back. saved: ?Saved = null, @@ -387,7 +389,7 @@ pub fn busy(p: *const Pardes) bool { const pane = slot orelse continue; const q = pane.query orelse continue; if (q.frozen) continue; - if (q.names.busy() or (q.source == .grep and q.grep_next < q.names.cands.items.len) or q.made_rev != q.seen_rev) return true; + if (q.names.busy() or p.stream.busyFor(pane.serial) or q.made_rev != q.seen_rev) return true; } return false; } @@ -482,32 +484,29 @@ fn produce(p: *Pardes, id: usize, tick: bool) void { makeAllRows(p, id) catch |err| p.reportError(id, "search", err); }, .find, .recent, .panes, .symbols => { - if (tick) q.names.walkSlice(p.gpa); if (changed) q.names.typed(p.gpa, a.pat) else if (q.names.busy()) q.names.rescore(p.gpa); if (q.names.gen == q.made_gen and !changed) return; q.made_gen = q.names.gen; makeNameRows(p, id, a.pat) catch |err| p.reportError(id, "search", err); }, .grep => { + const dir = std.fs.path.dirname(f.path) orelse "/"; if (changed) { q.found.clearRetainingCapacity(); q.found_bytes.clearRetainingCapacity(); - q.grep_next = 0; q.grep_gen +%= 1; + q.current = null; + // On the disk engine's worker, once the typing pauses. + if (a.pat.len > 0) Stream.query(p, rp.serial, .grep, a.pat, dir, .typed) else if (p.stream.target == rp.serial) { + _ = p.stream.stop(); + } + feedGrepPlaces(p, q, rp.serial, a.pat); } - if (a.pat.len == 0) { - if (changed) makeGrepRows(p, id) catch {}; - return; - } - if (tick or changed) { - q.names.walkSlice(p.gpa); - grepSlice(p, q, rp, a.pat); - } - const gen: u64 = (@as(u64, q.grep_gen) << 32) | q.found.items.len; - if (gen == q.made_gen and !changed and !(q.grep_next >= q.names.cands.items.len and q.more)) return; + const searching = p.stream.target == rp.serial and p.stream.state != .done and p.stream.state != .stopped and p.stream.state != .failed and p.stream.state != .idle; + const gen: u64 = (@as(u64, q.grep_gen) << 32) | (q.found.items.len << 1) | @intFromBool(searching); + if (gen == q.made_gen and !changed) return; q.made_gen = gen; - makeGrepRows(p, id) catch |err| p.reportError(id, "search", err); - if (changed or q.current == null) feedGrepPlaces(p, q, rp.serial, a.pat); + makeGrepRows(p, id, false) catch |err| p.reportError(id, "search", err); }, .refs, .defs, .impls, .diagnostics => { if (changed and q.lsp_rows.len == 0) { @@ -544,11 +543,12 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz q.names.reset(); q.found.clearRetainingCapacity(); q.found_bytes.clearRetainingCapacity(); - q.grep_next = 0; const dir = std.fs.path.dirname(rp.file.?.path) orelse "/"; const gpa = p.gpa; switch (source) { - .find, .grep => q.names.startWalk(gpa, dir) catch |err| p.reportError(id, "search", err), + // The files under the directory, listed once on the disk engine's + // worker (rg --files, else the walk); each keystroke scores them. + .find => Stream.query(p, rp.serial, .files, "", dir, .now), .recent => { var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); @@ -575,7 +575,11 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz const oid = owner_id orelse return; const owner = p.panes[oid].?; const f = if (owner.file) |*file| file else return; - const decls = ownerDecls(p, f, true) orelse return; + const decls = ownerDecls(p, f, false) orelse { + // Parsed on the worker; listed when it is done (declsArrived). + Stream.parseFor(p, oid); + return; + }; for (decls) |d| { const header = f.content[d.start_byte..@min(d.header_end_byte, f.content.len)]; const first = std.mem.trim(u8, header[0 .. std.mem.indexOfScalar(u8, header, '\n') orelse header.len], " \t\r"); @@ -821,6 +825,8 @@ fn makeTextRows(p: *Pardes, id: usize, oid: usize, full: bool) !void { defer out.deinit(p.gpa); var rows: std.ArrayList(locations.Row) = .empty; var budget: usize = if (full) max_rows else live_rows; + // Grouped once a parse of the text exists: asked of the worker now. + if (ownerDecls(p, &owner.file.?, false) == null) Stream.parseFor(p, oid); const at = set.current orelse (if (set.seek) |s| Places.firstFrom(set.hits.items, s) else 0); const from = if (full) 0 else at -| live_rows / 2; q.window = .{ .lo = from, .hi = from }; @@ -927,6 +933,7 @@ fn firstHit(q: *const Query) ?usize { /// `k` from the current row: the hits on the rows before it, and its own. fn countCurrent(q: *Query) void { + if (q.source == .grep) return countGrepCurrent(q); q.k = 0; const c = q.current orelse return; for (q.items.items[0..c]) |it| q.k += if (it.kind == .hit) @max(1, it.n) else 0; @@ -992,7 +999,7 @@ fn makeNameRows(p: *Pardes, id: usize, pat: []const u8) !void { const f = &rp.file.?; f.pager_spans = spans.toOwnedSlice(p.gpa) catch &.{}; q.total = q.names.hits.items.len; - q.more = q.names.busy(); + q.more = q.names.busy() or p.stream.busyFor(rp.serial); // The best is current until a step or a click moves it. q.current = if (shown > 0) 0 else null; countCurrent(q); @@ -1000,70 +1007,30 @@ fn makeNameRows(p: *Pardes, id: usize, pat: []const u8) !void { showTarget(p, id); } -/// A slice of a Grep: the files the walk has found so far read and -/// searched, about 4 ms of them a frame, each line holding the pattern -/// (a literal, smart case: a capital makes it exact) kept. -/// ponytail: on the loop, a file a step; the disk engine's worker (phase -/// 4) takes this over. -fn grepSlice(p: *Pardes, q: *Query, rp: *Pane, pat: []const u8) void { - const started = Places.nowNs(); - const dir = std.fs.path.dirname(rp.file.?.path) orelse "/"; - const fold = for (pat) |c| { - if (std.ascii.isUpper(c)) break false; - } else true; - var low_buf: [256]u8 = undefined; - const needle = if (fold) std.ascii.lowerString(low_buf[0..@min(pat.len, low_buf.len)], pat[0..@min(pat.len, low_buf.len)]) else pat; - while (q.grep_next < q.names.cands.items.len) { - if (q.found.items.len >= grep_cap) break; - const c = q.names.cands.items[q.grep_next]; - q.grep_next += 1; - const rel = q.names.bytes.items[c.start..][0..c.len]; - var pathbuf: [4096 + 8]u8 = undefined; - const path = std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), rel }) catch continue; - const bytes = blk: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :blk filesystem.read(p, path) catch continue; - }; - defer p.gpa.free(bytes); - if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) continue; - var line_no: u32 = 1; - var ls: usize = 0; - while (ls <= bytes.len) { - const le = std.mem.indexOfScalarPos(u8, bytes, ls, '\n') orelse bytes.len; - const ln = bytes[ls..le]; - const at = if (fold) std.ascii.findIgnoreCase(ln, needle) else std.mem.indexOf(u8, ln, needle); - if (at) |col| { - const path_at: u32 = @intCast(q.found_bytes.items.len); - q.found_bytes.appendSlice(p.gpa, rel) catch return; - const code_at: u32 = @intCast(q.found_bytes.items.len); - const code = ln[0..@min(ln.len, 400)]; - q.found_bytes.appendSlice(p.gpa, code) catch return; - q.found.append(p.gpa, .{ .path = path_at, .path_len = @intCast(rel.len), .line = line_no, .col = @intCast(col), .end = @intCast(col + needle.len), .code = code_at, .code_len = @intCast(code.len) }) catch return; - if (q.found.items.len >= grep_cap) break; - } - if (le == bytes.len) break; - ls = le + 1; - line_no += 1; - } - if (started != 0 and Places.nowNs() - started > Places.frame_ns) break; - } -} - /// A Grep keeps this many lines: the rest wait (the disk engine's `More`). pub const grep_cap = 2000; -fn makeGrepRows(p: *Pardes, id: usize) !void { +/// A Grep's rows: each file's name over its lines, while typed a window +/// of `live_rows` around the current one, `full` (frozen) every one sent. +fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void { const rp = p.panes[id].?; const q = rp.query.?; const dir = std.fs.path.dirname(rp.file.?.path) orelse "/"; + const at: usize = if (q.current) |c| (if (c < q.items.items.len) q.items.items[c].hit else 0) else 0; q.items.clearRetainingCapacity(); var out: std.ArrayList(u8) = .empty; defer out.deinit(p.gpa); var rows: std.ArrayList(locations.Row) = .empty; var last_path: []const u8 = ""; var pathbuf: [4096 + 8]u8 = undefined; - for (q.found.items, 0..) |fd, i| { + const lo = if (full) 0 else at -| live_rows / 2; + const hi = if (full) q.found.items.len else @min(q.found.items.len, lo + live_rows); + if (lo > 0) { + var hb: [64]u8 = undefined; + try noteRow(p, q, &out, &rows, std.fmt.bufPrint(&hb, " ... {d} lines before these", .{lo}) catch " ..."); + } + var current: ?usize = null; + for (q.found.items[lo..hi], lo..) |fd, i| { const rel = q.found_bytes.items[fd.path..][0..fd.path_len]; const path = std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), rel }) catch continue; if (!std.mem.eql(u8, rel, last_path)) { @@ -1072,19 +1039,62 @@ fn makeGrepRows(p: *Pardes, id: usize) !void { try writeRow(p, &out, &rows, rel, .{ .kind = .context, .path = @constCast(path), .at = .{}, .location_end = 0, .code_start = 0 }); } const code = q.found_bytes.items[fd.code..][0..fd.code_len]; + if (i == at) current = q.items.items.len; try q.items.append(p.gpa, .{ .kind = .hit, .hit = i, .n = 1 }); try writeHitRow(p, &out, &rows, 9, path, fd.line, code, fd.col, fd.end, &.{}); } - const walking = q.names.walking() or q.grep_next < q.names.cands.items.len; - if (q.found.items.len >= grep_cap) try noteRow(p, q, &out, &rows, " ... stopped at 2000 lines: type more of it"); + if (hi < q.found.items.len) { + var hb: [64]u8 = undefined; + try noteRow(p, q, &out, &rows, std.fmt.bufPrint(&hb, " ... {d} lines after these", .{q.found.items.len - hi}) catch " ..."); + } + if (p.stream.target == rp.serial and p.stream.state == .capped) try noteRow(p, q, &out, &rows, " ... stopped at 2000 lines: More in the tag goes on"); setRows(p, rp, &out, &rows); q.total = q.found.items.len; - q.more = walking and q.found.items.len < grep_cap; - if (q.current == null or q.current.? >= q.items.items.len) q.current = firstHit(q); - countCurrent(q); + q.more = p.stream.busyFor(rp.serial); + q.current = current orelse firstHit(q); + countGrepCurrent(q); showCurrent(rp); } +/// `k` for a Grep: the current row's line among all the lines sent. +fn countGrepCurrent(q: *Query) void { + q.k = 0; + const c = q.current orelse return; + 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 { + var lines = std.mem.splitScalar(u8, rows, '\n'); + while (lines.next()) |ln| { + if (ln.len == 0) continue; + switch (kind) { + .files => q.names.add(p.gpa, ln, 0, ln.len, Names.baseOf(ln), 0) catch return, + .grep => { + const t = look.parsePathLine(ln); + if (t.at.line == 0 or t.path.len == 0) continue; + const code = if (t.end < ln.len) ln[t.end + 1 ..] else ""; + const path_at: u32 = @intCast(q.found_bytes.items.len); + q.found_bytes.appendSlice(p.gpa, t.path) catch return; + const code_at: u32 = @intCast(q.found_bytes.items.len); + q.found_bytes.appendSlice(p.gpa, code) catch return; + q.found.append(p.gpa, .{ + .path = path_at, + .path_len = @intCast(t.path.len), + .line = @intCast(t.at.line), + .col = @intCast(t.at.col -| 1), + .end = @intCast(if (t.at.end_col > 0) t.at.end_col else t.at.col), + .code = code_at, + .code_len = @intCast(code.len), + }) catch return; + }, + } + } + q.names.gen +%= 1; +} + /// A Grep's hits in each open file are that pane's places: its tints, /// ticks and n/N. fn feedGrepPlaces(p: *Pardes, q: *Query, serial: u32, pat: []const u8) void { @@ -1552,6 +1562,8 @@ pub fn land(p: *Pardes, id: usize, row: ?usize) void { } return; }; + // A Grep's whole list, now it is a result. + if (q.source == .grep) makeGrepRows(p, id, true) catch {}; var buf: [4096 + 64]u8 = undefined; const address = if (meta.at.line > 0) std.fmt.bufPrint(&buf, "{s}:{d}:{d}", .{ meta.path, meta.at.line, meta.at.col }) catch meta.path @@ -1634,8 +1646,26 @@ pub fn cancel(p: *Pardes, id: usize) void { } else p.removePane(id, null) catch {}; } -/// The pane going (Del, its owner's Del): the places it fed go with it. +/// A parse of pane `serial`'s text came back (Stream.receiveParse): its +/// query panes' rows are made again, under the declarations now, and a +/// Symbols query gathers them. +pub fn declsArrived(p: *Pardes, serial: u32) void { + for (p.panes, 0..) |slot, id| { + const rp = slot orelse continue; + const q = rp.query orelse continue; + if (q.owner != serial or q.frozen) continue; + q.made_gen = std.math.maxInt(u64); + if (q.source == .symbols) { + startSource(p, id, q, .symbols, p.paneBySerial(serial)); + if (parse(q.asked)) |a| q.names.typed(p.gpa, a.pat); + } + } +} + +/// The pane going (Del, its owner's Del): the places it fed go with it, +/// and a disk search still running for it. pub fn release(p: *Pardes, pane: *Pane) void { + if (p.stream.target == pane.serial) _ = p.stream.stop(); for (p.panes) |slot| if (slot) |tp| if (tp.places.from == pane.serial) { tp.places.forget(p.gpa); tp.places.clear(p.gpa); @@ -1932,6 +1962,8 @@ fn leader(p: *Pardes, keys: []const u8) void { } fn settle(p: *Pardes) void { + // a typed Grep's pause run out on the clock + p.advance(p.now_ns + Stream.debounce_ns + std.time.ns_per_ms); var frames: usize = 0; while (busy(p) and frames < 2000) : (frames += 1) p.update(.tick); p.update(.tick); |
