diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Query.zig | 41 | ||||
| -rw-r--r-- | src/Stream.zig | 73 | ||||
| -rw-r--r-- | src/locations.zig | 24 |
3 files changed, 115 insertions, 23 deletions
diff --git a/src/Query.zig b/src/Query.zig index 2f417a68..dfe23046 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -372,24 +372,8 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved { const f = &rp.file.?; const content = try p.gpa.dupe(u8, f.content); errdefer p.gpa.free(content); - const rows = try p.gpa.alloc(locations.Row, f.location_rows.len); - var made: usize = 0; - errdefer { - for (rows[0..made]) |r| { - p.gpa.free(r.path); - p.gpa.free(r.colors); - } - p.gpa.free(rows); - } - for (f.location_rows, 0..) |r, i| { - rows[i] = r; - rows[i].path = try p.gpa.dupe(u8, r.path); - rows[i].colors = p.gpa.dupe(u8, r.colors) catch |err| { - p.gpa.free(rows[i].path); - return err; - }; - made += 1; - } + const rows = try locations.cloneRows(p.gpa, f.location_rows); + errdefer locations.freeRows(p.gpa, rows); const asked = try p.gpa.dupe(u8, q.asked); errdefer p.gpa.free(asked); return .{ .content = content, .rows = rows, .asked = asked, .source = q.source, .items = try p.gpa.dupe(Item, q.items.items), .current = q.current, .total = q.total, .k = q.k }; @@ -865,18 +849,29 @@ pub fn recolor(p: *Pardes) void { const f = &rp.file.?; var sh: Shades = .{}; var lines = std.mem.splitScalar(u8, f.content, '\n'); - var changed = false; - for (f.location_rows) |row| { + // The rows are a blob (File.State): a digest keeps its word for + // them and a rollback copy pins them, so they are made again, never + // written. Written in place, a session's digest kept the word of + // their colours before; a client joining hashed them after, and + // loaded "as another session" (and so did a replica that hashed them + // at another time). + var fresh: ?[]locations.Row = null; + for (f.location_rows, 0..) |row, i| { 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; + const rows = fresh orelse locations.cloneRows(p.gpa, f.location_rows) catch break; + fresh = rows; + @memcpy(rows[i].colors[0..got.len], got); + } + if (fresh) |rows| { + locations.freeRows(p.gpa, f.location_rows); + f.location_rows = rows; + dropHighlights(p, f); } - if (changed) dropHighlights(p, f); } } diff --git a/src/Stream.zig b/src/Stream.zig index 263a6628..69dc311f 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -1647,6 +1647,79 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work Stats.print("other frames", idle.items); } +test "Stream: a Grep's rows recoloured are made again, never written, and a dump of them dropped by an edit loads to its digest" { + if (comptime !filesystem.platform_has_fs) return; + if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + // Rows enough that a digest keeps a word for them (walk's blob_min). + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..80) |i| try text.print(testing.allocator, "pub const value_{d}: u32 = {d};\n", .{ i, i }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text.items }); + 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(); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + var shell: TestShell = .init(); + shell.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.?; + const coloured = struct { + fn count(rows: []const @import("locations.zig").Row) usize { + var n: usize = 0; + for (rows) |row| n += @intFromBool(row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + return n; + } + }.count; + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + // The rows, plain, digested (their word kept) before their colours land. + while (now() < until and f.location_rows.len < 80) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expect(f.location_rows.len >= 80 and coloured(f.location_rows) == 0); + _ = pardes.walk.digest(Pardes, p); + const plain = f.location_rows.ptr; + while (now() < until and (coloured(f.location_rows) < 80 or p.stream.rows_paint != null)) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 80), coloured(f.location_rows)); + // Made again: a Debug build's digest finds no blob written in place + // (the plain rows' kept word is theirs), and the rows are new ones. + _ = pardes.walk.digest(Pardes, p); + try testing.expect(f.location_rows.ptr != plain); + // An edit drops them with the text they were for (dropped_marks): a + // join's load of the session is it, digest for digest. + panes.File.setContent(p, f, try p.gpa.dupe(u8, "value\n")); + try testing.expect(f.dropped_marks != null); + const bytes = try pardes.dump.dumpBytes(p); + defer p.gpa.free(bytes); + const q = try pardes.dump.load(testing.allocator, p.opts, bytes, null); + defer q.deinit(); + try testing.expectEqual(pardes.walk.digest(panes.File.State, f), pardes.walk.digest(panes.File.State, &q.panes[qid].?.file.?)); +} + 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; diff --git a/src/locations.zig b/src/locations.zig index 40f22f85..d0904607 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -36,6 +36,30 @@ pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { freeRaw(gpa, rows); } +/// A copy of `rows` and all they own, gpa's: rows are a blob (File.State +/// `location_rows`), made again rather than written. +pub fn cloneRows(gpa: std.mem.Allocator, rows: []const Row) ![]Row { + const out = try gpa.alloc(Row, rows.len); + var made: usize = 0; + errdefer { + for (out[0..made]) |r| { + gpa.free(r.path); + gpa.free(r.colors); + } + gpa.free(out); + } + for (rows, out) |r, *o| { + o.* = r; + o.path = try gpa.dupe(u8, r.path); + o.colors = gpa.dupe(u8, r.colors) catch |err| { + gpa.free(o.path); + return err; + }; + made += 1; + } + return out; +} + /// A list of rows being built, given up: each row's own bytes, then the list. pub fn freeRowList(gpa: std.mem.Allocator, rows: *std.ArrayList(Row)) void { for (rows.items) |row| { |
