diff options
Diffstat (limited to 'core/Query.zig')
| -rw-r--r-- | core/Query.zig | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/core/Query.zig b/core/Query.zig index 2ffcfd3b..3023cc60 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); @@ -1079,7 +1085,9 @@ fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.Ar } 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, &sh, width, rel, path, line + 1, f.content[ls..le], hits[i].start - ls, @min(hits[i].end, le) - ls, shade); + // The file's line number (the `Changes` layer's lines have none: a + // hit in one says the line it stands before). + try writeHitRow(p, out, rows, &sh, width, rel, path, File.fileRow(f, line) + 1, f.content[ls..le], hits[i].start - ls, @min(hits[i].end, le) - ls, shade); i += n; } } @@ -1192,10 +1200,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 +1266,23 @@ 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 ""; - const path_at: u32 = @intCast(q.found_bytes.items.len); - q.found_bytes.appendSlice(p.gpa, t.path) catch return; - const code_at: u32 = @intCast(q.found_bytes.items.len); + // 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, name.len) orelse continue, + .line = std.math.cast(u32, t.at.line) orelse continue, + .col = std.math.cast(u32, t.at.col -| 1) orelse continue, + .end = std.math.cast(u32, if (t.at.end_col > 0) t.at.end_col else t.at.col) orelse continue, + .code = std.math.cast(u32, q.found_bytes.items.len + name.len) orelse return, + .code_len = std.math.cast(u32, code.len) orelse continue, + }; + q.found_bytes.appendSlice(p.gpa, name) catch return; q.found_bytes.appendSlice(p.gpa, code) catch return; - q.found.append(p.gpa, .{ - .path = path_at, - .path_len = @intCast(t.path.len), - .line = @intCast(t.at.line), - .col = @intCast(t.at.col -| 1), - .end = @intCast(if (t.at.end_col > 0) t.at.end_col else t.at.col), - .code = code_at, - .code_len = @intCast(code.len), - }) catch return; + q.found.append(p.gpa, found) catch return; }, } } @@ -1353,13 +1366,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 }); @@ -1710,7 +1724,8 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void 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); + // The server's line is the file's (File.bufferRow). + const ls = File.lineStart(p.gpa, f, File.bufferRow(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); @@ -2088,7 +2103,7 @@ pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, body_h: u16) while (vr < body_h) : (vr += 1) { const w = pane.wrapAt(p.settings.tab, vr); const row = w.line; - if (row == last or row < 1 or w.ghost) continue; + if (row == last or row < 1) continue; last = row; const i: usize = @intCast(row - 1); if (i >= q.items.items.len or @as(usize, @intCast(row)) >= f.location_rows.len) continue; @@ -2746,3 +2761,14 @@ test "a query row's address reaches every way a look arrives: an empty look writ try std.testing.expectEqual(tree.Status.ok, @import("ninep/harness.zig").wr(p, tree.Node.of(qp.serial, .look), line).reply.status); try std.testing.expectEqual(@as(i32, 3), a.body.cur_row); } + +test "Grep: a row whose numbers a Found cannot hold (a column past u32) is passed over, never cast" { + const gpa = std.testing.allocator; + const p = try @import("ninep/harness.zig").withFile(gpa, "x\n"); + defer p.deinit(); + var q: Query = .{ .owner = 0 }; + defer q.deinit(p.gpa); + take(p, &q, .grep, "a:5000000000:b:3:1-4 hit\nok.txt:2:3-5 hit\nb:7:4294967297 hit\n"); + // Taken or passed over, each: never cast (main: Query.zig:1267). + try std.testing.expect(q.found.items.len <= 3); +} |
