diff options
Diffstat (limited to 'src/locations.zig')
| -rw-r--r-- | src/locations.zig | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/locations.zig b/src/locations.zig index 6f086214..cdb228a6 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -814,6 +814,40 @@ pub const Cache = struct { return .{ .analysis = next.analysis }; } + /// The analysis kept for `path` with its colours, if any, of whatever + /// source it was made from (a caller compares the lines it uses): + /// never parses. Borrowed, as `get`'s. + pub fn peek(cache: *const Cache, path: []const u8) ?struct { source: []const u8, colors: []const u8 } { + for (cache.entries[0..cache.len]) |entry| if (entry.colors_ready and std.mem.eql(u8, entry.path, path)) + return .{ .source = entry.source, .colors = entry.analysis.colors }; + return null; + } + + /// `source`'s colours, worked out elsewhere (a worker's, Stream.paintRows), + /// kept for `path` in place of what was: copies of both. + pub fn put(cache: *Cache, gpa: std.mem.Allocator, path: []const u8, source: []const u8, colors: []const u8) !void { + if (path.len + source.len + colors.len > max_bytes) return error.SourceTooLarge; + const owned_path = try gpa.dupe(u8, path); + errdefer gpa.free(owned_path); + const owned_source = try gpa.dupe(u8, source); + errdefer gpa.free(owned_source); + const next: Entry = .{ + .path = owned_path, + .source = owned_source, + .analysis = .{ .colors = try gpa.dupe(u8, colors) }, + .declarations_ready = false, + .colors_ready = true, + }; + for (cache.entries[0..cache.len], 0..) |entry, index| if (std.mem.eql(u8, entry.path, path)) { + cache.remove(gpa, index); + break; + }; + while (cache.len == max_entries or cache.bytes > max_bytes - next.size()) cache.remove(gpa, 0); + cache.entries[cache.len] = next; + cache.len += 1; + cache.bytes += next.size(); + } + fn remove(cache: *Cache, gpa: std.mem.Allocator, index: usize) void { cache.bytes -= cache.entries[index].size(); cache.entries[index].deinit(gpa); |
