summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 17:50:13 -0300
committerGabriel Schneider <[email protected]>2026-11-23 17:58:08 -0300
commit4eb608b0c1f231005237b0f57283d22bfccee0a9 (patch)
treea04992918515c2dbdea0837c3de5d854ac65d5bf /core
parentfcc08ec6ceb9af11aeda8489cef0d4085878ef48 (diff)
downloadpardes-4eb608b0c1f231005237b0f57283d22bfccee0a9.tar.gz
pardes-4eb608b0c1f231005237b0f57283d22bfccee0a9.zip
a file's name in a row is written as git writes an unusual one, and read back so: a newline in a name no longer makes a row of its own: the Grep worker wrote `path:line:col-end code` with the name as it is (core/Stream.zig `row`, and Find's `file`), so a file named `a\nb:1:1` holding a hit made two rows, `a` (no place, passed over) and a hit in a file `b` at line 1, which a crafted name can point anywhere with any text (the test below on the base: the one hit's name `b`); the query pane's rows the same (a name with a newline was two lines of the pane's text over one row's metadata); and a name with a `:` and digits in it read as another file at a line (`a:5:b.txt:3`); THE RULE, one for every row that names a file: git's quote_c_style, `look.quoted`: a name holding a control byte, a `"`, a `\` or a `:` is written in double quotes with `\n \t \r \" \\` and `\ooo` for the other control bytes, any other name as it is; WRITTEN by the worker (Stream `row`, `file`), the query pane's rows (Query: a Grep's, a language server's, a pane's places: the name `lookPath` gives, quoted), the patch view's labels (hunks.zig `flush`); READ by `look.parsePathLine`, which takes a `"`-led word whole to its closing quote as the path (its `:` its own; the place after it as ever), Query `take` (the name unquoted, `look.unquoted`, into the Found it opens) and Find's rows (the name a row shows opens the file it names); a B3 on a quoted row whose name has an escape finds the escaped text and misses (no file has it): a miss, never another file; the patch view's names come from the diff, where git quotes the same way (diff.zig `cutPath` keeps the inside), and Find's rows show the name as the worker wrote it; tests: core/Stream.zig "a file named `a\nb:1:1` is one Grep row and one Find name, its own, for the walk and rg; a row is a line" (fails on the base), core/look.zig "a file's name written as a row writes it reads back whole: ..."; core-test Debug 953/1119 (166 skipped)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
-rw-r--r--core/Query.zig30
-rw-r--r--core/Stream.zig51
-rw-r--r--core/hunks.zig6
-rw-r--r--core/look.zig94
4 files changed, 165 insertions, 16 deletions
diff --git a/core/Query.zig b/core/Query.zig
index 7bc30c9d..a35f5797 100644
--- a/core/Query.zig
+++ b/core/Query.zig
@@ -486,7 +486,12 @@ fn makeNameRows(p: *Pardes, id: usize) !void {
var meta: locations.Row = .{ .kind = .match, .path = &.{}, .at = .{}, .location_end = 0, .code_start = 0, .mark_occurrence = false };
var pathbuf: [4096 + 32]u8 = undefined;
switch (q.source) {
- .find => meta.path = @constCast(std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), row }) catch row),
+ .find => {
+ // A name the worker quoted (look.quoted) opens as it is named.
+ var namebuf: [4096]u8 = undefined;
+ const name = if (row.len > 1 and row[0] == '"' and row[row.len - 1] == '"') look.unquoted(&namebuf, row[1 .. row.len - 1]) else row;
+ meta.path = @constCast(std.fmt.bufPrint(&pathbuf, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), name }) catch row);
+ },
.recent => {
const t = look.parsePathLine(row);
meta.path = @constCast(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 t.path);
@@ -1030,7 +1035,8 @@ fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.Ar
const index = try File.lineIndex(p.gpa, f);
const path = addressOf(p.scratch.allocator(), tp);
var relbuf: [4096]u8 = undefined;
- const rel = lookPath(&relbuf, path, dir);
+ var namebuf: [8192]u8 = undefined;
+ const rel = std.fmt.bufPrint(&namebuf, "{f}", .{look.quoted(lookPath(&relbuf, path, dir))}) catch lookPath(&relbuf, path, dir);
const width = addressWidth(rel, index.len);
var sh: Shades = .{};
defer sh.done(p);
@@ -1192,10 +1198,11 @@ fn makeGrepRows(p: *Pardes, id: usize, full: bool) !void {
defer sh.done(p);
var width: usize = 0;
var relbuf: [4096]u8 = undefined;
+ var namebuf: [8192]u8 = undefined;
for (q.found.items[lo..hi], lo..) |fd, i| {
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);
+ const rel = std.fmt.bufPrint(&namebuf, "{f}", .{look.quoted(lookPath(&relbuf, path, dir))}) catch lookPath(&relbuf, path, dir);
if (!std.mem.eql(u8, name, last_path)) {
last_path = name;
try q.items.append(p.gpa, .{ .kind = .header });
@@ -1257,19 +1264,21 @@ pub fn take(p: *Pardes, q: *Query, kind: Stream.Kind, rows: []const u8) void {
const t = look.parsePathLine(ln);
if (t.at.line == 0 or t.path.len == 0) continue;
const code = if (t.end < ln.len) ln[t.end + 1 ..] else "";
- // A row is a line's text, a file's name in it unescaped (one
- // named `a:5000000000:b` reads as `a` at line 5000000000): one
- // whose numbers a Found cannot hold is passed over.
+ // The worker writes a name that needs it quoted (look.quoted:
+ // a newline in it was a row of its own); one whose numbers a
+ // Found cannot hold is passed over.
+ var namebuf: [4096]u8 = undefined;
+ const name = if (ln[0] == '"') look.unquoted(&namebuf, t.path) else t.path;
const found: Found = .{
.path = std.math.cast(u32, q.found_bytes.items.len) orelse return,
- .path_len = std.math.cast(u16, t.path.len) orelse continue,
+ .path_len = std.math.cast(u16, name.len) orelse continue,
.line = std.math.cast(u32, t.at.line) orelse continue,
.col = std.math.cast(u32, t.at.col -| 1) orelse continue,
.end = std.math.cast(u32, if (t.at.end_col > 0) t.at.end_col else t.at.col) orelse continue,
- .code = std.math.cast(u32, q.found_bytes.items.len + t.path.len) orelse return,
+ .code = std.math.cast(u32, q.found_bytes.items.len + name.len) orelse return,
.code_len = std.math.cast(u32, code.len) orelse continue,
};
- q.found_bytes.appendSlice(p.gpa, t.path) catch return;
+ q.found_bytes.appendSlice(p.gpa, name) catch return;
q.found_bytes.appendSlice(p.gpa, code) catch return;
q.found.append(p.gpa, found) catch return;
},
@@ -1355,13 +1364,14 @@ fn makeListRows(p: *Pardes, id: usize, pat: []const u8) !void {
defer sh.done(p);
var width: usize = 0;
var relbuf: [4096]u8 = undefined;
+ var namebuf: [8192]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);
+ const rel = std.fmt.bufPrint(&namebuf, "{f}", .{look.quoted(lookPath(&relbuf, path, dir))}) catch 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 });
diff --git a/core/Stream.zig b/core/Stream.zig
index 7956e4f2..7f9ad012 100644
--- a/core/Stream.zig
+++ b/core/Stream.zig
@@ -832,13 +832,13 @@ const Worker = struct {
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] });
+ try w.rows.print(w.gpa, "{f}:{d}:{d}{c}{d} {s}\n", .{ look.quoted(path), line, from + 1, config.range_sep, end, ln[0..cut] });
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});
+ try w.rows.print(w.gpa, "{f}\n", .{look.quoted(path)});
w.total += 1;
try w.maybeSend();
return w.checkCap();
@@ -1275,6 +1275,53 @@ test "Stream: the listing of files for Find on a worker, a cap of hits stops the
try testing.expectEqual(State.done, p.stream.state);
}
+test "Stream: a file named `a\\nb:1:1` is one Grep row and one Find name, its own, for the walk and rg; a row is a line" {
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var root_buf: [4096]u8 = undefined;
+ // Up to 2026-11-23 its row, `a\nb:1:1:1:1-6 needle`, was two: `a`
+ // (no place) and a hit in a file `b`, line 1, that has none.
+ const odd = "a\nb:1:1";
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = odd, .data = "needle\n" });
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "b", .data = "hay\n" });
+ const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)];
+ for ([_]Engine{ .walk, .rg }) |engine| {
+ 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.Io.Dir.path.join(p.gpa, &.{ root, "main.txt" });
+ p.advance(1);
+ p.stream.engine = engine;
+ // No host workers: the search runs on the loop at the next tick.
+ panes.Query.open(p, 0, .grep, "", false);
+ const qp = for (p.panes) |slot| {
+ if (slot) |qp| if (qp.query != null) break qp;
+ } else return error.NoQueryPane;
+ const q = qp.query.?;
+ try query(p, qp.serial, .grep, "needle", root, .now);
+ p.update(.tick);
+ p.update(.tick);
+ if (engine == .rg and p.stream.used != .rg) return; // no rg here
+ try testing.expectEqual(@as(usize, 1), q.found.items.len);
+ const fd = q.found.items[0];
+ try testing.expectEqualStrings(odd, q.found_bytes.items[fd.path..][0..fd.path_len]);
+ try testing.expectEqual(@as(u32, 1), fd.line);
+ // The pane's rows: a line each, its name's newline written `\n`.
+ const f = &qp.file.?;
+ try testing.expectEqual(std.mem.count(u8, f.content, "\n"), f.location_rows.len);
+ try testing.expect(std.mem.indexOf(u8, f.content, "\"a\\nb:1:1\"") != null);
+ // Find: the same name, once, opening that file.
+ panes.Query.open(p, 0, .find, "", false);
+ p.update(.tick);
+ p.update(.tick);
+ const fq = for (p.panes) |slot| {
+ if (slot) |fp| if (fp.query) |got| if (got.source == .find) break got;
+ } else return error.NoFindPane;
+ try testing.expectEqual(@as(usize, 2), fq.names.cands.items.len);
+ }
+}
+
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 !syntax.minimal_grammars) return error.SkipZigTest;
const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 });
diff --git a/core/hunks.zig b/core/hunks.zig
index d8c5e2de..bdebc34d 100644
--- a/core/hunks.zig
+++ b/core/hunks.zig
@@ -15,6 +15,7 @@ const std = @import("std");
const diff = @import("diff.zig");
const syntax = @import("syntax.zig");
const locations = @import("locations.zig");
+const look = @import("look.zig");
const pardes = @import("root.zig");
const Pardes = pardes.Pardes;
@@ -385,7 +386,7 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, scratch: *[
var row = declaration.start_line;
while (row <= declaration.header_end_line and row < start_row) : (row += 1) {
if (row == declaration.start_line and ctx.tslocations) {
- const label = try std.fmt.allocPrint(arena, "{s}:{d}", .{ shown, row + 1 });
+ const label = try std.fmt.allocPrint(arena, "{f}:{d}", .{ look.quoted(shown), row + 1 });
try out.row(arena, label, .{ .kind = .declaration, .path = shown, .line = row + 1, .location_end = label.len, .code_start = label.len });
}
// A blank where a hunk line has its marker: the code aligns.
@@ -396,7 +397,8 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, scratch: *[
// The hunk's label: a step for n and N, and a look's place.
const line_no = change_row + 1;
var label: std.Io.Writer.Allocating = .init(arena);
- try label.writer.print("{s}:{d}", .{ shown, line_no });
+ // The name as a row writes it (look.quoted): a `:` in it never splits it.
+ try label.writer.print("{f}:{d}", .{ look.quoted(shown), line_no });
const location_end = label.written().len;
if (depth > 0) {
try label.writer.writeByte(' ');
diff --git a/core/look.zig b/core/look.zig
index 41b48393..fe5c898d 100644
--- a/core/look.zig
+++ b/core/look.zig
@@ -67,13 +67,89 @@ fn num(tok: []const u8, i: usize) struct { v: usize, end: usize } {
return .{ .v = v, .end = j };
}
+/// A file's name in a row (a Grep's, Find's, a query pane's, the patch
+/// view's), written as git writes an unusual one (quote_c_style): one with
+/// a control byte, a `"`, a `\` or a `:` in double quotes, `\n \t \r \" \\`
+/// and `\ooo` for any other control byte, so that a row is one line and its
+/// name never splits at a `:`. Any other name as it is.
+pub fn quoted(name: []const u8) Quoted {
+ return .{ .name = name };
+}
+
+pub const Quoted = struct {
+ name: []const u8,
+
+ pub fn format(q: Quoted, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ for (q.name) |c| {
+ if (c < 0x20 or c == 0x7f or c == '"' or c == '\\' or c == config.line_col_sep) break;
+ } else return w.writeAll(q.name);
+ try w.writeByte('"');
+ for (q.name) |c| switch (c) {
+ '\n' => try w.writeAll("\\n"),
+ '\t' => try w.writeAll("\\t"),
+ '\r' => try w.writeAll("\\r"),
+ '"', '\\' => try w.print("\\{c}", .{c}),
+ 0...0x08, 0x0b, 0x0c, 0x0e...0x1f, 0x7f => try w.print("\\{o:0>3}", .{c}),
+ else => try w.writeByte(c),
+ };
+ try w.writeByte('"');
+ }
+};
+
+/// A quoted name's inside (parsePathLine's path of a `"`-led word) read
+/// back into `buf` (escapes only shrink it: `buf` as long as `inside` is
+/// enough).
+pub fn unquoted(buf: []u8, inside: []const u8) []const u8 {
+ var n: usize = 0;
+ var i: usize = 0;
+ while (i < inside.len and n < buf.len) : (n += 1) {
+ const c = inside[i];
+ i += 1;
+ buf[n] = c;
+ if (c != '\\' or i == inside.len) continue;
+ const e = inside[i];
+ i += 1;
+ buf[n] = switch (e) {
+ 'n' => '\n',
+ 't' => '\t',
+ 'r' => '\r',
+ 'a' => 0x07,
+ 'b' => 0x08,
+ 'v' => 0x0b,
+ 'f' => 0x0c,
+ else => e,
+ };
+ // `\ooo`: three octal digits.
+ if (i + 2 <= inside.len) if (std.fmt.parseInt(u8, inside[i - 1 .. i + 2], 8)) |v| {
+ buf[n] = v;
+ i += 2;
+ } else |_| {};
+ }
+ return buf[0..n];
+}
+
+/// Where a `"`-led word's quoted name closes (`quoted`'s), past its
+/// escapes; null when it is no quoted name.
+fn quoteEnd(tok: []const u8) ?usize {
+ if (tok.len < 2 or tok[0] != '"') return null;
+ var i: usize = 1;
+ while (i < tok.len) : (i += 1) switch (tok[i]) {
+ '\\' => i += 1,
+ '"' => return i,
+ else => {},
+ };
+ return null;
+}
+
pub fn parsePathLine(tok: []const u8) struct { path: []const u8, at: Spot, end: usize } {
- var sep: usize = 0;
+ // A quoted name (a row's: `quoted`) is the path whole, its `:` its own.
+ const close = quoteEnd(tok);
+ var sep: usize = if (close) |c| c + 1 else 0;
while (sep < tok.len) : (sep += 1) {
if (tok[sep] != config.line_col_sep) continue;
const l = num(tok, sep + 1);
if (l.end == sep + 1) continue; // no digits after ':'
- const path = tok[0..sep];
+ const path = if (close) |c| tok[1..c] else tok[0..sep];
var i = l.end;
if (i < tok.len and tok[i] == config.range_sep) {
const e = num(tok, i + 1);
@@ -2038,6 +2114,20 @@ test "a Look whose column is past its line's end lands at the end, however big;
try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
}
+test "a file's name written as a row writes it reads back whole: a newline, a `:5:`, a quote, a control byte; a plain name as it is" {
+ var row: [256]u8 = undefined;
+ var back: [256]u8 = undefined;
+ for ([_][]const u8{ "a\nb:1:1", "a:5:b.txt", "say \"hi\".txt", "bell\x07\x1b[31m", "back\\slash", "plain/name.zig" }) |name| {
+ const written = try std.fmt.bufPrint(&row, "{f}:12:3-5 code", .{quoted(name)});
+ try std.testing.expect(std.mem.indexOfScalar(u8, written, '\n') == null);
+ const got = parsePathLine(written);
+ try std.testing.expectEqual(@as(usize, 12), got.at.line);
+ try std.testing.expectEqual(@as(usize, 3), got.at.col);
+ try std.testing.expectEqualStrings(name, if (written[0] == '"') unquoted(&back, got.path) else got.path);
+ }
+ try std.testing.expectEqualStrings("plain/name.zig:1", try std.fmt.bufPrint(&row, "{f}:1", .{quoted("plain/name.zig")}));
+}
+
test "@p names a pane by its serial, as /index does" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 });
defer p.deinit();