From 8ac6db5662dd2db0f082e1651835f1db36e235f8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 11:57:35 -0300 Subject: 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 --- src/Stream.zig | 1177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1177 insertions(+) create mode 100644 src/Stream.zig (limited to 'src/Stream.zig') diff --git a/src/Stream.zig b/src/Stream.zig new file mode 100644 index 00000000..eb420b42 --- /dev/null +++ b/src/Stream.zig @@ -0,0 +1,1177 @@ +//! The disk engine: a query pane's Grep, and the file listing its Find +//! scores, run on a worker thread away from the loop (lifted from the +//! stream prototype). ripgrep when `rg` is on PATH (`rg --json --sort path +//! --fixed-strings --smart-case`, `rg --files --sort path`: it knows +//! .gitignore and is 5-15x faster on a real tree), else pardes's own sorted +//! walk with fs.zig's skips. Rows come back in batches, each stamped with +//! the generation it was started for; a new query bumps the generation and +//! cancels the worker, and the loop drops any batch that is not the live +//! generation's (the race test proves it). A typed Grep starts once the +//! typing pauses (`debounce_ns`); a worker stops at `soft_cap` hits and +//! waits there for `More`. Embedded in Pardes as `stream`: one search on +//! disk at a time, the query pane `target` names. +//! +//! Hosts with workers perform the `search` effect (host_io.Host.search): +//! `start` runs `work` on io.concurrent, posting through the shell's +//! inbox. A host without (a test, a board) runs it on the loop (`runHere`), +//! its cap a hard stop. +const Stream = @This(); + +const std = @import("std"); +const pardes = @import("pardes.zig"); +const config = @import("config.zig"); +const filesystem = @import("fs.zig"); +const look = @import("look.zig"); +const panes = @import("panes.zig"); +const syntax = @import("syntax.zig"); +const Pardes = pardes.Pardes; + +pub const Kind = enum { grep, files }; +pub const Engine = enum { rg, walk }; +pub const State = enum { idle, waiting, searching, capped, done, stopped, failed }; + +/// A keystroke's Grep starts when no other has come for this long. +pub const debounce_ns = 80 * std.time.ns_per_ms; +/// Hits (a Grep's lines, a listing's files) a worker sends before it waits +/// for More. +pub const soft_cap = 2000; +/// A listing of files keeps going this far before it waits: the names are +/// scored on the loop, the cap is for a runaway tree. +pub const files_cap = 200_000; +/// A worker's batch goes once its first row is this old, or it is this big; +/// the first batch goes at its first file with a hit. +const batch_ns = 16 * std.time.ns_per_ms; +const batch_bytes = 256 * 1024; +/// A bigger file is not searched. +const max_file_bytes = 16 << 20; + +/// What a worker sends: rows as fs.zig wrote them (`path:line:col-end +/// text`, a listing's `path`), relative to the search's directory. Owned by +/// the message until taken; the core copies what it keeps. +pub const Batch = struct { + gen: u32, + rows: []u8 = &.{}, + hits: u32 = 0, + /// Files with a hit in this batch's rows. + files: u32 = 0, + /// Files read since the last batch (rg: all of them, at its end). + scanned: u32 = 0, + /// Directories it might not open. + skipped: u32 = 0, + engine: Engine = .walk, + /// The worker waits at its soft cap until More. + capped: bool = false, + end: ?End = null, + /// The worker's own time, start to end (in the last batch). + took_ns: u64 = 0, + /// A parse's answer (`parseFor`): the declarations of pane `serial`'s + /// text at `revision`. + parsed: ?struct { serial: u32, revision: u32 } = null, + decls: []syntax.ContextDeclaration = &.{}, + + pub const End = enum { done, failed }; + + pub fn deinit(b: *const Batch, gpa: std.mem.Allocator) void { + gpa.free(b.rows); + gpa.free(b.decls); + } +}; + +/// A parse of a pane's text for its declarations, on a worker: the rows of +/// a `/` group under them, and Symbols lists them, once it is done. The +/// loop never parses a big text (a 1.2 MB file is seconds in Debug). +pub const ParseJob = struct { + gpa: std.mem.Allocator, + /// What its answer is made with: the shell's, which frees a batch. + out: std.mem.Allocator, + gen: u32, + serial: u32, + revision: u32, + path: []u8, + content: []u8, + /// Set when it is no longer wanted: the parse ends early. + stop: std.atomic.Value(bool) = .init(false), + + pub fn deinit(job: *ParseJob) void { + job.gpa.free(job.path); + job.gpa.free(job.content); + job.gpa.destroy(job); + } +}; + +const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; + +/// Where a worker hands a batch: false when it was turned away (the inbox +/// closed, the wait for room canceled), and the worker stops. +pub const Sink = *const fn (ctx: ?*anyopaque, batch: Batch) bool; + +/// One search, the worker's: copied from the core when it starts. +pub const Job = struct { + gpa: std.mem.Allocator, + gen: u32, + kind: Kind, + pattern: []u8, + /// Rows name files relative to this, the query pane's directory, which + /// is also the one root searched. + base: []u8, + /// Forced (tests, PARDES_STREAM_ENGINE); else rg when there is one. + engine: ?Engine, + /// Hits it may send before it waits; More raises it and wakes it. Run + /// on the loop (no worker) it is a hard stop. + cap: std.atomic.Value(u32) = .init(soft_cap), + waits: bool = true, + + pub fn deinit(job: *Job) void { + const gpa = job.gpa; + gpa.free(job.pattern); + gpa.free(job.base); + gpa.destroy(job); + } +}; + +const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io }; + +gen: u32 = 0, +state: State = .idle, +kind: Kind = .grep, +/// The query pane the rows are for, by serial. +target: u32 = 0, +pattern: std.ArrayList(u8) = .empty, +dir: std.ArrayList(u8) = .empty, +/// A typed query's start, once the pause runs out (step). +due_ns: ?u64 = null, +task: ?Task = null, +/// Forced engine (tests); null, rg when it runs. +engine: ?Engine = null, +used: Engine = .walk, +hits: u32 = 0, +files: u32 = 0, +scanned: u32 = 0, +skipped: u32 = 0, +/// Stale batches dropped (the race test counts them). +dropped: usize = 0, +started_ns: u64 = 0, +first_ns: ?u64 = null, +finished_ns: u64 = 0, +worker_ns: u64 = 0, +/// The parse running or about to (one at a time), its generation, and the +/// one asked for while it ran (pane serial and revision), started after it. +parse: ?ParseTask = null, +parse_gen: u32 = 0, +parse_next: ?struct { serial: u32, revision: u32 } = null, +/// What the last parse cost the worker. +parse_ns: u64 = 0, + +pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { + s.cancelTask(); + s.cancelParse(); + s.pattern.deinit(gpa); + s.dir.deinit(gpa); +} + +/// The parse's worker told to stop and joined: short, its progress +/// callback hears the flag. +pub fn cancelParse(s: *Stream) void { + const t = s.parse orelse return; + s.parse = null; + t.job.stop.store(true, .release); + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.cancel(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); +} + +/// Pane `id`'s declarations asked for, as its text is now, unless a parse of +/// that text is already on its way. With workers, on one; without (a test), +/// here only for a small text, else not at all (the rows stay ungrouped). +pub fn parseFor(p: *Pardes, id: usize) void { + const s = &p.stream; + const pane = p.panes[id] orelse return; + const f = pane.file orelse return; + if (!syntax.supportsPath(f.path) or f.content.len == 0) return; + if (s.parse) |t| { + if (t.job.serial == pane.serial and t.job.revision == f.revision) return; + // A parse of a text that has changed since: told to stop; this one + // after it. + t.job.stop.store(true, .release); + s.parse_next = .{ .serial = pane.serial, .revision = f.revision }; + return; + } + const workers = p.host.vtable.search != null and !p.fs.serving; + if (!workers and f.content.len > 64 << 10) return; + const job = p.gpa.create(ParseJob) catch return; + const path = p.gpa.dupe(u8, f.path) catch { + p.gpa.destroy(job); + return; + }; + const content = p.gpa.dupe(u8, f.content) catch { + p.gpa.free(path); + p.gpa.destroy(job); + return; + }; + s.gen +%= 1; + s.parse_gen = s.gen; + job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = s.parse_gen, .serial = pane.serial, .revision = f.revision, .path = path, .content = content }; + s.parse = .{ .job = job }; + if (workers) return p.emit(.{ .search = .{ .gen = s.parse_gen } }); + // Here and now: a small text, a host with no workers. + var got: Collect = .{ .gpa = p.gpa }; + defer got.batches.deinit(p.gpa); + parseWork(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + for (got.batches.items) |*b| { + receive(p, b); + b.deinit(p.gpa); + } +} + +fn parseWork(io: std.Io, job: *ParseJob, ctx: ?*anyopaque, sink: Sink) void { + const began = std.Io.Clock.awake.now(io); + const decls: []syntax.ContextDeclaration = syntax.declarationsApart(job.out, job.path, job.content, &job.stop) catch @constCast(&[_]syntax.ContextDeclaration{}); + const took: u64 = @intCast(@max(0, began.durationTo(std.Io.Clock.awake.now(io)).nanoseconds)); + _ = sink(ctx, .{ .gen = job.gen, .decls = decls, .parsed = .{ .serial = job.serial, .revision = job.revision }, .end = .done, .took_ns = took }); +} + +fn now() u64 { + if (comptime !filesystem.platform_has_fs) return 0; + const t = std.Io.Clock.awake.now(std.Io.Threaded.global_single_threaded.io()); + return @intCast(@max(0, t.nanoseconds)); +} + +/// Joins the worker, if one runs: a cancel interrupts its waits and reads, +/// so this is short. The turn goes out for it, as for any blocking call: +/// the worker may be reading through a mount of this very session. +pub fn cancelTask(s: *Stream) void { + const t = s.task orelse return; + s.task = null; + pardes.turn.yield(); + var future = t.future; + future.cancel(t.io); + pardes.turn.back(); + t.job.deinit(); +} + +/// A new question for query pane `target`: a Grep of `pattern`, or the +/// listing of the files (`.files`), under `dir`. The generation before it +/// is over (its worker canceled, its batches dropped when they come); +/// `typed`, it starts once the typing pauses, else at the next step. +pub fn query(p: *Pardes, target: u32, kind: Kind, pattern: []const u8, dir: []const u8, how: enum { typed, now }) void { + const s = &p.stream; + s.cancelTask(); + s.gen +%= 1; + s.kind = kind; + s.target = target; + s.pattern.clearRetainingCapacity(); + s.pattern.appendSlice(p.gpa, pattern) catch return; + s.dir.clearRetainingCapacity(); + s.dir.appendSlice(p.gpa, dir) catch return; + s.hits = 0; + s.files = 0; + s.scanned = 0; + s.skipped = 0; + s.first_ns = null; + s.state = .waiting; + s.due_ns = if (how == .typed) p.now_ns + debounce_ns else p.now_ns; +} + +/// Over: the worker canceled, nothing more of it lands. +pub fn stop(s: *Stream) bool { + if (s.state != .waiting and s.state != .searching and s.state != .capped) return false; + s.cancelTask(); + s.due_ns = null; + s.gen +%= 1; + s.state = .stopped; + s.finished_ns = now(); + return true; +} + +/// More: a worker waiting at its cap goes on for another `soft_cap` hits. +pub fn more(s: *Stream) bool { + if (s.state != .capped) return false; + const t = s.task orelse return false; + _ = t.job.cap.fetchAdd(soft_cap, .release); + t.io.futexWake(u32, &t.job.cap.raw, 1); + s.state = .searching; + return true; +} + +/// The `More` word: the query pane's disk search past its cap. +pub fn moreFor(p: *Pardes, id: usize) bool { + if (!p.stream.more()) return false; + if (p.panes[id]) |pane| if (pane.query) |q| { + q.capped = false; + }; + if (p.paneBySerial(p.stream.target)) |tid| if (p.panes[tid].?.query) |q| { + q.capped = false; + }; + return true; +} + +/// Whether query pane `serial`'s rows are still coming. +pub fn busyFor(s: *const Stream, serial: u32) bool { + return s.target == serial and (s.state == .waiting or s.state == .searching); +} + +/// On each tick: a query whose pause ran out starts, on a worker where the +/// host has one, else here and now. +pub fn step(p: *Pardes) void { + const s = &p.stream; + const due = s.due_ns orelse return; + if (p.now_ns < due) return; + s.due_ns = null; + s.state = .searching; + s.started_ns = now(); + // A script's write (whose answer says whether it found anything) and a + // host with no workers: done before this returns. + if (p.host.vtable.search != null and !p.fs.serving) p.emit(.{ .search = .{ .gen = s.gen } }) else runHere(p, s.gen); +} + +/// When the core next needs a tick for the stream: the end of the pause. +pub fn wake(s: *const Stream, next_frame: u64) ?u64 { + const due = s.due_ns orelse return null; + return @max(due, next_frame); +} + +/// Start generation `gen` on a worker that sends to `sink` (a shell's +/// inbox). Called by the shell for the `search` effect, on the loop. +pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: Sink) void { + const s = &p.stream; + // A parse's generation: its own task. + if (s.parse) |*t| if (gen == s.parse_gen and t.job.gen == gen and t.future == null) { + t.job.out = gpa; + t.future = io.concurrent(parseWork, .{ io, t.job, ctx, sink }) catch { + const job = t.job; + s.parse = null; + job.deinit(); + return; + }; + t.io = io; + return; + }; + if (gen != s.gen or s.state != .searching) return; + s.cancelTask(); + const job = makeJob(gpa, s) catch |err| { + s.state = .failed; + return p.reportError(p.active, "search", err); + }; + const future = io.concurrent(work, .{ io, job, ctx, sink }) catch { + // No worker to be had: it runs here, on the loop. + job.deinit(); + return runHere(p, gen); + }; + s.task = .{ .job = job, .future = future, .io = io }; +} + +fn makeJob(gpa: std.mem.Allocator, s: *const Stream) !*Job { + const job = try gpa.create(Job); + errdefer gpa.destroy(job); + const pattern = try gpa.dupe(u8, s.pattern.items); + errdefer gpa.free(pattern); + const base = try gpa.dupe(u8, s.dir.items); + job.* = .{ .gpa = gpa, .gen = s.gen, .kind = s.kind, .pattern = pattern, .base = base, .engine = s.engine }; + if (s.kind == .files) job.cap = .init(files_cap); + return job; +} + +/// A host with no workers (a test's, the fallback when none can be had): +/// the whole search on the loop, the turn out for it, its batches taken in +/// as a worker's would be. Its cap is a hard stop. +pub fn runHere(p: *Pardes, gen: u32) void { + if (comptime !filesystem.platform_has_fs) { + p.stream.state = .failed; + return; + } + const s = &p.stream; + if (gen != s.gen or s.state != .searching) return; + const job = makeJob(p.gpa, s) catch |err| { + s.state = .failed; + return p.reportError(p.active, "search", err); + }; + defer job.deinit(); + job.waits = false; + var got: Collect = .{ .gpa = p.gpa }; + defer got.batches.deinit(p.gpa); + { + pardes.turn.yield(); + defer pardes.turn.back(); + work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + } + for (got.batches.items) |*b| { + receive(p, b); + b.deinit(p.gpa); + } +} + +const Collect = struct { + gpa: std.mem.Allocator, + batches: std.ArrayList(Batch) = .empty, + + fn take(ctx: ?*anyopaque, batch: Batch) bool { + const c: *Collect = @ptrCast(@alignCast(ctx.?)); + c.batches.append(c.gpa, batch) catch { + batch.deinit(c.gpa); + return false; + }; + return true; + } +}; + +/// A batch from a worker (`Event.search`), borrowed: the live generation's +/// rows go to its query pane (a Grep's lines, a listing's names); any other +/// generation's are dropped whole. +pub fn receive(p: *Pardes, b: *const Batch) void { + const s = &p.stream; + if (b.parsed) |parsed| return receiveParse(p, b, parsed.serial, parsed.revision); + if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { + s.dropped += 1; + return; + } + s.used = b.engine; + s.hits += b.hits; + s.files += b.files; + s.scanned += b.scanned; + s.skipped += b.skipped; + if (s.first_ns == null and b.rows.len > 0) s.first_ns = now(); + if (b.capped) s.state = .capped; + if (b.end) |end| { + s.worker_ns = b.took_ns; + s.state = if (end == .failed) .failed else .done; + s.finished_ns = now(); + // It sent its last: its join is a return away. + if (s.task) |t| { + s.task = null; + pardes.turn.yield(); + var future = t.future; + future.await(t.io); + pardes.turn.back(); + t.job.deinit(); + } + } + p.needs_frame = true; + 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); +} + +/// A parse's answer: its task joined; the declarations kept as the pane's +/// (File.State.context_declarations, for its revision) when its text is +/// still what was parsed; the query panes of that text told; and a parse +/// asked for while it ran started. +fn receiveParse(p: *Pardes, b: *const Batch, serial: u32, revision: u32) void { + const s = &p.stream; + if (s.parse) |t| if (t.job.gen == b.gen) { + s.parse = null; + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.await(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); + }; + s.parse_ns = b.took_ns; + if (p.paneBySerial(serial)) |id| if (p.panes[id].?.file) |*f| if (f.revision == revision and b.decls.len > 0) { + if (p.tree_sitter_gpa.dupe(syntax.ContextDeclaration, b.decls)) |copy| { + if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); + f.context_declarations = copy; + f.context_revision = revision; + panes.Query.declsArrived(p, serial); + } else |_| {} + }; + p.needs_frame = true; + if (s.parse_next) |next| { + s.parse_next = null; + if (p.paneBySerial(next.serial)) |id| parseFor(p, id); + } +} + +// ---- the worker ---- + +const Worker = struct { + io: std.Io, + gpa: std.mem.Allocator, + job: *Job, + ctx: ?*anyopaque, + sink: Sink, + engine: Engine = .walk, + exact: bool, + rows: std.ArrayList(u8) = .empty, + hits: u32 = 0, + files: u32 = 0, + scanned: u32 = 0, + skipped: u32 = 0, + total: u32 = 0, + sent: bool = false, + batch_began: ?std.Io.Timestamp = null, + began: std.Io.Timestamp, + /// One read buffer for every file: grown to the biggest, never 0xAA'd per file. + buf: std.ArrayList(u8) = .empty, + + fn deinit(w: *Worker) void { + w.rows.deinit(w.gpa); + w.buf.deinit(w.gpa); + } + + fn send(w: *Worker, end: ?Batch.End, capped: bool) error{ Refused, OutOfMemory }!void { + const rows = try w.rows.toOwnedSlice(w.gpa); + const batch: Batch = .{ + .gen = w.job.gen, + .rows = rows, + .hits = w.hits, + .files = w.files, + .scanned = w.scanned, + .skipped = w.skipped, + .engine = w.engine, + .capped = capped, + .end = end, + .took_ns = if (end != null) @intCast(@max(0, w.began.durationTo(std.Io.Clock.awake.now(w.io)).nanoseconds)) else 0, + }; + w.hits = 0; + w.files = 0; + w.scanned = 0; + w.skipped = 0; + w.sent = true; + w.batch_began = null; + if (!w.sink(w.ctx, batch)) return error.Refused; + } + + /// After a file with hits: the first batch goes at once (what the user + /// sees first), the others once old or big enough. + fn maybeSend(w: *Worker) !void { + if (w.rows.items.len == 0) return; + const began = w.batch_began orelse { + w.batch_began = std.Io.Clock.awake.now(w.io); + if (!w.sent) try w.send(null, false); + return; + }; + const age = began.durationTo(std.Io.Clock.awake.now(w.io)).nanoseconds; + if (age >= batch_ns or w.rows.items.len >= batch_bytes) try w.send(null, false); + } + + /// At the cap, what is gathered goes, and the worker waits for More (a + /// cancel ends the wait); on the loop it is the end. + fn checkCap(w: *Worker) !bool { + var cap = w.job.cap.load(.acquire); + if (w.total < cap) return false; + if (!w.job.waits) return true; + try w.send(null, true); + while (w.total >= cap) { + try w.io.futexWait(u32, &w.job.cap.raw, cap); + cap = w.job.cap.load(.acquire); + } + return false; + } + + /// Smart case: a pattern with a capital matches exactly, else in any case. + fn find(w: *const Worker, hay: []const u8) ?usize { + return if (w.exact) std.mem.indexOf(u8, hay, w.job.pattern) else std.ascii.findIgnoreCase(hay, w.job.pattern); + } + + fn grepText(w: *Worker, path: []const u8, text: []const u8) !u32 { + var found: u32 = 0; + var line: usize = 0; + var it = std.mem.splitScalar(u8, text, '\n'); + while (it.next()) |raw| { + line += 1; + if (w.total >= w.job.cap.load(.acquire)) break; + const at = w.find(raw) orelse continue; + try w.row(path, line, at, at + w.job.pattern.len, raw); + found += 1; + } + return found; + } + + fn row(w: *Worker, path: []const u8, line: usize, from: usize, end: usize, raw: []const u8) !void { + const ln = std.mem.trimEnd(u8, raw, " \t\r\n"); + var cut = @min(ln.len, 400); + while (cut > 0 and cut < ln.len and ln[cut] & 0xc0 == 0x80) cut -= 1; + try w.rows.print(w.gpa, "{s}:{d}:{d}{c}{d} {s}\n", .{ path, line, from + 1, config.range_sep, end, ln[0..cut] }); + w.hits += 1; + w.total += 1; + } + + /// A listing's file: its name, relative to the base. + fn file(w: *Worker, path: []const u8) !bool { + try w.rows.print(w.gpa, "{s}\n", .{path}); + w.hits += 1; + w.total += 1; + w.files += 1; + try w.maybeSend(); + return w.checkCap(); + } + + /// The name a row gives `abs`: under the base, relative to it. + fn shown(w: *const Worker, abs: []const u8) []const u8 { + const base = std.mem.trimEnd(u8, w.job.base, "/"); + if (abs.len > base.len + 1 and std.mem.startsWith(u8, abs, base) and abs[base.len] == '/') return abs[base.len + 1 ..]; + if (std.mem.startsWith(u8, abs, "./")) return abs[2..]; + return abs; + } + + // ---- our own walk: sorted, depth first, fs.zig's skips ---- + + fn walk(w: *Worker) !void { + var dir = std.Io.Dir.cwd().openDir(w.io, w.job.base, .{ .iterate = true }) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => return, + }; + defer dir.close(w.io); + _ = try w.walkDir(dir, "", 0); + } + + /// Whether the cap ended it (on the loop). + fn walkDir(w: *Worker, dir: std.Io.Dir, prefix: []const u8, depth: usize) anyerror!bool { + try w.io.checkCancel(); + var arena: std.heap.ArenaAllocator = .init(w.gpa); + defer arena.deinit(); + const a = arena.allocator(); + const Entry = struct { name: []const u8, kind: std.Io.File.Kind }; + var entries: std.ArrayList(Entry) = .empty; + var it = dir.iterate(); + while (true) { + const e = it.next(w.io) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => { + w.skipped += 1; + break; + }, + } orelse break; + try entries.append(a, .{ .name = try a.dupe(u8, e.name), .kind = e.kind }); + } + std.mem.sort(Entry, entries.items, {}, struct { + fn lt(_: void, x: Entry, y: Entry) bool { + return std.mem.lessThan(u8, x.name, y.name); + } + }.lt); + for (entries.items) |e| { + const path = if (prefix.len == 0) e.name else try std.fmt.allocPrint(a, "{s}/{s}", .{ prefix, e.name }); + switch (e.kind) { + .directory => { + if (depth + 1 >= filesystem.WalkNotes.max_depth or e.name[0] == '.') continue; + if (for (filesystem.find_skip) |skip| { + if (std.mem.eql(u8, e.name, skip)) break true; + } else false) continue; + var sub = dir.openDir(w.io, e.name, .{ .iterate = true }) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => { + w.skipped += 1; + continue; + }, + }; + defer sub.close(w.io); + if (try w.walkDir(sub, path, depth + 1)) return true; + }, + .file => switch (w.job.kind) { + .grep => if (try w.grepFile(dir, e.name, path)) return true, + .files => if (try w.file(path)) return true, + }, + else => {}, + } + } + return false; + } + + fn grepFile(w: *Worker, dir: std.Io.Dir, name: []const u8, path: []const u8) !bool { + const f = dir.openFile(w.io, name, .{}) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => return false, + }; + defer f.close(w.io); + const len = f.length(w.io) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => return false, + }; + // ponytail: a file past 16 MiB is passed over unsaid; a chunked + // read, as fs.grep's, would search it. + if (len > max_file_bytes) return false; + try w.buf.resize(w.gpa, @intCast(len)); + const n = f.readPositionalAll(w.io, w.buf.items, 0) catch |err| switch (err) { + error.Canceled => return error.Canceled, + else => return false, + }; + const text = w.buf.items[0..n]; + w.scanned += 1; + if (std.mem.indexOfScalar(u8, text[0..@min(text.len, 1024)], 0) != null) return false; + const found = try w.grepText(path, text); + if (found == 0) return false; + w.files += 1; + try w.maybeSend(); + return w.checkCap(); + } + + // ---- ripgrep: a child on the worker, parsed, never shown ---- + + /// error.NoRipgrep when there is none to run: the walk does it. + fn ripgrep(w: *Worker) !void { + var argv: std.ArrayList([]const u8) = .empty; + defer argv.deinit(w.gpa); + switch (w.job.kind) { + .grep => { + try argv.appendSlice(w.gpa, &.{ "rg", "--json", "--sort", "path", "--fixed-strings", "--smart-case", "--no-messages", "--" }); + try argv.append(w.gpa, w.job.pattern); + }, + .files => try argv.appendSlice(w.gpa, &.{ "rg", "--files", "--sort", "path", "--no-messages" }), + } + // As selection_pipe's children: this thread's mask is the child's. + const none = std.posix.sigemptyset(); + var kept: std.posix.sigset_t = undefined; + std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept); + var child = std.process.spawn(w.io, .{ + .argv = argv.items, + .cwd = if (w.job.base.len == 0) .inherit else .{ .path = w.job.base }, + .stdin = .ignore, + .stdout = .pipe, + .stderr = .ignore, + }) catch |err| { + std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); + return if (err == error.Canceled) error.Canceled else error.NoRipgrep; + }; + std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); + w.engine = .rg; + // Canceled, capped on the loop, failed: it is killed and reaped. + defer if (child.id != null) child.kill(w.io); + var line: std.ArrayList(u8) = .empty; + defer line.deinit(w.gpa); + var json_arena: std.heap.ArenaAllocator = .init(w.gpa); + defer json_arena.deinit(); + var file_hits: u32 = 0; + var file_path: std.ArrayList(u8) = .empty; + defer file_path.deinit(w.gpa); + // Cut at the cap (on the loop), rg is left to the kill. + var cut = false; + var chunk: [64 * 1024]u8 = undefined; + read: while (true) { + const n = child.stdout.?.readStreaming(w.io, &.{&chunk}) catch |err| switch (err) { + error.EndOfStream => break, + error.Canceled => return error.Canceled, + else => return error.ReadFailed, + }; + if (n == 0) break; + var at: usize = 0; + while (std.mem.indexOfScalarPos(u8, chunk[0..n], at, '\n')) |nl| { + try line.appendSlice(w.gpa, chunk[at..nl]); + at = nl + 1; + defer line.clearRetainingCapacity(); + if (w.job.kind == .files) { + // a dot-file is passed over, as the walk passes over + // dot-directories + if (std.mem.startsWith(u8, std.fs.path.basename(line.items), ".")) continue; + w.scanned += 1; + if (try w.file(w.shown(line.items))) { + cut = true; + break :read; + } + continue; + } + _ = json_arena.reset(.retain_capacity); + const value = std.json.parseFromSliceLeaky(std.json.Value, json_arena.allocator(), line.items, .{}) catch continue; + const obj = if (value == .object) value.object else continue; + const kind = (obj.get("type") orelse continue); + if (kind != .string) continue; + const data = if (obj.get("data")) |d| (if (d == .object) d.object else continue) else continue; + if (std.mem.eql(u8, kind.string, "begin")) { + file_hits = 0; + file_path.clearRetainingCapacity(); + const text = jsonText(data.get("path")) orelse continue; + try file_path.appendSlice(w.gpa, w.shown(text)); + } else if (std.mem.eql(u8, kind.string, "match")) { + if (file_path.items.len == 0 or w.total >= w.job.cap.load(.acquire)) continue; + const text = jsonText(data.get("lines")) orelse continue; + const number = data.get("line_number") orelse continue; + if (number != .integer) continue; + var from: usize = 0; + var end: usize = w.job.pattern.len; + if (data.get("submatches")) |subs| if (subs == .array and subs.array.items.len > 0 and subs.array.items[0] == .object) { + const first = subs.array.items[0].object; + if (first.get("start")) |v| if (v == .integer) { + from = @intCast(v.integer); + }; + if (first.get("end")) |v| if (v == .integer) { + end = @intCast(v.integer); + }; + }; + try w.row(file_path.items, @intCast(number.integer), from, end, text); + file_hits += 1; + } else if (std.mem.eql(u8, kind.string, "end")) { + if (file_hits == 0) continue; + w.files += 1; + file_hits = 0; + try w.maybeSend(); + // At the cap rg is left blocked on its full pipe: its + // own backpressure, until More or a cancel. + if (try w.checkCap()) { + cut = true; + break :read; + } + } else if (std.mem.eql(u8, kind.string, "summary")) { + if (data.get("stats")) |stats| if (stats == .object) if (stats.object.get("searches")) |v| if (v == .integer) { + w.scanned = @intCast(v.integer); + }; + } + } + try line.appendSlice(w.gpa, chunk[at..n]); + } + if (!cut and child.id != null) { + _ = child.wait(w.io) catch |err| if (err == error.Canceled) return error.Canceled; + child.id = null; + } + } +}; + +/// rg's `{"text": …}` (a name or line that is not UTF-8 comes as bytes, +/// and is passed over). +fn jsonText(v: ?std.json.Value) ?[]const u8 { + const o = v orelse return null; + if (o != .object) return null; + const t = o.object.get("text") orelse return null; + return if (t == .string) t.string else null; +} + +fn work(io: std.Io, job: *Job, ctx: ?*anyopaque, sink: Sink) void { + var exact = false; + for (job.pattern) |c| exact = exact or std.ascii.isUpper(c); + var w: Worker = .{ .io = io, .gpa = job.gpa, .job = job, .ctx = ctx, .sink = sink, .exact = exact, .began = std.Io.Clock.awake.now(io) }; + defer w.deinit(); + const forced: ?Engine = job.engine orelse if (std.c.getenv("PARDES_STREAM_ENGINE")) |v| + std.meta.stringToEnum(Engine, std.mem.span(v)) + else + null; + const ran = if ((forced orelse .rg) == .rg) w.ripgrep() else error.NoRipgrep; + ran catch |err| switch (err) { + error.Canceled, error.Refused => return, + error.NoRipgrep => { + w.engine = .walk; + w.walk() catch |walk_err| switch (walk_err) { + error.Canceled, error.Refused => return, + else => { + w.send(.failed, false) catch {}; + return; + }, + }; + }, + else => { + w.send(.failed, false) catch {}; + return; + }, + }; + w.send(.done, false) catch {}; +} + +// ---- tests ---- + +const host_io = @import("host_io.zig"); +const testing = std.testing; + +/// The shell's half, in a test: perform the `search` effect on a real +/// worker, hand what it posts to the core. +const TestShell = struct { + q: host_io.Inbox, + + fn init() TestShell { + return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; + } + + const vtable: pardes.Host.VTable = .{ .search = struct { + fn f(_: ?*anyopaque, _: u32) void {} + }.f }; + + fn attach(p: *Pardes) void { + p.host = .{ .vtable = &vtable }; + } + + fn pump(t: *TestShell, p: *Pardes) void { + while (p.nextEffect()) |e| switch (e) { + .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch), + else => {}, + }; + var batch: host_io.Inbox.Batch = .{}; + t.q.takeInto(&batch); + for (batch.slice()) |m| switch (m) { + .search => |b| { + p.update(.{ .search = b }); + b.deinit(testing.allocator); + }, + else => m.deinit(testing.allocator, testing.allocator), + }; + } + + /// Pump until the search is over (or `limit_ms` passes: a failure). + fn finish(t: *TestShell, p: *Pardes, limit_ms: i64) !void { + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + limit_ms; + while (p.stream.state == .searching or p.stream.state == .waiting) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + if (p.stream.state == .waiting) p.update(.tick); + t.pump(p); + testing.io.sleep(.fromMicroseconds(200), .awake) catch {}; + } + } +}; + +/// A worker that never heard of the cancel: it pushes rows of a stale +/// generation for as long as the live one streams. +const Rogue = struct { + q: *host_io.Inbox, + gen: u32, + stop: std.atomic.Value(bool) = .init(false), + done: std.atomic.Value(bool) = .init(false), + pushed: usize = 0, + + fn run(r: *Rogue) void { + defer r.done.store(true, .release); + while (!r.stop.load(.acquire) and r.pushed < 200) { + const rows = testing.allocator.dupe(u8, "d00/f000.txt:1:3-8 stale stale\n") catch return; + if (!host_io.Inbox.pushSearch(r.q, .{ .gen = r.gen, .rows = rows, .hits = 1, .files = 1 })) return; + r.pushed += 1; + std.Thread.yield() catch {}; + } + } +}; + +test "Stream: a stale generation's rows never land in the query pane, however two generations race (walk and rg)" { + if (comptime !filesystem.platform_has_fs) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var root_buf: [4096]u8 = undefined; + const files = 120; + var name_buf: [64]u8 = undefined; + for (0..files) |i| { + const name = try std.fmt.bufPrint(&name_buf, "d{d:0>2}/f{d:0>3}.txt", .{ i / 10, i }); + try tmp.dir.createDirPath(testing.io, std.fs.path.dirname(name).?); + try tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = "a needle line\nsome hay here\nneedle again\nhay hay\nmore hay\n" }); + } + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("x\n"); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + shell.q.deinit(); + } + // The query pane the rows are for: a Grep's. + panes.Query.open(p, 0, .grep, "", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const qp = p.panes[qid].?; + const q = qp.query.?; + var dropped_total: usize = 0; + for ([_]Engine{ .walk, .rg }) |engine| { + p.stream.engine = engine; + for (0..12) |round| { + // A starts and runs a little (0, 1 or 2 pumps: its batches in + // flight, queued, or taken in), then B replaces it, typed (its + // pause run out on the clock) or at once. + const a = if (round % 2 == 0) "needle" else "hay"; + const b = if (round % 2 == 0) "hay" else "needle"; + q.found.clearRetainingCapacity(); + q.found_bytes.clearRetainingCapacity(); + query(p, qp.serial, .grep, a, root, .now); + p.update(.tick); + for (0..round % 3) |_| shell.pump(p); + var rogue: Rogue = .{ .q = &shell.q, .gen = p.stream.gen }; + q.found.clearRetainingCapacity(); + q.found_bytes.clearRetainingCapacity(); + if (round % 4 == 3) { + query(p, qp.serial, .grep, b, root, .typed); + try testing.expectEqual(State.waiting, p.stream.state); + p.advance(p.now_ns + debounce_ns + std.time.ns_per_ms); + } else query(p, qp.serial, .grep, b, root, .now); + try testing.expect(p.stream.gen != rogue.gen); + const thread = try std.Thread.spawn(.{}, Rogue.run, .{&rogue}); + const finished = shell.finish(p, 20_000); + // It may be waiting for room: the queue is taken until it ends. + rogue.stop.store(true, .release); + while (!rogue.done.load(.acquire)) shell.pump(p); + thread.join(); + shell.pump(p); // whatever it pushed last + try finished; + try testing.expectEqual(State.done, p.stream.state); + // b's lines, every one, and not one of a's nor the rogue's. + const per_file: usize = if (std.mem.eql(u8, b, "hay")) 3 else 2; + try testing.expectEqual(files * per_file, q.found.items.len); + try testing.expect(std.mem.indexOf(u8, q.found_bytes.items, "stale") == null); + for (q.found.items) |fd| { + const code = q.found_bytes.items[fd.code..][0..fd.code_len]; + try testing.expect(std.ascii.findIgnoreCase(code, b) != null); + } + if (engine == .rg and p.stream.used != .rg) return; // no rg here: the walk was the test + dropped_total += p.stream.dropped; + p.stream.dropped = 0; + } + } + try testing.expect(dropped_total > 0); +} + +test "Stream: the listing of files for Find on a worker, a cap of hits stops the walk on the loop" { + if (comptime !filesystem.platform_has_fs) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var root_buf: [4096]u8 = undefined; + try tmp.dir.createDirPath(testing.io, "src"); + try tmp.dir.createDirPath(testing.io, ".git"); + for ([_][]const u8{ "src/a.zig", "src/b.zig", "notes.md", ".git/config" }) |name| + try tmp.dir.writeFile(testing.io, .{ .sub_path = name, .data = "needle\n" }); + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("x\n"); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + p.advance(1); + p.stream.engine = .walk; + // No host workers: the listing runs on the loop at the next tick. + panes.Query.open(p, 0, .find, "", false); + p.update(.tick); + p.update(.tick); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const q = p.panes[qid].?.query.?; + // the three files, not the dot-directory's + try testing.expectEqual(@as(usize, 3), q.names.cands.items.len); + try testing.expectEqual(State.done, p.stream.state); +} + +test "Stream: a big text's declarations are parsed on a worker, never the loop, and the rows of its / regroup under them" { + if (comptime !filesystem.platform_has_fs) return; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..4000) |i| try text.print(testing.allocator, "fn f{d}() void {{\n _ = needle;\n}}\n", .{i}); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + shell.q.deinit(); + } + for ("/needle") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const qp = p.panes[qid].?; + // Not grouped yet: no parse on the loop; one asked of the worker. + try testing.expect(p.stream.parse != null); + try testing.expect(std.mem.indexOf(u8, qp.file.?.content, "fn f") == null); + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + 60_000; + while (pane.file.?.context_revision == null or pane.file.?.context_revision.? != pane.file.?.revision) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + shell.pump(p); + p.update(.tick); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + p.update(.tick); + // Now under their declarations: `fn fN() void {` headers. + try testing.expect(std.mem.indexOf(u8, qp.file.?.content, "fn f") != null); + try testing.expect(p.stream.parse == null); +} + +test "zz stream timings on the stress file with a worker (QUERY_TIMES=)" { + const path_z = std.c.getenv("QUERY_TIMES") orelse return error.SkipZigTest; + const path = std.mem.span(path_z); + const bytes = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(8 << 20)); + defer testing.allocator.free(bytes); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + const pane = try p.setTestFile(bytes); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, path); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + shell.q.deinit(); + } + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + pane.body.cur_row = 15000; + pane.ensureCursorVisible(); + _ = try p.render(frame.allocator()); + const t0 = now(); + for ("/pane") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + const typed = now() - t0; + var worst_wait: u64 = 0; + while (pane.file.?.context_revision == null or pane.file.?.context_revision.? != pane.file.?.revision) { + const t1 = now(); + shell.pump(p); + p.update(.tick); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + worst_wait = @max(worst_wait, now() - t1); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + var worst_key: u64 = 0; + for (".v") |c| { + const t2 = now(); + p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + worst_key = @max(worst_key, now() - t2); + for (0..12) |_| p.update(.tick); + } + std.debug.print("stream: '/pane' typed in {d} us; the worker's parse {d} ms; worst frame while it parsed {d} us; a keystroke with grouped rows {d} us\n", .{ typed / 1000, p.stream.parse_ns / std.time.ns_per_ms, worst_wait / 1000, worst_key / 1000 }); +} + +test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's tag goes on" { + if (comptime !filesystem.platform_has_fs) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var root_buf: [4096]u8 = undefined; + var name_buf: [64]u8 = undefined; + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..20) |_| try text.appendSlice(testing.allocator, "a needle here\n"); + for (0..150) |i| try tmp.dir.writeFile(testing.io, .{ .sub_path = try std.fmt.bufPrint(&name_buf, "f{d:0>3}.txt", .{i}), .data = text.items }); + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("x\n"); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + shell.q.deinit(); + } + p.stream.engine = .walk; + panes.Query.open(p, 0, .grep, "needle", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const qp = p.panes[qid].?; + p.advance(p.now_ns + debounce_ns + std.time.ns_per_ms); + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + 20_000; + while (p.stream.state != .capped) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + p.update(.tick); + shell.pump(p); + testing.io.sleep(.fromMicroseconds(200), .awake) catch {}; + } + p.update(.tick); + try testing.expectEqual(@as(usize, soft_cap), qp.query.?.found.items.len); + try testing.expect(qp.query.?.capped); + try testing.expectEqualStrings(" More Del", @import("tagline.zig").curTail(qp)); + pardes.exec.runBuiltin(p, .More, qid, "", null); + try shell.finish(p, 20_000); + p.update(.tick); + try testing.expectEqual(State.done, p.stream.state); + try testing.expectEqual(@as(usize, 3000), qp.query.?.found.items.len); + try testing.expect(!qp.query.?.capped); +} -- cgit v1.3 From 0d3179f04a91c942fc10b0a05cdd6b8aa958d13e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 14:38:19 -0300 Subject: anim 5: a scroll's colours are painted ahead on a worker (Stream.paintAhead), two views on the way it goes, so a page after a page parses nothing on the frame --- src/File.zig | 32 +++++++++ src/Stream.zig | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/draw.zig | 23 +++++-- src/pardes.zig | 1 + src/syntax.zig | 49 ++++++++++++++ 5 files changed, 302 insertions(+), 4 deletions(-) (limited to 'src/Stream.zig') diff --git a/src/File.zig b/src/File.zig index cf44c69a..798e3e84 100644 --- a/src/File.zig +++ b/src/File.zig @@ -18,6 +18,7 @@ const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); const Tables = @import("Tables.zig"); +const Stream = @import("Stream.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; @@ -1730,10 +1731,22 @@ pub fn refreshHighlights(p: *Pardes) void { need_end <= f.highlight_start + f.highlights.len) { f.syntax_dirty = false; + if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f); continue; } const slack: usize = if (f.highlights.len == 0) 0 else pane.rows; const whole = pane.colorAlgo() == .locations or f.tree_context; + // Painted ahead on a worker (Stream.paintAhead): taken, no parse. + if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| { + if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); + f.highlights = got.styles; + f.highlight_start = got.start; + f.syntax_dirty = false; + p.stream.painted_ahead += 1; + paintAhead(p, pane, f); + continue; + }; + if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1; const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); const end = if (whole) f.content.len @@ -1755,9 +1768,28 @@ pub fn refreshHighlights(p: *Pardes) void { f.highlights = new_highlights; f.highlight_start = if (f.highlights.len > 0) start else 0; f.syntax_dirty = false; + if (!whole and pane.colorAlgo() == .source) paintAhead(p, pane, f); } } +/// The stretch a scroll of `pane` is heading into, two views on the way it +/// last went, asked of a worker (Stream.paintAhead) so it is painted by the +/// time the view gets there. +fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { + const seen = &p.stream.ahead_seen; + if (seen.serial == pane.serial and f.scroll != seen.scroll) seen.dir = if (f.scroll > seen.scroll) 1 else -1; + if (seen.serial != pane.serial) seen.dir = 1; + seen.serial = pane.serial; + seen.scroll = f.scroll; + const rows: usize = pane.rows; + const view = if (seen.dir > 0) f.scroll + 2 * rows else f.scroll -| 2 * rows; + const start = lineStart(p.gpa, f, view -| rows); + const end = @max(start, lineStart(p.gpa, f, view + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS)); + // Painted already, as far as that goes. + if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return; + Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end); +} + pub fn supportsContext(pane: *const Pane) bool { const file = pane.file orelse return false; return file.output == null and file.mini == null and syntax.supportsPath(file.path); diff --git a/src/Stream.zig b/src/Stream.zig index eb420b42..ace73ca4 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -68,12 +68,17 @@ pub const Batch = struct { /// text at `revision`. parsed: ?struct { serial: u32, revision: u32 } = null, decls: []syntax.ContextDeclaration = &.{}, + /// A paint's answer (`paintAhead`): the colours of pane `serial`'s text + /// at `revision` from byte `start`, one syntax.Syn a byte. + painted: ?struct { serial: u32, revision: u32, start: usize } = null, + styles: []u8 = &.{}, pub const End = enum { done, failed }; pub fn deinit(b: *const Batch, gpa: std.mem.Allocator) void { gpa.free(b.rows); gpa.free(b.decls); + gpa.free(b.styles); } }; @@ -101,6 +106,41 @@ pub const ParseJob = struct { const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +/// The colours of the text a scroll is heading into, worked out on a +/// worker before it gets there, as a game streams in the level ahead of +/// the player: refreshHighlights paints only the rows a view shows (and a +/// view's worth either side), so a scroll past them used to parse the next +/// stretch on the frame (6-58 ms in Debug on a 1.6 MB file, a hitch every +/// few pages). Now it finds them painted already, and only a jump past +/// what was foreseen (ge, a Look) paints on the frame. +/// ponytail: one paint at a time, for the pane last scrolled, in the way it +/// went; a pane per worker if two scroll at once. +pub const PaintJob = struct { + gpa: std.mem.Allocator, + out: std.mem.Allocator, + gen: u32, + serial: u32, + revision: u32, + /// Where `source` starts in the text. + start: usize, + path: []u8, + source: []u8, + stop: std.atomic.Value(bool) = .init(false), + + pub fn deinit(job: *PaintJob) void { + job.gpa.free(job.path); + job.gpa.free(job.source); + job.gpa.destroy(job); + } +}; + +const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; + +/// A paint's generation has the top bit, so it can never be a search's or +/// a parse's (those count from 0 in `gen`, and it would take two billion +/// searches to reach it). +const paint_bit: u32 = 1 << 31; + /// Where a worker hands a batch: false when it was turned away (the inbox /// closed, the wait for room canceled), and the worker stops. pub const Sink = *const fn (ctx: ?*anyopaque, batch: Batch) bool; @@ -161,10 +201,23 @@ parse_gen: u32 = 0, parse_next: ?struct { serial: u32, revision: u32 } = null, /// What the last parse cost the worker. parse_ns: u64 = 0, +/// The paint running (one at a time), its generation's count, and the +/// painted stretch it gave, kept until the view gets there (tree_sitter_gpa). +paint: ?PaintTask = null, +paint_count: u32 = 0, +ahead: []u8 = &.{}, +ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, +/// Stretches a view took painted ahead, and painted on the loop itself +/// (the tests count them). +painted_ahead: u32 = 0, +painted_on_loop: u32 = 0, +/// The view the last paint was asked from, and which way it was going. +ahead_seen: struct { serial: u32 = 0, scroll: usize = 0, dir: i2 = 1 } = .{}, pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { s.cancelTask(); s.cancelParse(); + s.cancelPaint(); s.pattern.deinit(gpa); s.dir.deinit(gpa); } @@ -184,6 +237,96 @@ pub fn cancelParse(s: *Stream) void { t.job.deinit(); } +/// The paint's worker told to stop and joined. +pub fn cancelPaint(s: *Stream) void { + const t = s.paint orelse return; + s.paint = null; + t.job.stop.store(true, .release); + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.cancel(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); +} + +/// The painted stretch kept for pane `serial`'s text at `revision`, if it +/// covers bytes `need_start..need_end`: handed over (the caller owns it, +/// tree_sitter_gpa), with where it starts. +pub fn takeAhead(p: *Pardes, serial: u32, revision: u32, need_start: usize, need_end: usize) ?struct { styles: []u8, start: usize } { + const s = &p.stream; + const a = s.ahead_for; + if (s.ahead.len == 0 or a.serial != serial or a.revision != revision) return null; + if (need_start < a.start or need_end > a.start + s.ahead.len) return null; + defer s.ahead = &.{}; + return .{ .styles = s.ahead, .start = a.start }; +} + +/// Asks a worker to paint bytes `start..end` of pane `serial`'s text ahead +/// of its view, unless that is painted or being painted already. Only with +/// workers, and only for a grammar a worker can paint (syntax.readyApart). +pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, content: []const u8, from: usize, to: usize) void { + const s = &p.stream; + if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return; + const a = s.ahead_for; + if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return; + if (s.paint) |t| { + // One under way for the same text that reaches into the stretch + // asked for is let finish: held keys move the stretch every frame, + // and a paint restarted every frame would never land. + if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return; + s.cancelPaint(); + } + if (!syntax.readyApart(path)) return; + const job = p.gpa.create(PaintJob) catch return; + const path_copy = p.gpa.dupe(u8, path) catch { + p.gpa.destroy(job); + return; + }; + const source = p.gpa.dupe(u8, content[from..to]) catch { + p.gpa.free(path_copy); + p.gpa.destroy(job); + return; + }; + s.paint_count +%= 1; + const gen = paint_bit | (s.paint_count & ~paint_bit); + job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; + s.paint = .{ .job = job }; + p.emit(.{ .search = .{ .gen = gen } }); +} + +fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { + const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return; + if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return; +} + +/// A paint's answer: its task joined, its stretch kept for when the view +/// gets there, if the text is still what was painted. +fn receivePaint(p: *Pardes, b: *const Batch) void { + const s = &p.stream; + const painted = b.painted.?; + if (s.paint) |t| if (t.job.gen == b.gen) { + s.paint = null; + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.await(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); + }; + const id = p.paneBySerial(painted.serial) orelse return; + const f = p.panes[id].?.file orelse return; + if (f.revision != painted.revision or b.styles.len == 0) return; + const copy = p.tree_sitter_gpa.dupe(u8, b.styles) catch return; + if (s.ahead.len > 0) p.tree_sitter_gpa.free(s.ahead); + s.ahead = copy; + s.ahead_for = .{ .serial = painted.serial, .revision = painted.revision, .start = painted.start }; + // The next frame looks again: the view may be there already. + p.panes[id].?.file.?.syntax_dirty = true; +} + /// Pane `id`'s declarations asked for, as its text is now, unless a parse of /// that text is already on its way. With workers, on one; without (a test), /// here only for a small text, else not at all (the rows stay ungrouped). @@ -338,6 +481,20 @@ pub fn wake(s: *const Stream, next_frame: u64) ?u64 { /// inbox). Called by the shell for the `search` effect, on the loop. pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: Sink) void { const s = &p.stream; + // A paint's generation: its own task, its own worker. + if (gen & paint_bit != 0) { + if (s.paint) |*t| if (t.job.gen == gen and t.future == null) { + t.job.out = gpa; + t.future = io.concurrent(paintWork, .{ t.job, ctx, sink }) catch { + const job = t.job; + s.paint = null; + job.deinit(); + return; + }; + t.io = io; + }; + return; + } // A parse's generation: its own task. if (s.parse) |*t| if (gen == s.parse_gen and t.job.gen == gen and t.future == null) { t.job.out = gpa; @@ -424,6 +581,7 @@ const Collect = struct { pub fn receive(p: *Pardes, b: *const Batch) void { const s = &p.stream; if (b.parsed) |parsed| return receiveParse(p, b, parsed.serial, parsed.revision); + if (b.painted != null) return receivePaint(p, b); if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { s.dropped += 1; return; @@ -1079,6 +1237,49 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, try testing.expect(p.stream.parse == null); } +test "Stream: a scroll's colours are painted ahead on a worker, so PageDown after PageDown parses nothing on the frame" { + if (comptime !filesystem.platform_has_fs or !syntax.enabled) return; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..6000) |i| try text.print(testing.allocator, "fn f{d}() void {{\n _ = \"needle\";\n}}\n", .{i}); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + const first = p.stream.painted_on_loop; + for (0..20) |_| { + // The worker has had its time (a frame's work at a held key's pace + // is all it needs; here it is waited for, so the test is not a race). + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + 60_000; + while (p.stream.paint != null) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + shell.pump(p); + testing.io.sleep(.fromMilliseconds(1), .awake) catch {}; + } + p.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + // Coloured: the rows shown have their colours. + const f = &pane.file.?; + try testing.expect(f.highlights.len > 0 and f.highlight_start <= f.line_starts[f.scroll]); + } + try testing.expect(p.stream.painted_on_loop == first); + try testing.expect(p.stream.painted_ahead >= 5); +} + test "zz stream timings on the stress file with a worker (QUERY_TIMES=)" { const path_z = std.c.getenv("QUERY_TIMES") orelse return error.SkipZigTest; const path = std.mem.span(path_z); diff --git a/src/draw.zig b/src/draw.zig index 92fd8205..265e07c1 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -1971,7 +1971,7 @@ test "each Motion flavour reaches the lift as its own trajectory" { try std.testing.expect(peaks[2] <= 1.001 and peaks[3] > 1.25 and peaks[4] > peaks[3]); } -test "the focused cursor glides a jump, lands a step at once, and is exact at rest" { +test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { if (!builtins.capabilities.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); @@ -1983,12 +1983,17 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re var now: u64 = std.time.ns_per_s; p.advance(now); _ = try p.render(arena.allocator()); - // A step down: at once. + // A step down: eases there in a few frames. p.update(.{ .key = .{ .cp = 'j' } }); now += 16 * ms; p.advance(now); _ = arena.reset(.retain_capacity); var s = try p.render(arena.allocator()); + try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); + now += 100 * ms; + p.advance(now); + _ = arena.reset(.retain_capacity); + s = try p.render(arena.allocator()); try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); const before = s.cursor.?; // To the last line: it glides, a quad partway, and asks for frames. @@ -2023,7 +2028,7 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re // At rest the quad is the cursor's own cell. try std.testing.expectEqual(@as(f32, @floatFromInt(target.x)), s.chrome.cursor_quad[0]); try std.testing.expectEqual(@as(f32, @floatFromInt(target.y)), s.chrome.cursor_quad[1]); - // A mode change mid-glide lands at once: insert mode never trails. + // A mode change mid-glide: the thin caret glides on to the mark. p.update(.{ .key = .{ .cp = 'g' } }); p.update(.{ .key = .{ .cp = 'g' } }); now += 7 * ms; @@ -2036,7 +2041,17 @@ test "the focused cursor glides a jump, lands a step at once, and is exact at re p.advance(now); _ = arena.reset(.retain_capacity); s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); + try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); + try std.testing.expect(s.cursor.?.bar); + while (p.continuous()) { + now += 7 * ms; + p.advance(now); + _ = arena.reset(.retain_capacity); + s = try p.render(arena.allocator()); + } + // At rest, a bar's width at the caret's cell. + try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.x)), s.chrome.cursor_quad[0]); + try std.testing.expectApproxEqAbs(@as(f32, 1.0 / 8.0), s.chrome.cursor_quad[2] - s.chrome.cursor_quad[0], 1e-4); } test "a tag caret past its row's end glides to the tag's right edge, where the layer's is" { diff --git a/src/pardes.zig b/src/pardes.zig index df6d347f..9177721f 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4983,6 +4983,7 @@ pub const Pardes = struct { p.pipe.deinit(gpa); p.stream.deinit(gpa); p.handover.deinit(gpa); + if (p.stream.ahead.len > 0) p.tree_sitter_gpa.free(p.stream.ahead); p.locations_cache.deinit(p.tree_sitter_gpa); p.fs.deinit(gpa); p.shell_rows.reset(gpa); diff --git a/src/syntax.zig b/src/syntax.zig index aca2c3ff..821754fa 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -304,6 +304,55 @@ pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []c return styles; } +/// Whether `highlightApart` can colour `path` on a worker: a grammar with +/// no injections (markdown's and typst's reach the shared parsers), made +/// ready here, on the loop, so the worker only reads its query. +pub fn readyApart(path: []const u8) bool { + if (!enabled) return false; + const selected = (forExt(std.fs.path.extension(path)) catch return false) orelse return false; + return !std.mem.eql(u8, selected.name, "markdown") and !std.mem.eql(u8, selected.name, "typst"); +} + +/// `highlightFileRange` of `source` (a copy of the range), on a worker: a +/// parser and a query cursor of its own, the grammar's query shared (it +/// is only read); `halt` ends the parse early. After `readyApart`. +pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const u8, halt: *const std.atomic.Value(bool)) ![]u8 { + if (comptime !enabled) return &.{}; + const shared = (try forExt(std.fs.path.extension(path))) orelse return &.{}; + const parser = ts.Parser.create(); + defer parser.destroy(); + try parser.setLanguage(shared.lang); + const cursor = ts.QueryCursor.create(); + defer cursor.destroy(); + const Reader = struct { + text: []const u8, + fn read(payload: ?*anyopaque, byte: u32, _: ts.Point, n: *u32) callconv(.c) [*c]const u8 { + const r: *@This() = @ptrCast(@alignCast(payload.?)); + const at = @min(byte, r.text.len); + n.* = @intCast(r.text.len - at); + return r.text.ptr + at; + } + fn progress(state: *ts.Parser.State) callconv(.c) bool { + const flag: *const std.atomic.Value(bool) = @ptrCast(@alignCast(state.payload.?)); + return flag.load(.monotonic); + } + }; + var reader: Reader = .{ .text = source }; + const tree = parser.parseWithOptions( + .{ .payload = &reader, .read = Reader.read, .encoding = .utf8 }, + null, + .{ .payload = @constCast(halt), .progress_callback = @ptrCast(&Reader.progress) }, + ) orelse return error.Canceled; + defer tree.destroy(); + const styles = try gpa.alloc(u8, source.len); + @memset(styles, 0); + var own = shared; + own.parser = parser; + own.cursor = cursor; + runQuery(styles, own, tree, 0); + return styles; +} + /// Source spans are independent of editor rows. Locations output uses the same /// declarations as sticky editor headers. Byte ends are exclusive; rows are /// zero-based and inclusive, including the last signature/opening-brace row. -- cgit v1.3 From a9e33e52d789c7433da1da17a8c82093c9114243 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 15:23:45 -0300 Subject: anim 7: what the probe found: the paced wait spins (asleep, the CPU's clock fell and an input's frame took 2-5x as long), a glide measures the core's line count first and emits only the rows it shows, a far jump paints only its view, a long line 16 KB round the caret, the caret's sideways scroll measured from the view's edge, a paint never joined on the loop nor asked while the view stands still, rewritten generated panes do not glide, pictures decode off the loop --- src/File.zig | 101 ++++++++++++++++++++++++++++++++++++++++++++++++-------- src/Stream.zig | 49 ++++++++++++++++++--------- src/gui/gui.zig | 77 ++++++++++++++++++++++++++++-------------- src/image.zig | 45 ++++++++++++++++++++++--- src/panes.zig | 20 ++++++----- src/pardes.zig | 4 ++- src/syntax.zig | 1 + src/tty/tty.zig | 38 +++++++++++++++++---- 8 files changed, 262 insertions(+), 73 deletions(-) (limited to 'src/Stream.zig') diff --git a/src/File.zig b/src/File.zig index 798e3e84..6df49c98 100644 --- a/src/File.zig +++ b/src/File.zig @@ -26,6 +26,7 @@ const terminal = panes.terminal; const Text = panes.Text; const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2; +const long_paint: usize = 16 << 10; /// Content and primary selection at one file edit boundary. Keeping only the /// primary avoids putting Text.max_selections ranges in every history entry. @@ -1724,8 +1725,23 @@ pub fn refreshHighlights(p: *Pardes) void { f.syntax_dirty = false; continue; } - const need_start = lineStart(p.gpa, f, f.scroll); - const need_end = @max(need_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS)); + // ponytail: a stretch over `long_paint` bytes (a minified line) is + // painted only around the caret, so a key typed in a 500 KB line + // parses 16 KB, not the line (190 ms a key in ReleaseSafe; 25 ms at + // 64 KB, so 16, still eight views of 80x25); its first + // tokens may be coloured as if the line began there. The upgrade: an + // incremental parse of the whole text, edited in place. + const caret = @min(f.content.len, lineStart(p.gpa, f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col)))); + const around = struct { + fn clamp(a: usize, b: usize, at: usize) [2]usize { + if (b - a <= long_paint) return .{ a, b }; + const lo = std.math.clamp(at -| long_paint / 2, a, b - long_paint); + return .{ lo, lo + long_paint }; + } + }; + const need = around.clamp(lineStart(p.gpa, f, f.scroll), @max(lineStart(p.gpa, f, f.scroll), lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS)), caret); + const need_start = need[0]; + const need_end = need[1]; if ((!f.tree_context or (f.highlight_start == 0 and f.highlights.len == f.content.len)) and f.highlights.len > 0 and need_start >= f.highlight_start and need_end <= f.highlight_start + f.highlights.len) @@ -1734,7 +1750,11 @@ pub fn refreshHighlights(p: *Pardes) void { if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f); continue; } - const slack: usize = if (f.highlights.len == 0) 0 else pane.rows; + // A view far from what is painted (a jump: ge, a Look) paints only + // itself on the frame, a third of the work; the stretch around it + // comes painted ahead (paintAhead) where there are workers. + const far = f.highlights.len == 0 or need_end <= f.highlight_start or need_start >= f.highlight_start + f.highlights.len; + const slack: usize = if (far and p.host.vtable.search != null) 0 else if (f.highlights.len == 0) 0 else pane.rows; const whole = pane.colorAlgo() == .locations or f.tree_context; // Painted ahead on a worker (Stream.paintAhead): taken, no parse. if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| { @@ -1747,11 +1767,14 @@ pub fn refreshHighlights(p: *Pardes) void { continue; }; if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1; - const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); - const end = if (whole) + const wide_start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); + const wide_end = if (whole) f.content.len else - @max(start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack)); + @max(wide_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack)); + const window = if (whole) [2]usize{ wide_start, wide_end } else around.clamp(wide_start, wide_end, caret); + const start = window[0]; + const end = window[1]; const new_highlights = (switch (pane.colorAlgo()) { .none => @as([]u8, &.{}), .diff => syntax.highlightDiff(p.tree_sitter_gpa, f.content, start, end), @@ -1772,24 +1795,76 @@ pub fn refreshHighlights(p: *Pardes) void { } } -/// The stretch a scroll of `pane` is heading into, two views on the way it -/// last went, asked of a worker (Stream.paintAhead) so it is painted by the -/// time the view gets there. +/// The stretch a scroll of `pane` is heading into, the view and three more +/// the way it last went, asked of a worker (Stream.paintAhead) so it is +/// painted by the time the view gets there. From the view itself: two +/// views out, an upward stretch stopped short of the view's own bottom +/// rows, and a half page up (or down) missed it by a line and painted on +/// the frame, 11-17 ms in ReleaseSafe. fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { const seen = &p.stream.ahead_seen; - if (seen.serial == pane.serial and f.scroll != seen.scroll) seen.dir = if (f.scroll > seen.scroll) 1 else -1; + // A view standing still (typing in it) needs nothing ahead: each edit + // is a new revision, and a paint asked at every keystroke was a copy + // of the stretch and a worker's start on the loop per key. + if (seen.serial == pane.serial and f.scroll == seen.scroll) return; + if (seen.serial == pane.serial) seen.dir = if (f.scroll > seen.scroll) 1 else -1; if (seen.serial != pane.serial) seen.dir = 1; seen.serial = pane.serial; seen.scroll = f.scroll; const rows: usize = pane.rows; - const view = if (seen.dir > 0) f.scroll + 2 * rows else f.scroll -| 2 * rows; - const start = lineStart(p.gpa, f, view -| rows); - const end = @max(start, lineStart(p.gpa, f, view + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS)); + const start = lineStart(p.gpa, f, if (seen.dir > 0) f.scroll else f.scroll -| 3 * rows); + const end = @max(start, lineStart(p.gpa, f, f.scroll + (if (seen.dir > 0) 4 * rows else rows) + SYNTAX_CONTEXT_AFTER_ROWS)); // Painted already, as far as that goes. if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return; Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end); } +test "a long line is painted only around the caret" { + if (!syntax.enabled) return; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const line = try std.testing.allocator.alloc(u8, 600 << 10); + defer std.testing.allocator.free(line); + for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ','; + const pane = try p.setTestFile(line); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json"); + pane.body.cur_col = 300 << 10; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + const f = &pane.file.?; + try std.testing.expect(f.highlights.len == long_paint); + try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10); +} + +test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + p.settings.wrap = false; + const line = try std.testing.allocator.alloc(u8, 500 << 10); + defer std.testing.allocator.free(line); + @memset(line, 'a'); + const pane = try p.setTestFile(line); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + // Right, far: the caret ends a margin in from the right edge. + pane.body.cur_col = 400_000; + pane.ensureCursorVisible(); + const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane)); + const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2)); + try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll); + // Back left of the view: a margin in from the left edge. + pane.body.cur_col = 100_000; + pane.ensureCursorVisible(); + try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll); + // At the start: none. + pane.body.cur_col = 1; + pane.ensureCursorVisible(); + try std.testing.expectEqual(@as(i32, 0), pane.hscroll); +} + pub fn supportsContext(pane: *const Pane) bool { const file = pane.file orelse return false; return file.output == null and file.mini == null and syntax.supportsPath(file.path); diff --git a/src/Stream.zig b/src/Stream.zig index ace73ca4..e42d4a16 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -140,6 +140,7 @@ const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, /// a parse's (those count from 0 in `gen`, and it would take two billion /// searches to reach it). const paint_bit: u32 = 1 << 31; +const max_paint_bytes = 512 << 10; /// Where a worker hands a batch: false when it was turned away (the inbox /// closed, the wait for room canceled), and the worker stops. @@ -204,6 +205,10 @@ parse_ns: u64 = 0, /// The paint running (one at a time), its generation's count, and the /// painted stretch it gave, kept until the view gets there (tree_sitter_gpa). paint: ?PaintTask = null, +/// One told to stop and not waited for: joined when its answer comes (a +/// paint of a long line runs on in its query; waiting for it here held the +/// loop for half a second). No new paint starts while it is out. +paint_retired: ?PaintTask = null, paint_count: u32 = 0, ahead: []u8 = &.{}, ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, @@ -237,18 +242,20 @@ pub fn cancelParse(s: *Stream) void { t.job.deinit(); } -/// The paint's worker told to stop and joined. +/// The paints' workers told to stop and joined (the session's end). pub fn cancelPaint(s: *Stream) void { - const t = s.paint orelse return; - s.paint = null; - t.job.stop.store(true, .release); - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.cancel(t.io.?); - pardes.turn.back(); + for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| { + const t = slot.* orelse continue; + slot.* = null; + t.job.stop.store(true, .release); + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.cancel(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); } - t.job.deinit(); } /// The painted stretch kept for pane `serial`'s text at `revision`, if it @@ -271,12 +278,19 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return; const a = s.ahead_for; if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return; + // A stretch bigger than this (a few lines of a minified file) is not + // painted ahead: the loop paints what it shows of it. + if (to - from > max_paint_bytes) return; if (s.paint) |t| { // One under way for the same text that reaches into the stretch // asked for is let finish: held keys move the stretch every frame, // and a paint restarted every frame would never land. if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return; - s.cancelPaint(); + // Else it is told to stop and left to end on its own, once. + if (s.paint_retired != null) return; + t.job.stop.store(true, .release); + s.paint_retired = t; + s.paint = null; } if (!syntax.readyApart(path)) return; const job = p.gpa.create(PaintJob) catch return; @@ -297,8 +311,10 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont } fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { - const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return; - if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return; + // Stopped or failed, it still answers (with nothing), so the loop + // knows to join it. + const styles: []u8 = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]u8{}); + _ = sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done }); } /// A paint's answer: its task joined, its stretch kept for when the view @@ -306,8 +322,9 @@ fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { fn receivePaint(p: *Pardes, b: *const Batch) void { const s = &p.stream; const painted = b.painted.?; - if (s.paint) |t| if (t.job.gen == b.gen) { - s.paint = null; + // Its task joined: it has sent its last, so the join is a return away. + for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) { + slot.* = null; if (t.future) |f| { pardes.turn.yield(); var future = f; @@ -315,6 +332,8 @@ fn receivePaint(p: *Pardes, b: *const Batch) void { pardes.turn.back(); } t.job.deinit(); + // A retired one's answer is for a stretch nobody wants now. + if (slot == &s.paint_retired) return; }; const id = p.paneBySerial(painted.serial) orelse return; const f = p.panes[id].?.file orelse return; diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 1a56ecd0..5d3cbafd 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4126,8 +4126,8 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // tick, skipping (never bunching) the ticks a slow frame missed. tick = if (g.pace_ns != 0 and now -| g.pace_ns < 2 * period) g.pace_ns + period else now; if (tick < now) tick += (now - tick) / period * period; - const left: c_int = @intCast(@min(std.math.maxInt(c_int), (tick -| now) / std.time.ns_per_ms)); - ms = if (ms < 0) left else @min(ms, left); + // Waited out polling, below. + ms = 0; } g.input_seen = false; // The wait is the 9P connections' turn with the core. @@ -4177,16 +4177,25 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // never adds a tick to an input's latency), and the ticks after it // run from it. if (paced and g.input_seen) tick = c.SDL_GetTicksNS(); + // The tick is waited out polling, not asleep, as a game's frame + // limiter spins: asleep between 60 Hz frames, the CPU's clock fell + // (intel_pstate, 4.9 GHz down toward 0.8) and every frame, an + // input's first among them, took 2-5x as long: input-to-present p50 + // 1.9 ms spinning against 7-8 ms asleep (docs/next-anim.md). + // ponytail: a core while something moves (~250 ms a scroll); a + // sleep to a millisecond short of the tick if the power matters more. while (paced and !g.input_seen) { - const left = tick -| c.SDL_GetTicksNS(); - if (left < std.time.ns_per_ms) break; - const wait_ms: c_int = @intCast(@min(left / std.time.ns_per_ms, @as(u64, @intCast(demoDue(g) orelse std.math.maxInt(c_int))))); + if (tick -| c.SDL_GetTicksNS() < 50 * std.time.ns_per_us) break; pardes.turn.rest(); - const more = c.SDL_WaitEventTimeout(&sev, wait_ms); + const more = c.SDL_PollEvent(&sev); pardes.turn.wake(); if (more) { dispatch(g, &in, &sev); while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev); + } else { + // The events asked for every 100 µs, the core busy between. + const until = c.SDL_GetTicksNS() + 100 * std.time.ns_per_us; + while (c.SDL_GetTicksNS() < @min(until, tick)) std.atomic.spinLoopHint(); } runDemo(g, &in, core); if (g.input_seen) tick = c.SDL_GetTicksNS(); @@ -5071,7 +5080,7 @@ const Glide = struct { /// whatever moved it. /// When the picture was last sampled (sample). shown_ns: u64 = 0, - seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0 } = @splat(.{}), + seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}), /// How far the picture may lag, in bodies: the rows kept past each /// edge, so a jump made while one glides carries on from where it is. @@ -5152,23 +5161,31 @@ const Glide = struct { } }; // The least shift under which the rows agree, but for a few: the - // cursor's line and its number's weight move with the scroll. - var shift: u16 = found: for (1..h) |k| { - const kk: u16 = @intCast(k); - const allowed = @max(2, (h - kk) / 8); - var misses: usize = 0; - for (0..h - kk) |r| { - const rr: u16 = @intCast(r); - const old_row = glide.before[@as(usize, if (glide.dir > 0) rr + kk else rr) * w ..][0..w]; - const new_row = now.row(surface, x0, glide.body_y + if (glide.dir > 0) rr else rr + kk, w); - if (!sameRow(old_row, new_row)) { - misses += 1; - if (misses > allowed) continue :found; + // cursor's line and its number's weight move with the scroll. The + // lines the core moved are tried first: unwrapped, they are the + // rows, and one pass over the body settles it (the search over + // every shift was 6 ms of the first frame of a page in Debug). + const agrees = struct { + fn at(gl: *const Glide, surf: *const pardes.Surface, x: u16, width: u16, height: u16, kk: u16) bool { + if (kk == 0 or kk + 3 > height) return false; + const allowed = @max(2, (height - kk) / 8); + var misses: usize = 0; + for (0..height - kk) |r| { + const rr: u16 = @intCast(r); + const old_row = gl.before[@as(usize, if (gl.dir > 0) rr + kk else rr) * width ..][0..width]; + const new_row = now.row(surf, x, gl.body_y + if (gl.dir > 0) rr else rr + kk, width); + if (!sameRow(old_row, new_row)) { + misses += 1; + if (misses > allowed) return false; + } } + return true; } - // Too few rows left to tell: none in common. - if (h - kk < 3) break :found 0; - break :found kk; + }; + const guess: u16 = @intFromFloat(@min(glide.pending_lines, @as(f32, @floatFromInt(h)))); + // A body's lines or more: every row moved off, none in common. + var shift: u16 = if (guess >= h) 0 else if (agrees.at(glide, surface, x0, w, h, guess)) guess else found: for (1..h) |k| { + if (agrees.at(glide, surface, x0, w, h, @intCast(k))) break :found @intCast(k); } else 0; // None in common: a body's move from the last frame's body, if the // lines moved are that many (rows that merely changed, a small @@ -5285,9 +5302,14 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void { }; const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) pardes.panes.terminal.gridOffset(pane) else 0; const file: u64 = if (pane.file) |f| std.hash.Wyhash.hash(0, f.path) else 0; + const revision: u32 = if (pane.file) |f| f.revision else 0; const was = glide.seen[id]; - glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file }; + glide.seen[id] = .{ .serial = pane.serial, .line = line, .file = file, .revision = revision }; if (was.serial != pane.serial or was.file != file or was.line == line or pane.collapsed or pane.hasPdf()) continue; + // Generated text rewritten as its view moved (a query pane's live + // rows, a +Search refilled, Callstack): the rows are another + // text's, not this one's moved; it lands. + if (pane.file) |f| if (f.output != null and revision != was.revision) continue; if (pane.file == null and wheeled != id) continue; if (g.scroll_pane == id and (g.scroll_lag != 0 or g.scroll_delta != 0)) continue; const moved = line - was.line; @@ -5350,8 +5372,13 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde const cursor_idx: u32 = if (surface.cursor) |cu| @as(u32, cu.y) * surface.cols + cu.x else std.math.maxInt(u32); const cursor_bar = if (surface.cursor) |cu| cu.bar else false; var n: u32 = 0; - var row = y0 + kept.rows; - while (row < y0 + bh) : (row += 1) { + // Only the rows the scissor shows: lagging a body or more, every body + // row is past the edge, and drawing them all was twice the frame's + // instances for the length of a long glide. + const lag: u16 = @intFromFloat(@min(@floor(@abs(glide.offset)), @as(f32, @floatFromInt(bh)))); + var row = y0 + kept.rows + if (glide.offset > 0) lag else 0; + const row_end = y0 + bh - if (glide.offset < 0) lag else 0; + while (row < row_end) : (row += 1) { const line = surface.cells[@as(usize, row) * surface.cols ..][0..surface.cols]; const row_cursor: ?usize = if (surface.cursor) |cu| if (cu.y == row) cu.x else null else null; var col = x0; diff --git a/src/image.zig b/src/image.zig index 5797ce9e..19dbb9e4 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,5 +1,6 @@ //! Image decoding, pixel placement, and glyph approximation. const std = @import("std"); +const builtin = @import("builtin"); const zstbi = @import("zstbi"); /// The 16-colour ANSI table below is the only thing this file ever wanted from /// the emulator, and a build without one still renders images — see @@ -767,7 +768,9 @@ pub fn readable(bytes: []const u8) bool { /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the /// pane then simply shows a blank body. -pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?struct { rgba: []u8, w: usize, h: usize } { +const Decoded = struct { rgba: []u8, w: usize, h: usize }; + +pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null; // Bounded before decoding: a few KB of PNG can name gigapixels. var iw: c_int = 0; @@ -812,8 +815,14 @@ pub const State = struct { raw: []u8 = &.{}, grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, grid_key: GridKey = .{}, + /// The decode under way off the loop (ensureDecoded), and its answer. + decoder: ?std.Thread = null, + decoded: std.atomic.Value(bool) = .init(false), + answer: ?Decoded = null, pub fn deinit(state: *State, gpa: std.mem.Allocator) void { + if (state.decoder) |t| t.join(); + if (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); if (state.raw.len > 0) gpa.free(state.raw); @@ -941,15 +950,41 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![ ); } +/// The picture decoded off the loop: the first draw starts a thread and +/// draws the pane without it (its tag and frame at once); the frames after +/// it look (Pardes.nextWake asks for them) and the picture lands on the +/// first one after the decode (80 ms in ReleaseSafe, 290 in Debug, for the +/// docs' screenshot: all of it once on the frame before). +/// ponytail: a thread a picture; a pool if many open at once. fn ensureDecoded(p: *pardes.Pardes, state: *State) void { - if (state.tried) return; - state.tried = true; - if (state.raw.len == 0) return; // nothing was readable at open - if (decode(p.image_gpa, state.raw)) |decoded| { + if (state.decoder) |t| { + if (!state.decoded.load(.acquire)) return; + t.join(); + state.decoder = null; + } + if (state.answer) |decoded| { + state.answer = null; state.rgba = decoded.rgba; state.iw = decoded.w; state.ih = decoded.h; } + if (state.tried) return; + state.tried = true; + if (state.raw.len == 0) return; // nothing was readable at open + const Work = struct { + fn run(gpa: std.mem.Allocator, st: *State) void { + st.answer = decode(gpa, st.raw); + st.decoded.store(true, .release); + } + }; + if (comptime pardes.hosted and !builtin.is_test) { + if (std.Thread.spawn(.{}, Work.run, .{ p.image_gpa, state })) |t| { + state.decoder = t; + return; + } else |_| {} + } + Work.run(p.image_gpa, state); + ensureDecoded(p, state); } fn ensureGrid(p: *pardes.Pardes, state: *State, cols: u16, rows: u16) void { diff --git a/src/panes.zig b/src/panes.zig index 20618ccc..51710d08 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -619,14 +619,18 @@ pub const Pane = struct { const line = modal.lineSlice(f.content, @intCast(@max(0, pane.body.cur_row))); const raw_cur: usize = @intCast(@max(0, pane.body.cur_col)); const raw_scroll: usize = @intCast(@max(0, pane.hscroll)); - const cur = @as(i32, @intCast(File.rawDisplayCol(line, raw_cur))); - const visual_scroll = @as(i32, @intCast(File.rawDisplayCol(line, raw_scroll))); - var target = visual_scroll; - if (cur < visual_scroll + hmargin) - target = @max(0, cur - hmargin) - else if (cur > visual_scroll + w - 1 - hmargin) - target = cur - (w - 1 - hmargin); - if (target != visual_scroll) pane.hscroll = @intCast(File.rawAtDisplay(line, @intCast(target))); + // Measured from the view's left edge to the caret, never from the + // line's start: a 500 KB line was walked from byte 0 at every key + // (widths add, so the span alone is exact). + const apart = File.lineDisplayOffset(line, raw_scroll, raw_cur); + const back: ?i32 = if (apart < hmargin) hmargin else if (apart > w - 1 - hmargin) w - 1 - hmargin else null; + if (back) |cols| { + // The byte `cols` columns before the caret, looked for from a + // few bytes a column back (a grapheme is at most 4 a column). + const from = raw_cur -| @as(usize, @intCast(cols)) * 4; + const ahead = File.lineDisplayOffset(line, from, raw_cur) - cols; + pane.hscroll = @intCast(if (ahead <= 0) from else File.byteAtDisplayFrom(line, from, @intCast(ahead))); + } } } diff --git a/src/pardes.zig b/src/pardes.zig index 9177721f..0f3f87f2 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7696,8 +7696,10 @@ pub const Pardes = struct { // seen are stamped 0, which `advance` would count back from). if (p.fs.batch.bytes.items.len > 0) return @max(next, p.fs.batch.last_ns + ctlfs.pane.batch_pause_ns); if (p.chrome_animation.isActive() or p.presentation.animating()) return next; - // Places still to find resume on the next frame. + // Places still to find resume on the next frame; a picture being + // decoded is looked for. for (p.panes) |slot| if (slot) |pane| if (pane.places.busy(pane)) return next; + for (p.panes) |slot| if (slot) |pane| if (pane.image) |iv| if (iv.decoder != null) return next; // A markdown text's tables still being read from the top (Tables.zig). for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.tables.busy()) return next; if (panes.Query.busy(p)) return next; diff --git a/src/syntax.zig b/src/syntax.zig index 821754fa..985a9888 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -344,6 +344,7 @@ pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const .{ .payload = @constCast(halt), .progress_callback = @ptrCast(&Reader.progress) }, ) orelse return error.Canceled; defer tree.destroy(); + if (halt.load(.monotonic)) return error.Canceled; const styles = try gpa.alloc(u8, source.len); @memset(styles, 0); var own = shared; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 264cfbef..b7bb2d54 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1521,8 +1521,10 @@ const Shell = struct { /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the /// running-out wait moves it: the core's next wake. test_clock: ?u64 = null, - /// The tick the last paced frame was due at (waitInput). + /// The tick the last paced frame was due at (waitInput), and whether + /// this wait read an input (its frame is drawn at once). pace_ns: u64 = 0, + input_seen: bool = false, wake_to: u64 = 0, fn of(ctx: ?*anyopaque) *Shell { @@ -1574,7 +1576,6 @@ const Shell = struct { fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = of(ctx); - var batch: usize = 0; // The wait is the 9P connections' turn with the core. A timeout (the // core's next wake) arrives through the same queue (`wakeWatch`). // While anything moves, a frame a tick (animation.frame_ns) on a grid @@ -1583,14 +1584,32 @@ const Shell = struct { // ends the wait at once and its frame is drawn then; the ticks after // it run from it. var wait = timeout_ms; - if (s.test_clock == null and s.core.continuous()) { + const paced = s.test_clock == null and s.core.continuous(); + var tick: u64 = 0; + if (paced) { const period = pardes.animation.frame_ns; const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - var tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now; + tick = if (s.pace_ns != 0 and now -| s.pace_ns < 2 * period) s.pace_ns + period else now; if (tick < now) tick += (now - tick) / period * period; s.pace_ns = tick; wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms)); } + s.input_seen = false; + // Paced, what is not an input (a frame's ack, a pty's output) is + // taken in and the wait goes on to the tick: the acks alone ran the + // loop at the terminal's round trip. + while (true) { + s.waitOnce(wait); + if (!paced or s.input_seen or s.core.quit) break; + const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (now + std.time.ns_per_ms > tick) break; + wait = @intCast((tick - now) / std.time.ns_per_ms); + } + if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + } + + fn waitOnce(s: *Shell, wait: u32) void { + var batch: usize = 0; if (wait != 0) { s.wake_to = s.core.nextWake() orelse 0; requestWake(s.io, wait); @@ -1638,8 +1657,15 @@ const Shell = struct { // A key's or a click's time, for its latency to the frame that // answers it (FrameStats): when the loop takes it in. switch (event) { - .key_press => core.frames.input(pardes.FrameStats.nowNs()), - .mouse => |m| if (m.type == .press) core.frames.input(pardes.FrameStats.nowNs()), + .key_press => { + core.frames.input(pardes.FrameStats.nowNs()); + s.input_seen = true; + }, + .mouse => |m| if (m.type == .press) { + core.frames.input(pardes.FrameStats.nowNs()); + s.input_seen = true; + }, + .paste, .paste_start, .winsize => s.input_seen = true, else => {}, } switch (event) { -- cgit v1.3 From 685b4050bb0caaa95c4c6a209e69f7777469e9fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 25 Oct 2026 11:40:05 -0300 Subject: query: a row is its place as text, `path:line:col` then its code, the path the shortest a Look from the query pane resolves (Query.lookPath: relative, `../` included, when shorter, else absolute, never `@pN` but for a text with no file), aligned per group, the declaration headers `path:line`; its code coloured as its file's language colours it, the file painted whole on the worker (Stream.paintRows, highlightApart, kept in the locations cache, Query.Shades a walk a refill, Query.recolor in place as each lands) with the match tinted over it; the +Preview's places are the query's hits in its file (a Grep searched, a list fed), tinted and ticked, the row's one current and selected, as in a pane that already shows it; tests for B3 on an address, a saved list's Looks, the shortest path, the preview's places and colours, the worker's colours; fsearch, grep, lookrange, lsp-client, lsprelpath (now `../shade.zig`), stepgrain, tagbottom goldens: the address rows; docs/next-searchrows.md: the design and the numbers Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016u3hBPFfja5pK4EBRC1UVD --- docs/next-searchrows.md | 89 ++++++++ docs/search-proto-converge.md | 48 ++++- docs/typ/guide.typ | 9 +- src/Query.zig | 443 ++++++++++++++++++++++++++++++++++----- src/Stream.zig | 272 +++++++++++++++++++++++- src/locations.zig | 34 +++ src/ninep/ctl.zig | 5 +- test/snapshots/fsearch.golden | 12 +- test/snapshots/grep.golden | 2 +- test/snapshots/lookrange.golden | 16 +- test/snapshots/lsp-client.golden | 4 +- test/snapshots/lsprelpath.golden | 22 +- test/snapshots/lsprelpath.snap | 23 +- test/snapshots/stepgrain.golden | 12 +- test/snapshots/tagbottom.golden | 4 +- 15 files changed, 877 insertions(+), 118 deletions(-) create mode 100644 docs/next-searchrows.md (limited to 'src/Stream.zig') diff --git a/docs/next-searchrows.md b/docs/next-searchrows.md new file mode 100644 index 00000000..79ffc444 --- /dev/null +++ b/docs/next-searchrows.md @@ -0,0 +1,89 @@ +# searchrows: query rows that are addresses, coloured as their files + +The author's ask (2026-10-25): the search results "should show Lookable +locations for the matches, and it should highlight the near text on the +preview/result pane", then "using treesitter for the highlight I mean", +then on the row format: "the row can be just path:line:row but the path +should be minimal i.e. it should be the smallest one that doesn't refer to +panes like @p1 but that resolves a Look". Change `tsyvmpux`, on +`zmluwpuv`. Positions, not a verdict. + +## The row + + Places.zig:163:8 pub fn firstFrom(hits: []const Hit, at: usize) usize { + Places.zig:237:19 const i = firstFrom(set.hits.items, at); + ../shade.zig:1:11 pub const Shade = enum { + +- `path:line:col`, then the line's code from its first non-blank: plain + text, so B3 anywhere on the address is a Look as anywhere, a sweep copies + it, and a frozen pane saved is a list of addresses that still work. +- The path is `Query.lookPath(buf, path, dir)`: the shortest text a Look + from the query pane resolves: relative to the pane's directory (`x`, + `sub/x`, `../x`) when strictly shorter, else absolute. A relative name a + Look would read as something else (`@p...`, `://`) stays absolute. One + function, used by every hit row and by the file and declaration headers + of Grep, `/`/All and the language server lists. +- On every row, `/` in the owner included: a bare `:12:5` would be looked + up in the +Search itself (a Look resolves against the pane it is in) and + a saved list would lose its file. +- A group's code starts in one column: the address padded to + `path + ':' + digits(longest line in the group) + ':' + 3`, then two + spaces (a column of four digits or more pushes its own row one cell). +- Declaration headers carry `path:line` in the same column; file headers + stay the bare path (itself a Look). + +## Colours + +- Rows: `Stream.paintRows` paints a whole file on the worker + (`syntax.highlightApart`, the same call as the anim paint-ahead): the text + of the pane that shows it, else read from disk there. It lands in the + locations cache beside Callstack's (`locations.Cache.put`); each row takes + its line's slice when the line still reads as the row does + (`Query.Shades`, one walk down each file a refill). Until then rows are + plain; on arrival `Query.recolor` colours every query pane's plain rows + in place and asks for the next file: one at a time, each (path, revision) + once a Grep (a 256 ring). The match tint (`decorateRow`) sits on top, its + position past the address (`code_start`). Not on the loop, not in a 9P + write (asked again on the next step), not without workers. +- The +Preview: colours are its own pane's (painted as any file pane), and + the query's hits in that file are its places (`Places.search` literal for + a Grep, `Places.feed` from the rows for a list), tinted and ticked, the + current row's one current and selected, centred. A pane that already + shows the file gets the same (`selectPlace`). + +## Numbers + +`zz grep rows timings` (src/Stream.zig, `GREP_TIMES=`): Debug, a +TestShell worker, 160x50, the stress file (src/layout.zig x17, 1.22 MB, +29002 lines), typing `pane` three times with 400 ms of frames after each +key, then Enter on 2000 rows. Load 2.4-3.0 before, 2.7-2.8 after; after is +three runs. + +| frame work | before | after | +|---|---|---| +| keystroke (update + render) p50 / p90 / max | 2.2 / 4.2 / 6.0 ms | 2.0-2.1 / 2.9-6.5 / 4.4-6.7 ms | +| refill frame (rows remade) p50 / p90 / max | 4.4 / 5.2 / 5.2 ms | 4.8-5.1 / 5.4-6.1 / 5.4-6.1 ms | +| other frames p50 / p90 / max | 0.9 / 1.3 / 10.6 ms | 0.9 / 1.2-1.3 / 10.0-10.2 ms | +| Enter (2000 rows written) | 27.0 ms | 28.2-28.7 ms | +| frames after Enter p50 / max | 0.9 / 1.7 ms | 0.9 / 1.3-1.7 ms | + +Rows coloured after Enter: 1718 of 2000; the other 282 are plain in the +file's own colours too (checked against `highlightApart` of the file). A +refill costs ~0.4 ms more (the wider rows and the Shades walk), inside the +noise of the keystroke column; the paint itself never shows on the loop. + +## Pushback and rough edges + +- The current row's address is barely legible: the address's dim location + colour on the current row's mark tint (visible in the screenshot). The + thing B3 aims at should read; a brighter location fg on the marked row is + a one-line theme decision I left to you. +- A text with no file (a +New scratch) has no path any Look resolves, so + its `/` rows keep `@pN`, the only address that works there. +- `lsprelpath` asserted the opposite policy ("must NOT become + `../shade.zig`"); the author's shortest-path rule wins, the script says so. +- Rows of a file over 4 MB, and of markdown/typst (their injections share + parsers, so no worker paints them) stay plain; a disk file changed after + it was painted keeps plain rows for its changed lines until the next Grep. +- One file painted at a time: a Grep over hundreds of files colours file + by file (each lands in a frame); a worker per file if that is slow. diff --git a/docs/search-proto-converge.md b/docs/search-proto-converge.md index 99adbc2c..7a3e361a 100644 --- a/docs/search-proto-converge.md +++ b/docs/search-proto-converge.md @@ -40,13 +40,40 @@ only the prompts that are not search. `Search ` is the builtin under the three argument paths (`config.leader_args`, pick's table). -**Rows look like Callstack's**: a gutter line number, the hit's line with -the match tinted (every match on a line tinted, one row a line), under the -declaration it is in (once a parse exists, see below), under the file -(Grep, the LSP lists), under `@pN name (count)` in serial order (All). -B3 on a row's line number looks at its address; B3 on its code is a word -Look as anywhere; B2 on line 1 runs it again. The pane has no gutter of its -own, and its rows are read-only while live (queryline's clampEdit). +**Rows look like Callstack's**: each row's place first, as text, +`path:line:col`, the path the shortest a Look from the query pane +resolves (relative to its directory, `../` included, when that is shorter, +else absolute; `Query.lookPath`), never an `@pN` but for a text with no +file, padded so a group's code starts in one column, then +the hit's line coloured as its file's language colours it, the match +tinted over that (every match on a line tinted, one row a line); under the +declaration it is in (once a parse exists, see below; the header has its +`path:line` too), under the file (Grep, the LSP lists), under `@pN name +(count)` in serial order (All). B3 on the address is a Look as anywhere, a +sweep copies it, and a frozen pane saved is a list of addresses that still +work; B3 on its code is a word Look; B2 on line 1 runs it again. The pane +has no gutter of its own, and its rows are read-only while live +(queryline's clampEdit). The address is on every row, `/`'s included: +a bare `:12:5` would be looked up in the +Search itself (a Look resolves +against the pane it is in), and a saved list would lose its file. + +The rows' colours are their files' whole (`Stream.paintRows`): a file is +painted once on the worker (`syntax.highlightApart`, the text of the pane +that shows it, else read there from disk), kept in the locations cache +beside Callstack's, and each row takes its line's slice when the line +still reads as the row does (`Query.Shades`, one walk down each file a +refill). Until then the rows are plain; when a file lands, every query +pane's plain rows from it are coloured in place (`Query.recolor`), which +asks for the next file, one at a time. Without workers (tests) the rows +stay plain. + +**The +Preview is the file as its own pane shows it**: its colours are +the file's (`colorAlgo` `.source`, painted ahead as any pane), and the +query's hits in it are its places (a Grep's searched there as in an open +pane, a list's fed from its rows), tinted and ticked, the current row's +one current and selected, centred with its neighbours round it. A pane +that already shows the file gets the same: its places, the row's one +selected. **Keys** on line 1, typing: Ctrl-n / Ctrl-p (and Down / Up, `config.query_next` / `query_prev`) step the current row; Enter lands; Esc cancels. Tab stays the @@ -197,9 +224,10 @@ of 100 (the Debug SafeAllocator's stack capture per allocation was 30% at - LookNext / LookPrev do not step query rows (their addresses are in row metadata, not text). -- Grep rows carry no syntax colours; `/` rows are coloured where the - owner's highlights cover them (zeros elsewhere, so no fragment parse per - refill). +- Rows of a file bigger than 4 MB, of markdown and typst (their + injections share parsers, so no worker paints them) stay plain; a disk + file changed after it was painted keeps plain rows for its changed + lines until the next Grep. - Rail ticks are not carried over the detached wire (a detached frontend shows tints, not ticks). - In a window too short for the group (4 rows), only the query pane's tag diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index 61ba5455..58e5d907 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -175,7 +175,7 @@ is born #word("SearchRows") rows tall and never resizes itself; nothing found is a count of `0` in its tag, not an exit. #pairs( - [#key("/") `vw`], [every match in the text tinted and ticked on its scroll rail, the first after the cursor selected; the rows under line 1 are the lines holding it, a line number in the gutter, each under the declaration it is in], + [#key("/") `vw`], [every match in the text tinted and ticked on its scroll rail, the first after the cursor selected; the rows under line 1 are the lines holding it, each its place as text first (`file:line:col`, the shortest path a #word("Look") from the pane resolves), then the line coloured as the file is, under the declaration it is in], [#keys("Ctrl-n", "Ctrl-p")], [the next or previous match, typing; the text follows it], [#key("Enter")], [the keyboard on the match, the pattern in `/`, the rows kept as text (#word("Save") keeps them as a file)], [#keys("n", "N")], [the next or previous match in the text, round the end without a word], @@ -202,8 +202,11 @@ typing, #keys("n", "N") and #keys("j", "k") in the pane's normal mode); #btn("B1") on the rows places the caret and selects, as in any pane, and moves nothing else. #key("Enter") lands the current row, making the preview that file's pane where it stands. -#btn("B3") on a row's line number looks at the row; #btn("B2") on line 1 -runs it again. The next search from the same pane reuses its query pane, +The preview colours the file as its own pane does, and the query's hits +in it are its places, tinted and ticked, the current one selected. +#btn("B3") on a row's place goes there, as on any address; the rows kept +as text (#word("Save")) are a list of places that still work. +#btn("B2") on line 1 runs it again. The next search from the same pane reuses its query pane, its kept rows back on #key("Esc") (#word("SearchPane") `new`: a pane each; #word("SearchPlace") `column`: in a finder column at the right). A PDF's and a terminal's #key("/") still ask on the message row and list a diff --git a/src/Query.zig b/src/Query.zig index 11db8579..09b46f3b 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -10,11 +10,12 @@ //! matched fuzzy (Names.zig), text by regex. Editing the word changes //! the source. //! -//! The rows look like a Callstack's: the hit line with the match tinted -//! under a gutter line number, under the declaration it is in (when the -//! owner's parse exists: never made here on the loop), under its file -//! (Grep) or under `@pN name (count)` (All). They are addresses, all text: -//! B3 on one looks, B2 on line 1 runs it again. Wherever a row points into +//! The rows look like a Callstack's: the hit's place as text, +//! `path:line:col`, then its line coloured as its file is (painted on a +//! worker, Shades) with the match tinted, under the declaration it is in +//! (when the owner's parse exists: never made here on the loop), under its +//! file (Grep) or under `@pN name (count)` (All). They are addresses, all +//! text: B3 on one looks, B2 on line 1 runs it again. Wherever a row points into //! an open pane's text, that pane's places (Places.zig) show the set: //! tints, rail ticks, the current one selected. A row pointing at a file //! no pane shows is shown in the +Preview attached under this pane; Enter @@ -399,6 +400,10 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved { /// on a tick. Nothing here is synchronous with an edit of the owner: its /// places shift (Places.shift) and the rows follow once it stands a frame. pub fn produceAll(p: *Pardes, tick: bool, keyed: bool) void { + if (p.stream.rows_later) { + p.stream.rows_later = false; + recolor(p); + } for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; if (pane.query == null) continue; @@ -735,7 +740,7 @@ fn setRows(p: *Pardes, rp: *Pane, out: *std.ArrayList(u8), rows: *std.ArrayList( } else { locations.freeRows(p.gpa, f.location_rows); f.location_rows = meta; - f.syntax_dirty = true; + dropHighlights(p, f); } } else { File.setContent(p, f, content); @@ -758,41 +763,127 @@ fn gpa_free_pager(p: *Pardes, f: *File.State) void { f.pager_spans = &.{}; } -/// The gutter a file of `nlines` lines numbers its rows in: two spaces, -/// the number right-aligned, two spaces. -fn gutterWidth(nlines: usize) usize { +/// The room a group of rows gives its addresses, so that their code +/// starts in one column: the path, a colon, its longest line number, a +/// colon, three digits of column (a longer one pushes its code on a cell). +fn addressWidth(rel: []const u8, max_line: usize) usize { var digits: usize = 1; - var n = nlines; + var n = max_line; while (n >= 10) : (n /= 10) digits += 1; - return 2 + @max(3, digits) + 2; + return rel.len + 1 + digits + 1 + 3; } -fn writeHitRow(p: *Pardes, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row), width: usize, path: []const u8, line_no: usize, whole: []const u8, col: usize, end: usize, shade: []const u8) !void { +/// One hit row: its place as text first, `rel:line:col` (`rel` the path +/// as Look finds it from the query pane's directory), so that B3 on it is a +/// Look as anywhere, a sweep copies it and a saved list is a list of +/// addresses; padded to `width`, then two spaces and the line's code from +/// its first non-blank. Coloured as its file is: `shade`, the line's +/// colours when its pane has them painted, else the whole file's from the +/// worker (Shades). +fn writeHitRow(p: *Pardes, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row), sh: *Shades, width: usize, rel: []const u8, path: []const u8, line_no: usize, whole: []const u8, col: usize, end: usize, shade: []const u8) !void { var lead: usize = 0; while (lead < whole.len and (whole[lead] == ' ' or whole[lead] == '\t')) lead += 1; const code = std.mem.trimEnd(u8, whole[lead..], "\r"); - var buf: [4096 + 32]u8 = undefined; - var num: [24]u8 = undefined; - const number = std.fmt.bufPrint(&num, "{d}", .{line_no}) catch ""; - const pad = width -| 2 -| number.len; - if (pad + number.len + 2 + code.len > buf.len) return; - @memset(buf[0..pad], ' '); - @memcpy(buf[pad..][0..number.len], number); - @memcpy(buf[pad + number.len ..][0..2], " "); - @memcpy(buf[pad + number.len + 2 ..][0..code.len], code); - const text = buf[0 .. pad + number.len + 2 + code.len]; - const gutter = pad + number.len + 2; - try writeRow(p, out, rows, text, .{ + var buf: [8192]u8 = undefined; + const address = std.fmt.bufPrint(&buf, "{s}:{d}:{d}", .{ rel, line_no, col + 1 }) catch return; + const gutter = @max(width, address.len) + 2; + if (gutter + code.len > buf.len) return; + @memset(buf[address.len..gutter], ' '); + @memcpy(buf[gutter..][0..code.len], code); + try writeRow(p, out, rows, buf[0 .. gutter + code.len], .{ .kind = .match, .source_column = lead, .path = @constCast(path), .at = .{ .line = line_no, .col = col + 1, .end_line = line_no, .end_col = @max(col + 1, end) }, - .location_end = gutter, + .location_end = address.len, .code_start = gutter, - .colors = @constCast(if (shade.len >= lead) shade[lead..][0..@min(shade.len - lead, code.len)] else &.{}), + .colors = @constCast(if (shade.len >= lead + code.len) shade[lead..][0..code.len] else sh.of(p, path, line_no, code, lead)), }); } +/// Rows' syntax colours from their file's whole (as Callstack's rows are), +/// kept in the locations cache once a worker has painted it +/// (Stream.paintRows): the line found going down the file a row at a time, +/// so a refill walks each file once. A file not painted, or whose line no +/// longer reads as the row does, is asked for (once a refill) and its rows +/// are plain till it lands (`recolor`). +const Shades = struct { + path: []const u8 = "", + source: []const u8 = "", + colors: []const u8 = &.{}, + /// The line (1-based) that starts at `at` in `source`. + line: usize = 1, + at: usize = 0, + asked: bool = false, + + /// The colours of `code`, which is line `line_no` of `path` from byte + /// `column` on, or none. + fn of(sh: *Shades, p: *Pardes, path: []const u8, line_no: usize, code: []const u8, column: usize) []const u8 { + if (path.len == 0 or line_no == 0 or code.len == 0) return &.{}; + if (!std.mem.eql(u8, path, sh.path)) { + // its own copy: a caller's path is often a buffer it reuses + sh.* = .{ .path = p.scratch.allocator().dupe(u8, path) catch return &.{} }; + if (p.locations_cache.peek(path)) |e| { + sh.source = e.source; + sh.colors = e.colors; + } + } + if (sh.colors.len == 0 or sh.colors.len != sh.source.len) return sh.ask(p); + if (line_no < sh.line) { + sh.line = 1; + sh.at = 0; + } + while (sh.line < line_no) : (sh.line += 1) { + sh.at = (std.mem.indexOfScalarPos(u8, sh.source, sh.at, '\n') orelse return sh.ask(p)) + 1; + } + const line = sh.source[sh.at .. std.mem.indexOfScalarPos(u8, sh.source, sh.at, '\n') orelse sh.source.len]; + if (column + code.len > line.len or !std.mem.eql(u8, line[column..][0..code.len], code)) return sh.ask(p); + return sh.colors[sh.at + column ..][0..code.len]; + } + + fn ask(sh: *Shades, p: *Pardes) []const u8 { + if (!sh.asked) Stream.paintRows(p, sh.path); + sh.asked = true; + return &.{}; + } +}; + +/// A file's colours arrived (Stream.paintRows): every query pane's hit +/// rows still plain, live or frozen, coloured from what is painted now, in +/// place; the first row of a file not painted yet asks for it, so they are +/// painted one after another. +pub fn recolor(p: *Pardes) void { + for (p.panes) |slot| { + const rp = slot orelse continue; + if (rp.query == null) continue; + const f = &rp.file.?; + var sh: Shades = .{}; + var lines = std.mem.splitScalar(u8, f.content, '\n'); + var changed = false; + for (f.location_rows) |row| { + const text = lines.next() orelse break; + if (row.kind != .match or row.location_end == 0 or row.at.line == 0 or row.colors.len == 0) continue; + if (std.mem.indexOfNone(u8, row.colors, &.{0}) != null) continue; + const code = text[@min(row.code_start, text.len)..]; + const got = sh.of(p, row.path, row.at.line, code[0..@min(code.len, row.colors.len)], row.source_column); + if (got.len == 0) continue; + @memcpy(row.colors[0..got.len], got); + changed = true; + } + if (changed) dropHighlights(p, f); + } +} + +/// The pane's painted colours let go, so the next frame paints them from +/// its rows again: with its text the same, a frame would keep the old ones +/// (refreshHighlights keeps a painting that covers the view). +fn dropHighlights(p: *Pardes, f: *File.State) void { + if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); + f.highlights = &.{}; + f.highlight_start = 0; + f.syntax_dirty = true; +} + /// The path a row of pane `tp`'s text says: its file's, or `@pN` for a /// text that is no file (a list, a scratch). fn addressOf(arena: std.mem.Allocator, tp: *const Pane) []const u8 { @@ -801,17 +892,47 @@ fn addressOf(arena: std.mem.Allocator, tp: *const Pane) []const u8 { return std.fmt.allocPrint(arena, config.pane_addr ++ "{d}", .{tp.serial}) catch ""; } +/// The shortest text that names file `path` to a Look from directory +/// `dir` (a Look joins a relative word to its pane's directory): the path +/// relative to `dir` (`x`, `sub/x`, `../x`) when that is shorter, else the +/// absolute one, never an `@pN`. A relative name a Look would read as +/// something else (a pane address, a URL) stays absolute. `buf` holds a +/// relative one that climbs. +pub fn lookPath(buf: []u8, path: []const u8, dir: []const u8) []const u8 { + // a relative pane's (a test's): its directory's prefix off, as ever + if (path.len == 0 or path[0] != '/' or dir.len == 0 or dir[0] != '/') return Names.relative(path, dir); + const d = std.mem.trimEnd(u8, dir, "/"); + // the last slash both share, ending a whole component of `dir` + var k: usize = 0; + var i: usize = 0; + while (i < path.len and i <= d.len) : (i += 1) { + if (path[i] == '/' and (i == d.len or d[i] == '/')) k = i; + if (i == d.len or d[i] != path[i]) break; + } + const ups = std.mem.count(u8, d[k..], "/"); + const rest = path[k + 1 ..]; + if (ups * 3 + rest.len >= path.len or ups * 3 + rest.len > buf.len) return path; + for (0..ups) |u| @memcpy(buf[u * 3 ..][0..3], "../"); + @memcpy(buf[ups * 3 ..][0..rest.len], rest); + const rel = buf[0 .. ups * 3 + rest.len]; + if (std.mem.startsWith(u8, rel, config.pane_addr) or std.mem.indexOf(u8, rel, "://") != null) return path; + return rel; +} + /// Rows from pane `tp`'s places: one a line with any (two on a line are /// one row: everywhere's bug), the declaration they are in over them when /// its parse exists, from place `from` on, `budget` rows at most. The /// place it stopped before. -fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row), tp: *Pane, from: usize, budget: *usize) !usize { +fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row), tp: *Pane, dir: []const u8, from: usize, budget: *usize) !usize { const f = &tp.file.?; const set = &tp.places; const hits = set.hits.items; const index = try File.lineIndex(p.gpa, f); - const width = gutterWidth(index.len); const path = addressOf(p.scratch.allocator(), tp); + var relbuf: [4096]u8 = undefined; + const rel = lookPath(&relbuf, path, dir); + const width = addressWidth(rel, index.len); + var sh: Shades = .{}; const decls = ownerDecls(p, f, false) orelse &.{}; var decl_now: ?usize = null; var i: usize = @min(from, hits.len); @@ -852,15 +973,20 @@ fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.Ar const head_ls = index[decls[di].start_line]; const head_le = if (decls[di].start_line + 1 < index.len) index[decls[di].start_line + 1] - 1 else f.content.len; const head = std.mem.trim(u8, f.content[head_ls..head_le], " \t\r"); - var hb: [600]u8 = undefined; - const text = std.fmt.bufPrint(&hb, " {s}", .{head[0..@min(head.len, 560)]}) catch ""; + // Its address too, `rel:line`, in the column the hits' are. + var hb: [8192]u8 = undefined; + const address = std.fmt.bufPrint(&hb, "{s}:{d}", .{ rel, decls[di].start_line + 1 }) catch ""; + const gutter = @max(width, address.len) + 2; + const shown = head[0..@min(head.len, hb.len -| gutter)]; + @memset(hb[address.len..gutter], ' '); + @memcpy(hb[gutter..][0..shown.len], shown); try q.items.append(p.gpa, .{ .kind = .header, .serial = tp.serial }); - try writeRow(p, out, rows, text, .{ .kind = .context, .declaration = true, .path = @constCast(path), .at = .{ .line = decls[di].start_line + 1 }, .location_end = 0, .code_start = 0 }); + try writeRow(p, out, rows, hb[0 .. gutter + shown.len], .{ .kind = .context, .declaration = true, .path = @constCast(path), .at = .{ .line = decls[di].start_line + 1 }, .location_end = address.len, .code_start = gutter }); }; } const shade: []const u8 = if (f.highlights.len > 0 and ls >= f.highlight_start and le <= f.highlight_start + f.highlights.len) f.highlights[ls - f.highlight_start .. le - f.highlight_start] else &.{}; try q.items.append(p.gpa, .{ .kind = .hit, .serial = tp.serial, .hit = i, .n = n }); - try writeHitRow(p, out, rows, width, path, line + 1, f.content[ls..le], hits[i].start - ls, @min(hits[i].end, le) - ls, shade); + try writeHitRow(p, out, rows, &sh, width, rel, path, line + 1, f.content[ls..le], hits[i].start - ls, @min(hits[i].end, le) - ls, shade); i += n; } return i; @@ -891,7 +1017,7 @@ fn makeTextRows(p: *Pardes, id: usize, oid: usize, full: bool) !void { var hb: [64]u8 = undefined; try noteRow(p, q, &out, &rows, std.fmt.bufPrint(&hb, " ... {d} places before these", .{from}) catch " ..."); } - q.window.hi = try writePlacesRows(p, q, &out, &rows, owner, from, &budget); + q.window.hi = try writePlacesRows(p, q, &out, &rows, owner, std.fs.path.dirname(rp.file.?.path) orelse "/", from, &budget); q.window.lo = @min(from, q.window.hi); } if (q.more) try noteRow(p, q, &out, &rows, " ... and more: the count says how many"); @@ -974,7 +1100,7 @@ fn makeAllRows(p: *Pardes, id: usize) !void { const head = std.fmt.bufPrint(&hb, config.pane_addr ++ "{d} {s} ({d})", .{ tp.serial, name[0..@min(name.len, 500)], set.hits.items.len }) catch ""; try q.items.append(p.gpa, .{ .kind = .header, .serial = tp.serial }); try writeRow(p, &out, &rows, head, .{ .kind = .context, .declaration = true, .path = @constCast(addressOf(p.scratch.allocator(), tp)), .at = .{}, .location_end = 0, .code_start = 0 }); - _ = try writePlacesRows(p, q, &out, &rows, tp, 0, &budget); + _ = try writePlacesRows(p, q, &out, &rows, tp, std.fs.path.dirname(rp.file.?.path) orelse "/", 0, &budget); } if (budget == 0) try noteRow(p, q, &out, &rows, " ... and more: the count says how many"); setRows(p, rp, &out, &rows); @@ -1084,6 +1210,8 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void { const q = rp.query.?; const dir = std.fs.path.dirname(rp.file.?.path) orelse "/"; const at: usize = @min(q.grep_at, q.found.items.len -| 1); + // nothing current before these rows: the first one's shown once it is + const shown = q.current != null; q.items.clearRetainingCapacity(); var out: std.ArrayList(u8) = .empty; defer out.deinit(p.gpa); @@ -1097,18 +1225,29 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void { try noteRow(p, q, &out, &rows, std.fmt.bufPrint(&hb, " ... {d} lines before these", .{lo}) catch " ..."); } var current: ?usize = null; + var sh: Shades = .{}; + var width: usize = 0; + var relbuf: [4096]u8 = undefined; 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)) { - last_path = rel; + const name = q.found_bytes.items[fd.path..][0..fd.path_len]; + const path = std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), name }) catch continue; + const rel = lookPath(&relbuf, path, dir); + if (!std.mem.eql(u8, name, last_path)) { + last_path = name; try q.items.append(p.gpa, .{ .kind = .header }); try writeRow(p, &out, &rows, rel, .{ .kind = .context, .path = @constCast(path), .at = .{}, .location_end = 0, .code_start = 0 }); + // the file's rows in this window, for the widest line number + var max_line: usize = fd.line; + for (q.found.items[i..hi]) |next| { + if (!std.mem.eql(u8, q.found_bytes.items[next.path..][0..next.path_len], name)) break; + max_line = @max(max_line, next.line); + } + width = addressWidth(rel, max_line); } 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, &.{}); + try writeHitRow(p, &out, &rows, &sh, width, rel, path, fd.line, code, fd.col, fd.end, &.{}); } if (hi < q.found.items.len) { var hb: [64]u8 = undefined; @@ -1122,7 +1261,7 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void { q.current = current orelse firstHit(q); countGrepCurrent(q); showCurrent(rp); - if (q.current == null) blankPreview(p, id); + if (q.current == null) blankPreview(p, id) else if (!shown) showTarget(p, id); } /// What the disk walk passed over, said under the rows: a search that @@ -1258,16 +1397,28 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void { var last_path: []const u8 = ""; var pathbuf: [4096 + 8]u8 = undefined; var n: usize = 0; - for (sorted.items) |ln| { + var sh: Shades = .{}; + var width: usize = 0; + var relbuf: [4096]u8 = undefined; + for (sorted.items, 0..) |ln, li| { if (ln.len == 0) continue; if (pat.len > 0 and std.ascii.findIgnoreCase(ln, pat) == null) continue; const t = look.parsePathLine(ln); if (t.at.line == 0) continue; const path = if (t.path.len > 0 and t.path[0] == '/') t.path else std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), t.path }) catch continue; + const rel = lookPath(&relbuf, path, dir); if (!std.mem.eql(u8, t.path, last_path)) { last_path = t.path; try q.items.append(p.gpa, .{ .kind = .header }); - try writeRow(p, &out, &rows, Names.relative(t.path, dir), .{ .kind = .context, .path = @constCast(path), .at = .{}, .location_end = 0, .code_start = 0 }); + try writeRow(p, &out, &rows, rel, .{ .kind = .context, .path = @constCast(path), .at = .{}, .location_end = 0, .code_start = 0 }); + // sorted by place: the file's last row has its widest line number + var max_line: usize = t.at.line; + for (sorted.items[li..]) |next| { + const tn = look.parsePathLine(next); + if (!std.mem.eql(u8, tn.path, t.path)) break; + max_line = @max(max_line, tn.at.line); + } + width = addressWidth(rel, max_line); } const rest = std.mem.trimStart(u8, ln[@min(t.end, ln.len)..], " \t:"); const col0 = t.at.col -| 1; @@ -1293,7 +1444,7 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void { break; } try q.items.append(p.gpa, .{ .kind = .hit, .n = 1, .serial = shown_in }); - try writeHitRow(p, &out, &rows, 9, path, t.at.line, code, col, end, &.{}); + try writeHitRow(p, &out, &rows, &sh, width, rel, path, t.at.line, code, col, end, &.{}); n += 1; } if (n == 0) try noteRow(p, q, &out, &rows, " nothing"); @@ -1520,11 +1671,26 @@ fn showTarget(p: *Pardes, id: usize) void { const line0 = meta.at.line -| 1; const visible = line0 >= tf.scroll and line0 < tf.scroll + tp.rows; if (meta.at.line > 0) look.focusPaneLine(p, i, meta.at, if (visible) .keep else .center); + _ = selectPlace(p, tp, rp.serial, meta.at); return; } displayPreview(p, id, meta.path, meta.at); } +/// The place a row points at (its line and column) made current in pane +/// `tp`'s places, when query `serial` fed them: selected and in view, as +/// the owner's are. False when there is none there. +fn selectPlace(p: *Pardes, tp: *Pane, serial: u32, at: look.Spot) bool { + const f = if (tp.file) |*file| file else return false; + const set = &tp.places; + if (set.from != serial or !set.started or at.line == 0 or at.line > File.nlines(p.gpa, f)) return false; + const ls = File.lineStart(p.gpa, f, at.line - 1); + const i = Places.firstFrom(set.hits.items, ls + (at.col -| 1)); + if (i >= set.hits.items.len or (at.line < File.nlines(p.gpa, f) and set.hits.items[i].start >= File.lineStart(p.gpa, f, at.line))) return false; + set.goTo(tp, i, false); + return true; +} + // ---- the +Preview ---- /// The +Preview under query pane `qid`, if it has one: by serial. @@ -1597,6 +1763,8 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void f.scroll = 0; f.syntax_dirty = true; pane.hscroll = 0; + // another file: its own places, found below + pane.places.clear(p.gpa); p.sync(); } look.clearNavigationSelection(p, pane); @@ -1609,6 +1777,36 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void return; } look.focusPaneLine(p, id, at, .center); + // The query's hits in this file are the preview's places, tinted and + // ticked as in the file's own pane would be, the row's one current and + // selected; the view centred on it, its neighbours round it. + const qp = p.panes[qid].?; + const q = qp.query.?; + switch (q.source) { + .grep => if (parse(q.asked)) |a| pane.places.search(p, pane, .list, qp.serial, a.pat, true, null, false), + .refs, .defs, .impls, .diagnostics => if (pane.places.from != qp.serial or !pane.places.shows(pane)) { + var hits: std.ArrayList(Places.Hit) = .empty; + defer hits.deinit(p.gpa); + const dir = std.fs.path.dirname(qp.file.?.path) orelse "/"; + const nlines = File.nlines(p.gpa, f); + var pathbuf: [4096 + 8]u8 = undefined; + var lines = std.mem.splitScalar(u8, q.lsp_rows, '\n'); + while (lines.next()) |ln| { + const t = look.parsePathLine(ln); + if (t.at.line == 0 or t.at.line > nlines) continue; + const full = if (t.path.len > 0 and t.path[0] == '/') t.path else std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), t.path }) catch continue; + if (!std.mem.eql(u8, full, f.path)) continue; + const ls = File.lineStart(p.gpa, f, t.at.line - 1); + const le = std.mem.indexOfScalarPos(u8, f.content, ls, '\n') orelse f.content.len; + const col = @min(t.at.col -| 1, le - ls); + const end = @min(if (t.at.end_col > 0 and t.at.end_line == t.at.line) t.at.end_col else col + 1, le - ls); + hits.append(p.gpa, .{ .start = ls + col, .end = ls + @max(end, col + 1) }) catch break; + } + pane.places.feed(p, pane, qp.serial, hits.items, null); + }, + else => {}, + } + if (selectPlace(p, pane, qp.serial, at)) return; if (at.end_line == 0 or at.end_col <= at.col) pane.body.msel = .{ .active = true, .r0 = @intCast(at.line - 1), .r1 = @intCast(at.line - 1) }; } @@ -1914,8 +2112,9 @@ pub fn clampEdit(p: *Pardes, t: *panes.Text, old: []const u8, new: []u8) ?[]u8 { return out; } -/// B3 on a row's gutter (its line number): the row's address, as a +Diff -/// line's prefix is its place (exec.operandAt). Null off a gutter. +/// B3 on a row's address text: the address whole, as a +Diff line's +/// prefix is its place (exec.operandAt), so a path with a space in it is +/// still one; the text says the same place. Null off the address. pub fn rowAddress(pane: *const Pane, row: usize, col: usize, buf: []u8) ?[]const u8 { if (pane.query == null) return null; const f = pane.file orelse return null; @@ -2122,9 +2321,9 @@ test "a / query: Enter lands and freezes, the register filled, n and N step sile try std.testing.expectEqual(Places.Provenance.search, pane.places.provenance); const frozen = try std.testing.allocator.dupe(u8, qp.file.?.content); defer std.testing.allocator.free(frozen); - // the rows: one a line (gamma alpha has one), a gutter number each - try std.testing.expect(std.mem.indexOf(u8, frozen, " 1 alpha one\n") != null); - // B3 on a row's line number is its address; on its code, no address + // the rows: one a line (gamma alpha has one), each its place as text + try std.testing.expect(std.mem.indexOf(u8, frozen, "\ntest.txt:1:1 alpha one\n") != null); + // B3 on a row's address is its address; on its code, no address var abuf: [256]u8 = undefined; const addr = rowAddress(qp, 1, 2, &abuf).?; try std.testing.expect(std.mem.endsWith(u8, addr, ":1:1")); @@ -2511,3 +2710,151 @@ test "B1 on a query pane's rows places the caret and selects, nothing else, the try std.testing.expectEqual(@as(usize, 0), p.active); try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); } + +test "a query row's place is its text: the match tinted after it, B3 on it opens the place, the rows saved and read again still Look" { + if (!filesystem.platform_has_fs) return; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48 }); + defer p.deinit(); + const a = try testFileIn(p, &tmp, "a.txt", "zero\none needle\ntwo\nthree needle here\n"); + leader(p, "fg"); + typeKeys(p, "needle"); + settle(p); + const qid = queryPane(p).?; + const qp = p.panes[qid].?; + const f = &qp.file.?; + // each row `path:line:col`, relative to the pane's directory, the code + // after it in one column + try std.testing.expect(std.mem.indexOf(u8, f.content, "\na.txt:2:5 one needle\n") != null); + try std.testing.expect(std.mem.indexOf(u8, f.content, "\na.txt:4:7 three needle here\n") != null); + // the match's tint where the match is, past the address + var row4: usize = 0; + var lines = std.mem.splitScalar(u8, f.content, '\n'); + var i: usize = 0; + while (lines.next()) |line| : (i += 1) { + if (i >= f.location_rows.len) break; + const meta = f.location_rows[i]; + if (meta.kind != .match or meta.location_end == 0) continue; + const dec = Output.decorateRow(f.output, line, meta); + try std.testing.expectEqualStrings("needle", line[dec.mark_start..dec.mark_end]); + try std.testing.expect(look.parsePathLine(line[0..meta.location_end]).at.line == meta.at.line); + if (meta.at.line == 4) row4 = i; + } + try std.testing.expect(row4 > 0); + // B3 on the address: the place, in the pane that shows it + a.body.cur_row = 0; + p.acknowledgePanelPresentation(&.{}); + const r = p.rects[qid]; + const y = r.y + qp.tag_rows + @as(u16, @intCast(row4)) - @as(u16, @intCast(qp.scroll())); + const x = r.x + config.GUTTER + File.gutterWidth(qp) + 3; + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = x, .row = y } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = x, .row = y } }); + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expectEqual(@as(i32, 3), a.body.cur_row); + // the rows as a plain text in that directory (what Save writes): a + // Look at a row's address word still goes there + const saved = try std.testing.allocator.dupe(u8, f.content); + defer std.testing.allocator.free(saved); + p.newScratchBelow(0); + const sid = p.active; + const sp = p.panes[sid].?; + File.setContent(p, &sp.file.?, try p.gpa.dupe(u8, saved)); + try std.testing.expect(sp.query == null and sp.file.?.location_rows.len == 0); + const at = std.mem.indexOf(u8, saved, "a.txt:2:5").?; + const line = saved[at .. std.mem.indexOfScalarPos(u8, saved, at, '\n').?]; + const word = look.expandedWord(line, 3).?; + a.body.cur_row = 0; + look.lookAt(p, sid, line[word.lo..word.hi]); + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expectEqual(@as(i32, 1), a.body.cur_row); +} + +test "the +Preview: the query's hits in its file are its places, the row's one current and selected, coloured as the file is" { + if (!filesystem.platform_has_fs) return; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const text = "const needle = 1;\nconst hay = 2;\nfn f() void {\n _ = needle;\n}\npub const needle_too = needle;\n"; + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "b.zig", .data = text }); + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 60 }); + defer p.deinit(); + _ = try testFileIn(p, &tmp, "a.txt", "x\n"); + leader(p, "fg"); + typeKeys(p, "needle"); + settle(p); + const qid = queryPane(p).?; + // the first row shown in the preview as the rows land; a step, the next + const pid = previewOf(p, qid).?; + try std.testing.expectEqual(@as(?usize, 0), p.panes[pid].?.places.current); + p.update(.{ .key = .{ .cp = 'n', .ctrl = true } }); + const pv = p.panes[pid].?; + try std.testing.expect(std.mem.endsWith(u8, pv.file.?.path, "/b.zig")); + // its places: every `needle` in the file, as its own pane would find them + var expected: std.ArrayList(Places.Hit) = .empty; + defer expected.deinit(std.testing.allocator); + var from: usize = 0; + while (std.mem.indexOfPos(u8, text, from, "needle")) |at| : (from = at + 1) try expected.append(std.testing.allocator, .{ .start = at, .end = at + 6 }); + try std.testing.expectEqualSlices(Places.Hit, expected.items, pv.places.hits.items); + // the current row's (line 4, column 9) current and selected + try std.testing.expectEqual(@as(?usize, 1), pv.places.current); + var sel = pv.body.primaryRange(pv.file.?.content, 0); + try std.testing.expectEqual(expected.items[1].start, @min(sel.anchor, sel.head)); + // the next row: the next place, the preview's places standing + p.update(.{ .key = .{ .cp = 'n', .ctrl = true } }); + try std.testing.expectEqual(pid, previewOf(p, qid).?); + try std.testing.expectEqual(@as(?usize, 2), pv.places.current); + sel = pv.body.primaryRange(pv.file.?.content, 0); + try std.testing.expectEqual(expected.items[2].start, @min(sel.anchor, sel.head)); + // its colours the file's, as its own pane paints them + if (!syntax.enabled) return; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + const pf = &pv.file.?; + try std.testing.expect(pf.highlights.len > 0 and std.mem.indexOfNone(u8, pf.highlights, &.{0}) != null); + const own = try syntax.highlightFileRange(p.tree_sitter_gpa, pf.path, pf.content, pf.highlight_start, pf.highlight_start + pf.highlights.len); + defer p.tree_sitter_gpa.free(own); + try std.testing.expectEqualSlices(u8, own, pf.highlights); +} + +test "a row's address is the shortest path a Look from the query pane resolves: in its directory, under it, above it, else absolute" { + if (!filesystem.platform_has_fs) return; + if (!pardes.lsp.supports.contains(.references)) return; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(std.testing.io, "q/sub"); + for ([_][]const u8{ "q/sub/deep.txt", "up.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "one\ntwo\n" }); + var root_buf: [std.fs.max_path_bytes]u8 = undefined; + const root = root_buf[0..try tmp.dir.realPath(std.testing.io, &root_buf)]; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + _ = try testFileIn(p, &tmp, "q/here.txt", "one\ntwo\n"); + // another tree, where the climb out would be longer than the path + const far = "/etc/passwd"; + if (std.c.access(far, std.c.F_OK) != 0) return; + const answer = try std.fmt.allocPrint(std.testing.allocator, "{s}/q/here.txt:2:1 two\n{s}/q/sub/deep.txt:2:1 two\n{s}/up.txt:2:1 two\n" ++ far ++ ":1:1 root\n", .{ root, root, root }); + defer std.testing.allocator.free(answer); + fillList(p, 0, .refs, answer); + const qp = p.panes[queryPane(p).?].?; + const f = &qp.file.?; + const dir = std.fs.path.dirname(f.path).?; + var lines = std.mem.splitScalar(u8, f.content, '\n'); + var seen: std.ArrayList([]const u8) = .empty; + defer seen.deinit(std.testing.allocator); + for (f.location_rows) |row| { + const line = lines.next().?; + if (row.kind != .match or row.location_end == 0) continue; + const addr = line[0..row.location_end]; + try seen.append(std.testing.allocator, addr); + // Looked at from the pane: the row's file, at its line + var realbuf: [4096]u8 = undefined; + const t = look.resolve(p, addr, dir, &realbuf); + try std.testing.expectEqualStrings(row.path, t.file.path); + try std.testing.expectEqual(row.at.line, t.file.at.line); + } + for ([_][]const u8{ "here.txt:2:1", "sub/deep.txt:2:1", "../up.txt:2:1", far ++ ":1:1" }) |want| { + for (seen.items) |got| { + if (std.mem.eql(u8, got, want)) break; + } else return error.TestUnexpectedResult; + } +} diff --git a/src/Stream.zig b/src/Stream.zig index e42d4a16..da7d12eb 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -218,6 +218,20 @@ painted_ahead: u32 = 0, painted_on_loop: u32 = 0, /// The view the last paint was asked from, and which way it was going. ahead_seen: struct { serial: u32 = 0, scroll: usize = 0, dir: i2 = 1 } = .{}, +/// A file's colours for query rows being worked out (paintRows), one at a +/// time, its own task beside the scroll's. ponytail: one file at a time, +/// the next asked when it lands (Query.recolor); a worker per file if a +/// Grep of hundreds of files colours too slowly. +rows_paint: ?PaintTask = null, +/// The files (a hash of path and revision) asked since the last Grep +/// began: each asked once, so a file whose colours never fit its rows +/// (changed on disk since) is not painted again and again. ponytail: a +/// ring of 256; the oldest forgotten. +rows_asked: [256]u64 = @splat(0), +rows_asked_at: u8 = 0, +/// Rows asked for their colours while a 9P write was served: asked again +/// on the next step (Query.produceAll). +rows_later: bool = false, pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { s.cancelTask(); @@ -244,7 +258,7 @@ pub fn cancelParse(s: *Stream) void { /// The paints' workers told to stop and joined (the session's end). pub fn cancelPaint(s: *Stream) void { - for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| { + for ([_]*?PaintTask{ &s.paint, &s.paint_retired, &s.rows_paint }) |slot| { const t = slot.* orelse continue; slot.* = null; t.job.stop.store(true, .release); @@ -310,11 +324,69 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont p.emit(.{ .search = .{ .gen = gen } }); } -fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { +/// A file's colours for the query rows that show its lines, the whole +/// file painted on a worker (rows coloured as the file's language colours +/// them, as Callstack's are): the text of the pane that shows it, else read +/// from disk there. Kept in the locations cache when it lands, where the +/// rows find them (Query.Shades). Not without workers (the rows stay +/// plain), nor for a file bigger than `max_rows_paint_bytes`, nor twice for +/// one revision of a file. +pub fn paintRows(p: *Pardes, path: []const u8) void { + const s = &p.stream; + if (s.rows_paint != null or p.host.vtable.search == null) return; + // Asked while a 9P write is served (a script's Grep): not now, as no + // paint is (the write would wait for it to settle); the next step asks. + if (p.fs.serving) { + s.rows_later = true; + return; + } + if (!syntax.readyApart(path)) return; + var content: ?[]const u8 = null; + var revision: u32 = 0; + for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.output == null and std.mem.eql(u8, f.path, path)) { + content = f.content; + revision = f.revision; + break; + }; + if (content) |c| if (c.len > max_rows_paint_bytes) return; + const key = std.hash.Wyhash.hash(revision, path) | 1; + if (std.mem.indexOfScalar(u64, &s.rows_asked, key) != null) return; + const job = p.gpa.create(PaintJob) catch return; + const path_copy = p.gpa.dupe(u8, path) catch { + p.gpa.destroy(job); + return; + }; + const source = p.gpa.dupe(u8, content orelse "") catch { + p.gpa.free(path_copy); + p.gpa.destroy(job); + return; + }; + s.rows_asked[s.rows_asked_at] = key; + s.rows_asked_at +%= 1; + s.paint_count +%= 1; + const gen = paint_bit | (s.paint_count & ~paint_bit); + job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source }; + s.rows_paint = .{ .job = job }; + p.emit(.{ .search = .{ .gen = gen } }); +} + +/// The biggest file whose colours query rows are painted from. +const max_rows_paint_bytes = 4 << 20; + +fn paintWork(io: std.Io, job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { + // A file's colours for query rows (paintRows, serial 0) with no text + // given: read here, off the loop, and sent back with the colours, the + // text they are of. + const read: []u8 = if (job.serial == 0 and job.source.len == 0) + std.Io.Dir.cwd().readFileAlloc(io, job.path, job.out, .limited(max_rows_paint_bytes)) catch @constCast(&[_]u8{}) + else + @constCast(&[_]u8{}); + const source = if (read.len > 0) read else job.source; // Stopped or failed, it still answers (with nothing), so the loop // knows to join it. - const styles: []u8 = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]u8{}); - _ = sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done }); + const styles: []u8 = syntax.highlightApart(job.out, job.path, source, &job.stop) catch @constCast(&[_]u8{}); + const text: []u8 = if (job.serial != 0 or read.len > 0) read else job.out.dupe(u8, job.source) catch @constCast(&[_]u8{}); + _ = sink(ctx, .{ .gen = job.gen, .rows = text, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done }); } /// A paint's answer: its task joined, its stretch kept for when the view @@ -322,6 +394,22 @@ fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { fn receivePaint(p: *Pardes, b: *const Batch) void { const s = &p.stream; const painted = b.painted.?; + // A file's colours for query rows: kept where the rows find them, and + // the rows coloured from them (which asks for the next file). + if (s.rows_paint) |t| if (t.job.gen == b.gen) { + s.rows_paint = null; + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.await(t.io.?); + pardes.turn.back(); + } + defer t.job.deinit(); + p.locations_cache.put(p.tree_sitter_gpa, t.job.path, b.rows, b.styles) catch {}; + panes.Query.recolor(p); + p.needs_frame = true; + return; + }; // Its task joined: it has sent its last, so the join is a return away. for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) { slot.* = null; @@ -436,6 +524,7 @@ pub fn query(p: *Pardes, target: u32, kind: Kind, pattern: []const u8, dir: []co s.first_ns = null; s.state = .waiting; s.due_ns = if (how == .typed) p.now_ns + debounce_ns else p.now_ns; + if (kind == .grep) s.rows_asked = @splat(0); } /// Over: the worker canceled, nothing more of it lands. @@ -502,11 +591,11 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*an const s = &p.stream; // A paint's generation: its own task, its own worker. if (gen & paint_bit != 0) { - if (s.paint) |*t| if (t.job.gen == gen and t.future == null) { + for ([_]*?PaintTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |*t| if (t.job.gen == gen and t.future == null) { t.job.out = gpa; - t.future = io.concurrent(paintWork, .{ t.job, ctx, sink }) catch { + t.future = io.concurrent(paintWork, .{ io, t.job, ctx, sink }) catch { const job = t.job; - s.paint = null; + slot.* = null; job.deinit(); return; }; @@ -555,6 +644,16 @@ fn makeJob(gpa: std.mem.Allocator, s: *const Stream) !*Job { /// the whole search on the loop, the turn out for it, its batches taken in /// as a worker's would be. Its cap is a hard stop. pub fn runHere(p: *Pardes, gen: u32) void { + // A paint with no worker to run it (a shell not threaded yet): let go, + // never left to hold its slot; the rows or the view ask again. + if (gen & paint_bit != 0) { + const s = &p.stream; + for ([_]*?PaintTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |t| if (t.job.gen == gen and t.future == null) { + slot.* = null; + t.job.deinit(); + }; + return; + } if (comptime !filesystem.platform_has_fs) { p.stream.state = .failed; return; @@ -1395,3 +1494,162 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's try testing.expectEqual(@as(usize, 3000), qp.query.?.found.items.len); try testing.expect(!qp.query.?.capped); } + +test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a worker (GREP_TIMES=)" { + const Key = pardes.Key; + const path_z = std.c.getenv("GREP_TIMES") orelse return error.SkipZigTest; + const path = std.mem.span(path_z); + const bytes = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(8 << 20)); + defer testing.allocator.free(bytes); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + const pane = try p.setTestFile(bytes); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, path); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + const Stats = struct { + fn print(name: []const u8, xs: []u64) void { + if (xs.len == 0) return; + std.mem.sort(u64, xs, {}, std.sort.asc(u64)); + std.debug.print("{s}: n {d} p50 {d} us p90 {d} us max {d} us\n", .{ name, xs.len, xs[xs.len / 2] / 1000, xs[xs.len * 9 / 10] / 1000, xs[xs.len - 1] / 1000 }); + } + }; + var keys: std.ArrayList(u64) = .empty; + defer keys.deinit(testing.allocator); + var refills: std.ArrayList(u64) = .empty; + defer refills.deinit(testing.allocator); + var idle: std.ArrayList(u64) = .empty; + defer idle.deinit(testing.allocator); + panes.Query.open(p, 0, .grep, "", true); + const qid = p.active; + const q = p.panes[qid].?.query.?; + for (0..3) |round| { + for ("pane") |c| { + const t0 = now(); + p.advance(t0); + p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try keys.append(testing.allocator, now() - t0); + // the frames till the next key: the debounce, the batches, the paints + const until = now() + 400 * std.time.ns_per_ms; + while (now() < until) { + const made = q.made_gen; + const t1 = now(); + p.advance(t1); + shell.pump(p); + p.update(.tick); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try (if (q.made_gen != made) &refills else &idle).append(testing.allocator, now() - t1); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + } + // back to an empty pattern for the next round + if (round < 2) for (0..4) |_| p.update(.{ .key = .{ .cp = Key.backspace } }); + } + // Enter: the whole list frozen, 2000 rows, and the frames after it. + var after: std.ArrayList(u64) = .empty; + defer after.deinit(testing.allocator); + const t3 = now(); + p.advance(t3); + p.update(.{ .key = .{ .cp = Key.enter } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const t_enter = now() - t3; + const until = now() + 1000 * std.time.ns_per_ms; + while (now() < until) { + const t1 = now(); + p.advance(t1); + shell.pump(p); + p.update(.tick); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try after.append(testing.allocator, now() - t1); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + var coloured: usize = 0; + for (p.panes[qid].?.file.?.location_rows) |row| coloured += @intFromBool(row.kind == .match and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + std.debug.print("grep rows: {d} found ({s}), {d} rows frozen, {d} coloured; Enter (2000 rows written) {d} us\n", .{ q.found.items.len, @tagName(p.stream.used), p.panes[qid].?.file.?.location_rows.len, coloured, t_enter / 1000 }); + Stats.print("frames after Enter", after.items); + Stats.print("keystroke (update + render)", keys.items); + Stats.print("refill frame (pump + tick + render, rows remade)", refills.items); + Stats.print("other frames", idle.items); +} + +test "Stream: a Grep's rows are coloured as their file's language colours them, the file painted on a worker, the match tint on top" { + if (comptime !filesystem.platform_has_fs) return; + if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const text = "const std = @import(\"std\");\npub fn value() u32 {\n return 42; // value\n}\n"; + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text }); + var root_buf: [4096]u8 = undefined; + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + // the file open in the pane that asks: its text painted, not the disk's + const pane = try p.setTestFile(text); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + panes.Query.open(p, 0, .grep, "value", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const f = &p.panes[qid].?.file.?; + var halt: std.atomic.Value(bool) = .init(false); + const own = try syntax.highlightApart(testing.allocator, "c.zig", text, &halt); + defer testing.allocator.free(own); + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + var coloured: usize = 0; + while (now() < until) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + // a frame each, as a shell draws: the rows painted before their colours land + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + coloured = 0; + for (f.location_rows) |row| if (row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null) { + coloured += 1; + }; + if (coloured == 2 and p.stream.rows_paint == null) break; + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 2), coloured); + // and painted: the frame's colours are the rows' + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + // each row's colours its line's, byte for byte; the match tinted over them + var lines = std.mem.splitScalar(u8, f.content, '\n'); + for (f.location_rows) |row| { + const line = lines.next().?; + if (row.kind != .match or row.location_end == 0) continue; + const ls = @import("modal.zig").lineStartOffset(text, row.at.line - 1); + try testing.expectEqualSlices(u8, own[ls + row.source_column ..][0..row.colors.len], row.colors); + const at = @intFromPtr(line.ptr) - @intFromPtr(f.content.ptr) + row.code_start - f.highlight_start; + try testing.expectEqualSlices(u8, row.colors, f.highlights[at..][0..row.colors.len]); + const dec = panes.Output.decorateRow(f.output, line, row); + try testing.expectEqualStrings("value", line[dec.mark_start..dec.mark_end]); + } +} diff --git a/src/locations.zig b/src/locations.zig index 6f086214..cdb228a6 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -814,6 +814,40 @@ pub const Cache = struct { return .{ .analysis = next.analysis }; } + /// The analysis kept for `path` with its colours, if any, of whatever + /// source it was made from (a caller compares the lines it uses): + /// never parses. Borrowed, as `get`'s. + pub fn peek(cache: *const Cache, path: []const u8) ?struct { source: []const u8, colors: []const u8 } { + for (cache.entries[0..cache.len]) |entry| if (entry.colors_ready and std.mem.eql(u8, entry.path, path)) + return .{ .source = entry.source, .colors = entry.analysis.colors }; + return null; + } + + /// `source`'s colours, worked out elsewhere (a worker's, Stream.paintRows), + /// kept for `path` in place of what was: copies of both. + pub fn put(cache: *Cache, gpa: std.mem.Allocator, path: []const u8, source: []const u8, colors: []const u8) !void { + if (path.len + source.len + colors.len > max_bytes) return error.SourceTooLarge; + const owned_path = try gpa.dupe(u8, path); + errdefer gpa.free(owned_path); + const owned_source = try gpa.dupe(u8, source); + errdefer gpa.free(owned_source); + const next: Entry = .{ + .path = owned_path, + .source = owned_source, + .analysis = .{ .colors = try gpa.dupe(u8, colors) }, + .declarations_ready = false, + .colors_ready = true, + }; + for (cache.entries[0..cache.len], 0..) |entry, index| if (std.mem.eql(u8, entry.path, path)) { + cache.remove(gpa, index); + break; + }; + while (cache.len == max_entries or cache.bytes > max_bytes - next.size()) cache.remove(gpa, 0); + cache.entries[cache.len] = next; + cache.len += 1; + cache.bytes += next.size(); + } + fn remove(cache: *Cache, gpa: std.mem.Allocator, index: usize) void { cache.bytes -= cache.entries[index].size(); cache.entries[index].deinit(gpa); diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index d9bd4372..9cb96667 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -3059,8 +3059,9 @@ test "a script's Grep and Find fill the pane's one query pane, a row a line unde const rows = grepped.file.?.content; try testing.expect(std.mem.startsWith(u8, rows, "Grep bravo\n")); try testing.expect(std.mem.indexOf(u8, rows, "\nother.txt\n") != null); - try testing.expect(std.mem.indexOf(u8, rows, "2 bravo here\n") != null); - try testing.expect(std.mem.indexOf(u8, rows, "3 three bravo\n") != null); + // each row its place as text, `path:line:col`, the code in one column + try testing.expect(std.mem.indexOf(u8, rows, "\nother.txt:2:1 bravo here\n") != null); + try testing.expect(std.mem.indexOf(u8, rows, "\nother.txt:3:7 three bravo\n") != null); try testing.expectEqual(@as(usize, 2), grepped.query.?.total); try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .exec), "Find other\n").reply.status); const found = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trimEnd(u8, rd(p, root_exec, 0, 64).bytes, "\n"), 10)).?].?; diff --git a/test/snapshots/fsearch.golden b/test/snapshots/fsearch.golden index f7e5bba6..b195e7a5 100644 --- a/test/snapshots/fsearch.golden +++ b/test/snapshots/fsearch.golden @@ -31,9 +31,9 @@ == snap typing grid=180x31 cursor=7,10 |9: ^ +Search 1/3 Del |10: /MARK -|11: 2 MARK a -|12: 6 MARK b -|13: 10 MARK c +|11: g.txt:2:1 MARK a +|12: g.txt:6:1 MARK b +|13: g.txt:10:1 MARK c == style typing grid=180x31 |0: 0-1 d,#1d2824, 2-179 #b2c3b8,#1d2824, |1: 0-1 d,#887455, 2-89 #becfc4,#314738, 90-91 #becfc4,#887455, 92-179 #b2c3b8,#1d2824, @@ -46,9 +46,9 @@ |8: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-10 #f5f7ee,#3c614a, 11-11 #f5f7ee,#0d1410, 12-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, |9: 0-1 #000000,#add38a, 2-89 #becfc4,#314738, 90-90 #becfc4,#608269, 91-179 #becfc4,#0d1410, |10: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|11: 0-0 d,#608269, 1-1 d,#0d1410, 2-5 d,#243a2d, 6-8 #59675c,#243a2d, 9-12 #f7e4a5,#5e4923, 13-13 #f7e4a5,#243a2d, 14-89 #e5eee5,#243a2d, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|12: 0-0 d,#608269, 1-5 d,#0d1410, 6-8 #59675c,#0d1410, 9-12 #f7e4a5,#352e19, 13-13 #f7e4a5,#0d1410, 14-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, -|13: 0-0 d,#608269, 1-4 d,#0d1410, 5-8 #59675c,#0d1410, 9-12 #f7e4a5,#352e19, 13-13 #f7e4a5,#0d1410, 14-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, +|11: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#243a2d, 16-19 #f7e4a5,#5e4923, 20-20 #f7e4a5,#243a2d, 21-89 #e5eee5,#243a2d, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, +|12: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#0d1410, 16-19 #f7e4a5,#352e19, 20-20 #f7e4a5,#0d1410, 21-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, +|13: 0-0 d,#608269, 1-1 d,#0d1410, 2-15 #59675c,#0d1410, 16-19 #f7e4a5,#352e19, 20-20 #f7e4a5,#0d1410, 21-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-179 #e5eee5,#0d1410, |14: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, |15: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, |16: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, diff --git a/test/snapshots/grep.golden b/test/snapshots/grep.golden index 6dbe19fa..165839f7 100644 --- a/test/snapshots/grep.golden +++ b/test/snapshots/grep.golden @@ -23,7 +23,7 @@ |9: ^ +Search 1/3 Del |10: Grep TARGET |11: a.txt -|12: 1 hello TARGET one +|12: a.txt:1:7 hello TARGET one |13: /tmp/pardes-snap/grep/cwd/+Preview Del |14: 1 == snap second grid=180x31 cursor=13,10 diff --git a/test/snapshots/lookrange.golden b/test/snapshots/lookrange.golden index 2c0aa63d..8f5908bb 100644 --- a/test/snapshots/lookrange.golden +++ b/test/snapshots/lookrange.golden @@ -71,8 +71,8 @@ == snap hit1 grid=150x31 cursor=17,15 |21: +Search 2/2 Save Unattach Del |22: /NEEDLE -|23: 7 gg NEEDLE gg -|24: 8 hhhh NEEDLE +|23: r.txt:7:4 gg NEEDLE gg +|24: r.txt:8:6 hhhh NEEDLE == style hit1 grid=150x31 |14: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-9 #e5eee5,#0d1410, 10-15 #e5eee5,#352e19, 16-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-11 #e5eee5,#0d1410, 12-17 #f5f7ee,#3c614a, 18-149 #f5f7ee,#0d1410, @@ -80,21 +80,21 @@ |18: 0-0 d,#e5eee5, 1-149 d,#0d1410, |21: 0-1 d,#496656, 2-149 #b2c3b8,#1d2824, |22: 0-0 d,#608269, 1-1 d,#0d1410, 2-149 #e5eee5,#0d1410, -|23: 0-0 d,#608269, 1-5 d,#0d1410, 6-8 #59675c,#0d1410, 9-11 #e5eee5,#0d1410, 12-17 #f7e4a5,#352e19, 18-18 #f7e4a5,#0d1410, 19-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-5 d,#243a2d, 6-8 #59675c,#243a2d, 9-13 #e5eee5,#243a2d, 14-19 #f7e4a5,#5e4923, 20-149 #f7e4a5,#243a2d, +|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#352e19, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, +|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#5e4923, 26-149 #f7e4a5,#243a2d, == snap hit2 grid=150x31 cursor=15,14 == style hit2 grid=150x31 |14: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-9 #e5eee5,#0d1410, 10-15 #f5f7ee,#3c614a, 16-16 #f5f7ee,#0d1410, 17-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-11 #e5eee5,#0d1410, 12-17 #e5eee5,#352e19, 18-149 #e5eee5,#0d1410, |16: 0-0 d,#e5eee5, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, |18: 0-0 d,#5e4923, 1-149 d,#0d1410, -|23: 0-0 d,#608269, 1-5 d,#0d1410, 6-8 #59675c,#0d1410, 9-11 #e5eee5,#0d1410, 12-17 #f7e4a5,#5e4923, 18-18 #f7e4a5,#0d1410, 19-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-5 d,#243a2d, 6-8 #59675c,#243a2d, 9-13 #e5eee5,#243a2d, 14-19 #f7e4a5,#352e19, 20-149 #f7e4a5,#243a2d, +|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#5e4923, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, +|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#352e19, 26-149 #f7e4a5,#243a2d, == snap back grid=150x31 cursor=17,15 == style back grid=150x31 |14: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-9 #e5eee5,#0d1410, 10-15 #e5eee5,#352e19, 16-149 #e5eee5,#0d1410, |15: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-11 #e5eee5,#0d1410, 12-17 #f5f7ee,#3c614a, 18-149 #f5f7ee,#0d1410, |16: 0-0 d,#5e4923, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-149 #e5eee5,#0d1410, |18: 0-0 d,#e5eee5, 1-149 d,#0d1410, -|23: 0-0 d,#608269, 1-5 d,#0d1410, 6-8 #59675c,#0d1410, 9-11 #e5eee5,#0d1410, 12-17 #f7e4a5,#352e19, 18-18 #f7e4a5,#0d1410, 19-149 #e5eee5,#0d1410, -|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-5 d,#243a2d, 6-8 #59675c,#243a2d, 9-13 #e5eee5,#243a2d, 14-19 #f7e4a5,#5e4923, 20-149 #f7e4a5,#243a2d, +|23: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-17 #e5eee5,#0d1410, 18-23 #f7e4a5,#352e19, 24-24 #f7e4a5,#0d1410, 25-149 #e5eee5,#0d1410, +|24: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-19 #e5eee5,#243a2d, 20-25 #f7e4a5,#5e4923, 26-149 #f7e4a5,#243a2d, diff --git a/test/snapshots/lsp-client.golden b/test/snapshots/lsp-client.golden index f3d7006e..670d6a86 100644 --- a/test/snapshots/lsp-client.golden +++ b/test/snapshots/lsp-client.golden @@ -20,9 +20,9 @@ |13: +Search 1/3 Del |14: Refs |15: lib.rs -|16: 2 pub fn add(a: i32, b: i32) -> i32 { +|16: lib.rs:2:8 pub fn add(a: i32, b: i32) -> i32 { |17: main.rs -|18: 4 let x = add(1, 2); +|18: main.rs:4:13 let x = add(1, 2); |19: /tmp/pardes-snap/lsp-client/cwd/+Preview Del |20: 1 == snap gr-opened grid=100x25 cursor=21,6 diff --git a/test/snapshots/lsprelpath.golden b/test/snapshots/lsprelpath.golden index dacb6b89..3af53a5c 100644 --- a/test/snapshots/lsprelpath.golden +++ b/test/snapshots/lsprelpath.golden @@ -17,10 +17,10 @@ | | +Search 2/2 Del | Refs -| /tmp/pardes-snap/lsprelpath/cwd/shade.zig -| 1 pub const Shade = enum { +| ../shade.zig +| ../shade.zig:1:11 pub const Shade = enum { | parent.zig -| 3 pub fn pick(s: shade.Shade) u8 { +| parent.zig:3:22 pub fn pick(s: shade.Shade) u8 { | | | @@ -56,15 +56,15 @@ |16: 0-1 d,#496656, 2-99 #b2c3b8,#1d2824, |17: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, |18: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, -|19: 0-0 d,#608269, 1-1 d,#0d1410, 2-7 d,#243a2d, 8-10 #59675c,#243a2d, 11-20 #e5eee5,#243a2d, 21-25 #f7e4a5,#5e4923, 26-26 #f7e4a5,#243a2d, 27-99 #e5eee5,#243a2d, +|19: 0-0 d,#608269, 1-1 d,#0d1410, 2-21 #59675c,#243a2d, 22-31 #e5eee5,#243a2d, 32-36 #f7e4a5,#5e4923, 37-37 #f7e4a5,#243a2d, 38-39 #e5eee5,#243a2d, 40-44 #c9a7ed,#243a2d, 45-99 #e5eee5,#243a2d, |20: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, -|21: 0-0 d,#608269, 1-7 d,#0d1410, 8-10 #59675c,#0d1410, 11-31 #e5eee5,#0d1410, 32-36 #f7e4a5,#5e4923, 37-99 #e5eee5,#0d1410, +|21: 0-0 d,#608269, 1-1 d,#0d1410, 2-19 #59675c,#0d1410, 20-23 #e5eee5,#0d1410, 24-26 #c9a7ed,#0d1410, 27-40 #e5eee5,#0d1410, 41-45 #f7e4a5,#5e4923, 46-99 #e5eee5,#0d1410, |22: 0-0 d,#608269, 1-99 d,#0d1410, |23: 0-0 d,#608269, 1-99 d,#0d1410, |24: 0-0 d,#608269, 1-99 d,#0d1410, |25: 0-0 d,#608269, 1-99 d,#0d1410, |26: 0-1 d,#add38a, 2-33 #becfc4,#314738, 34-43 #dfbce5,#314738, 44-99 #becfc4,#314738, -|27: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-16 #e5eee5,#0d1410, 17-21 #f5f7ee,#3c614a, 22-22 #f5f7ee,#0d1410, 23-24 #e5eee5,#0d1410, 25-29 #c9a7ed,#0d1410, 30-99 #e5eee5,#0d1410, +|27: 0-0 d,#e5eee5, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-16 #e5eee5,#0d1410, 17-21 #f5f7ee,#3c614a, 22-22 #f5f7ee,#0d1410, 23-24 #e5eee5,#0d1410, 25-29 #c9a7ed,#0d1410, 30-99 #e5eee5,#0d1410, |28: 0-0 d,#608269, 1-4 d,#0d1410, 5-10 #59675c,#0d1410, 11-99 #e5eee5,#0d1410, |29: 0-0 d,#608269, 1-4 d,#0d1410, 5-10 #59675c,#0d1410, 11-99 #e5eee5,#0d1410, |30: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-99 #e5eee5,#0d1410, @@ -92,9 +92,9 @@ | +Search 2/2 Del | Refs | inner/tint.zig -| 1 pub const Tint = enum { +| inner/tint.zig:1:11 pub const Tint = enum { | subdir.zig -| 3 pub fn hue(t: tint.Tint) u8 { +| subdir.zig:3:20 pub fn hue(t: tint.Tint) u8 { | | | @@ -130,15 +130,15 @@ |16: 0-1 d,#496656, 2-99 #b2c3b8,#1d2824, |17: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, |18: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, -|19: 0-0 d,#608269, 1-1 d,#0d1410, 2-7 d,#243a2d, 8-10 #59675c,#243a2d, 11-20 #e5eee5,#243a2d, 21-24 #f7e4a5,#5e4923, 25-25 #f7e4a5,#243a2d, 26-99 #e5eee5,#243a2d, +|19: 0-0 d,#608269, 1-1 d,#0d1410, 2-23 #59675c,#243a2d, 24-33 #e5eee5,#243a2d, 34-37 #f7e4a5,#5e4923, 38-38 #f7e4a5,#243a2d, 39-40 #e5eee5,#243a2d, 41-45 #c9a7ed,#243a2d, 46-99 #e5eee5,#243a2d, |20: 0-0 d,#608269, 1-1 d,#0d1410, 2-99 #e5eee5,#0d1410, -|21: 0-0 d,#608269, 1-7 d,#0d1410, 8-10 #59675c,#0d1410, 11-29 #e5eee5,#0d1410, 30-33 #f7e4a5,#5e4923, 34-99 #e5eee5,#0d1410, +|21: 0-0 d,#608269, 1-1 d,#0d1410, 2-19 #59675c,#0d1410, 20-23 #e5eee5,#0d1410, 24-26 #c9a7ed,#0d1410, 27-38 #e5eee5,#0d1410, 39-42 #f7e4a5,#5e4923, 43-99 #e5eee5,#0d1410, |22: 0-0 d,#608269, 1-99 d,#0d1410, |23: 0-0 d,#608269, 1-99 d,#0d1410, |24: 0-0 d,#608269, 1-99 d,#0d1410, |25: 0-0 d,#608269, 1-99 d,#0d1410, |26: 0-1 d,#add38a, 2-43 #becfc4,#314738, 44-52 #dfbce5,#314738, 53-99 #becfc4,#314738, -|27: 0-0 d,#608269, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-16 #e5eee5,#0d1410, 17-20 #f5f7ee,#3c614a, 21-21 #f5f7ee,#0d1410, 22-23 #e5eee5,#0d1410, 24-28 #c9a7ed,#0d1410, 29-99 #e5eee5,#0d1410, +|27: 0-0 d,#e5eee5, 1-1 d,#0d1410, 2-4 d,#0d1410,b 5-6 #616f64,#0d1410,b 7-16 #e5eee5,#0d1410, 17-20 #f5f7ee,#3c614a, 21-21 #f5f7ee,#0d1410, 22-23 #e5eee5,#0d1410, 24-28 #c9a7ed,#0d1410, 29-99 #e5eee5,#0d1410, |28: 0-0 d,#608269, 1-4 d,#0d1410, 5-10 #59675c,#0d1410, 11-99 #e5eee5,#0d1410, |29: 0-0 d,#608269, 1-4 d,#0d1410, 5-10 #59675c,#0d1410, 11-99 #e5eee5,#0d1410, |30: 0-0 d,#608269, 1-4 d,#0d1410, 5-6 #59675c,#0d1410, 7-99 #e5eee5,#0d1410, diff --git a/test/snapshots/lsprelpath.snap b/test/snapshots/lsprelpath.snap index 70ca3fdc..152a13a3 100644 --- a/test/snapshots/lsprelpath.snap +++ b/test/snapshots/lsprelpath.snap @@ -1,19 +1,18 @@ # What a language row SPELLS its path as, and that both spellings still LOOK. # -# A row is written relative to the directory of the file the query was asked -# about (lsp.rel, fed from Req.root) when the answer lives UNDER that directory, -# and keeps its full absolute path when it does not — a `gr` list was otherwise +# A row spells its path the shortest way a Look from the query pane resolves +# (Query.lookPath): relative to the pane's directory, `../` included, when that +# is shorter than the absolute path, else absolute — a `gr` list was otherwise # the same forty-character prefix repeated down the pane with the part you came -# to read pushed off the right edge, and a path outside the tree has nothing -# short to be called. +# to read pushed off the right edge. # # A row in the SAME directory shortens to a bare basename; the two that a -# prefix strip can actually get wrong are here, one app each, as `gr` rows +# careless shortening can get wrong are here, one app each, as `gr` rows # (the file headers of the asking text's query pane, line 1 `Refs`): # -# PARENT the enum is one level UP from the asking file, so nothing is -# stripped and the row carries `/tmp/.../cwd/shade.zig`. This is -# the case that must NOT become `../shade.zig`. +# PARENT the enum is one level UP from the asking file, so the row +# climbs: `../shade.zig`, shorter than `/tmp/.../cwd/shade.zig`, +# and a Look from the pane still lands on it. # SUBDIRECTORY the enum is one level DOWN, so the strip leaves a path with a # separator still in it — `inner/tint.zig`, not `tint.zig`. A # strip that took the basename instead would produce a row that @@ -29,7 +28,7 @@ file shade.zig pub const Shade = enum {\n verdigris,\n cinnabar,\n};\n file sub/inner/tint.zig pub const Tint = enum {\n ochre,\n indigo,\n};\n file sub/parent.zig const shade = @import("../shade.zig");\n\npub fn pick(s: shade.Shade) u8 {\n return switch (s) {\n \n };\n}\n file sub/subdir.zig const tint = @import("inner/tint.zig");\n\npub fn hue(t: tint.Tint) u8 {\n return switch (t) {\n \n };\n}\n -# ---- PARENT: outside the asking file's tree, so the row stays absolute ---- +# ---- PARENT: outside the asking file's tree, so the row climbs ---- start 35 100 sub/parent.zig wait 8000 fn pick stable 700 20000 @@ -39,8 +38,8 @@ stable 400 5000 key g key r # the query pane under parent.zig, line 1 `Refs`, the rows under each file: -# shade.zig's spelled whole, its directory being above -wait 15000 cwd/shade.zig +# shade.zig's spelled from above, `../shade.zig` +wait 15000 ../shade.zig:1 stable 700 15000 snap absrow # the current row is parent.zig's own; the one before it is shade.zig's, diff --git a/test/snapshots/stepgrain.golden b/test/snapshots/stepgrain.golden index a406597c..33eea330 100644 --- a/test/snapshots/stepgrain.golden +++ b/test/snapshots/stepgrain.golden @@ -14,10 +14,10 @@ |^ +Search 1/3 Del | Grep ZQMARK | a.txt -| 1 hello ZQMARK one -| 2 see also sub/b.txt for ZQMARK two +| a.txt:1:7 hello ZQMARK one +| a.txt:2:24 see also sub/b.txt for ZQMARK two | sub/b.txt -| 1 ZQMARK inside sub +| sub/b.txt:1:1 ZQMARK inside sub | | | @@ -41,10 +41,10 @@ |12: 0-1 #000000,#add38a, 2-149 #becfc4,#314738, |13: 0-0 d,#608269, 1-1 d,#0d1410, 2-149 #e5eee5,#0d1410, |14: 0-0 d,#608269, 1-1 d,#0d1410, 2-149 #e5eee5,#0d1410, -|15: 0-0 d,#608269, 1-7 d,#0d1410, 8-10 #59675c,#0d1410, 11-16 #e5eee5,#0d1410, 17-22 #f7e4a5,#5e4923, 23-23 #f7e4a5,#0d1410, 24-149 #e5eee5,#0d1410, -|16: 0-0 d,#608269, 1-1 d,#0d1410, 2-7 d,#243a2d, 8-10 #59675c,#243a2d, 11-33 #e5eee5,#243a2d, 34-39 #f7e4a5,#5e4923, 40-40 #f7e4a5,#243a2d, 41-149 #e5eee5,#243a2d, +|15: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#0d1410, 15-20 #e5eee5,#0d1410, 21-26 #f7e4a5,#5e4923, 27-27 #f7e4a5,#0d1410, 28-149 #e5eee5,#0d1410, +|16: 0-0 d,#608269, 1-1 d,#0d1410, 2-14 #59675c,#243a2d, 15-37 #e5eee5,#243a2d, 38-43 #f7e4a5,#5e4923, 44-44 #f7e4a5,#243a2d, 45-149 #e5eee5,#243a2d, |17: 0-0 d,#608269, 1-1 d,#0d1410, 2-149 #e5eee5,#0d1410, -|18: 0-0 d,#608269, 1-7 d,#0d1410, 8-10 #59675c,#0d1410, 11-16 #f7e4a5,#5e4923, 17-17 #f7e4a5,#0d1410, 18-149 #e5eee5,#0d1410, +|18: 0-0 d,#608269, 1-1 d,#0d1410, 2-18 #59675c,#0d1410, 19-24 #f7e4a5,#5e4923, 25-25 #f7e4a5,#0d1410, 26-149 #e5eee5,#0d1410, |19: 0-0 d,#608269, 1-149 d,#0d1410, |20: 0-0 d,#608269, 1-149 d,#0d1410, |21: 0-0 d,#608269, 1-149 d,#0d1410, diff --git a/test/snapshots/tagbottom.golden b/test/snapshots/tagbottom.golden index cf690a40..06798df3 100644 --- a/test/snapshots/tagbottom.golden +++ b/test/snapshots/tagbottom.golden @@ -153,8 +153,8 @@ == snap search-prompt grid=100x31 cursor=5,21 |20: /tmp/pardes-snap/tagbottom/cwd/bottom.txt Save Tty Collapse Del |21: /ZZ -|22: 1 ZZONE -|23: 2 ZZTWO +|22: bottom.txt:1:1 ZZONE +|23: bottom.txt:2:1 ZZTWO |30: ^ +Search 1/2 Del == snap leader grid=100x31 cursor=9,13 |12: 1 ZZONE SPC -- cgit v1.3