diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:07:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-04 23:07:26 -0300 |
| commit | 398270fb7e121cbaab9e264a39bc83b5d32c40c7 (patch) | |
| tree | 3b1be9a8d97874ede9e62ecb4d0ac42ff3aa1329 | |
| parent | cdbb06a5de858a721bdbfa127ea839f791b0dc24 (diff) | |
| download | pardes-398270fb7e121cbaab9e264a39bc83b5d32c40c7.tar.gz pardes-398270fb7e121cbaab9e264a39bc83b5d32c40c7.zip | |
locations: one appendRow copies a row's path and colours onto a list being built, for appendMetadata, hunks' Out.row and Query's writeRow, which each copied, appended and freed on failure by hand
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/Query.zig | 23 | ||||
| -rw-r--r-- | src/hunks.zig | 11 | ||||
| -rw-r--r-- | src/locations.zig | 17 |
3 files changed, 23 insertions, 28 deletions
diff --git a/src/Query.zig b/src/Query.zig index 5fdfff2a..87c9d3bd 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -676,27 +676,20 @@ fn noteRows(p: *Pardes, id: usize, note: []const u8) void { setRows(p, rp, &out, &rows); } -/// One row: its text and its metadata, the path copied. +/// One row: its text and its metadata, the path and colours copied. fn writeRow(p: *Pardes, out: *std.ArrayList(u8), rows: *std.ArrayList(locations.Row), text: []const u8, meta: locations.Row) !void { try out.appendSlice(p.gpa, text); try out.append(p.gpa, '\n'); - var m = meta; - m.path = try p.gpa.dupe(u8, meta.path); - errdefer p.gpa.free(m.path); // Coloured whole (zeros where nothing coloured it): the pane's syntax // pass then parses no fragment of it, per refill. const code = text[@min(meta.code_start, text.len)..]; - m.colors = if (meta.colors.len >= code.len) try p.gpa.dupe(u8, meta.colors[0..code.len]) else blk: { - const c = try p.gpa.alloc(u8, code.len); - @memset(c, 0); - @memcpy(c[0..meta.colors.len], meta.colors); - break :blk c; - }; - rows.append(p.gpa, m) catch |err| { - p.gpa.free(m.path); - p.gpa.free(m.colors); - return err; - }; + var m = meta; + if (meta.colors.len >= code.len) m.colors = meta.colors[0..code.len] else { + m.colors = try p.scratch.allocator().alloc(u8, code.len); + @memset(m.colors, 0); + @memcpy(m.colors[0..meta.colors.len], meta.colors); + } + try locations.appendRow(p.gpa, rows, m); } /// The rows made into the pane: line 1 as it is (the user's), then `out`, diff --git a/src/hunks.zig b/src/hunks.zig index 8cd5c43a..ae7b5966 100644 --- a/src/hunks.zig +++ b/src/hunks.zig @@ -125,21 +125,16 @@ const Out = struct { /// One row: its text, a newline, and its metadata. fn row(out: *Out, text: []const u8, path: []const u8, meta: Meta) !void { - const gpa = out.p.gpa; try out.text.writer.writeAll(text); try out.text.writer.writeByte('\n'); - const owned_path = try gpa.dupe(u8, path); - errdefer gpa.free(owned_path); - const colors = try gpa.dupe(u8, meta.colors); - errdefer gpa.free(colors); - try out.rows.append(gpa, .{ + try locations.appendRow(out.p.gpa, &out.rows, .{ .kind = meta.kind, .declaration = meta.declaration, - .path = owned_path, + .path = @constCast(path), .at = meta.at, .location_end = meta.location_end, .code_start = meta.code_start, - .colors = colors, + .colors = @constCast(meta.colors), }); } }; diff --git a/src/locations.zig b/src/locations.zig index b14b3a2c..889a45a0 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -82,6 +82,17 @@ pub fn freeRowList(gpa: std.mem.Allocator, rows: *std.ArrayList(Row)) void { rows.deinit(gpa); } +/// `row` onto a list being built, with copies of its own of the path and +/// the colours it borrows. +pub fn appendRow(gpa: std.mem.Allocator, rows: *std.ArrayList(Row), row: Row) !void { + var owned = row; + owned.path = try gpa.dupe(u8, row.path); + errdefer gpa.free(owned.path); + owned.colors = try gpa.dupe(u8, row.colors); + errdefer gpa.free(owned.colors); + try rows.append(gpa, owned); +} + pub fn equalRows(a: []const Row, b: []const Row) bool { if (a.len != b.len) return false; for (a, b) |left, right| { @@ -601,11 +612,7 @@ fn writeCode(writer: *std.Io.Writer, rows: []const Pending, index: usize) !void } fn appendMetadata(gpa: std.mem.Allocator, rows: *std.ArrayList(Row), pending: Pending, kind: @FieldType(Row, "kind"), location_end: usize, code_start: usize, source: bool) !void { - const path = try gpa.dupe(u8, pending.path); - errdefer gpa.free(path); - const colors = try gpa.dupe(u8, if (source) pending.colors else &.{}); - errdefer gpa.free(colors); - try rows.append(gpa, .{ .kind = kind, .declaration = pending.declaration, .mark_occurrence = pending.mark_occurrence, .source_column = pending.source_column, .path = path, .at = pending.at, .location_end = location_end, .code_start = code_start, .colors = colors }); + try appendRow(gpa, rows, .{ .kind = kind, .declaration = pending.declaration, .mark_occurrence = pending.mark_occurrence, .source_column = pending.source_column, .path = @constCast(pending.path), .at = pending.at, .location_end = location_end, .code_start = code_start, .colors = @constCast(if (source) pending.colors else &.{}) }); } const alignment_entries = 8; |
