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 --- src/Query.zig | 443 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 395 insertions(+), 48 deletions(-) (limited to 'src/Query.zig') 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; + } +} -- cgit v1.3