summaryrefslogtreecommitdiff
path: root/src/locations.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/locations.zig')
-rw-r--r--src/locations.zig583
1 files changed, 570 insertions, 13 deletions
diff --git a/src/locations.zig b/src/locations.zig
index b34f7a6f..cdb228a6 100644
--- a/src/locations.zig
+++ b/src/locations.zig
@@ -4,7 +4,6 @@ const look = @import("look.zig");
const syntax = @import("syntax.zig");
const filesystem = @import("fs.zig");
-pub const Config = @import("locations_config.zig").Config;
const neighbor_context: u8 = 1;
const declaration_context: u8 = 2;
const declaration_start: u8 = 4;
@@ -17,6 +16,8 @@ pub fn isContextLine(line: []const u8) bool {
pub const Row = struct {
kind: enum { match, context, preview },
declaration: bool = false,
+ mark_occurrence: bool = true,
+ source_column: usize = 0,
path: []u8,
at: look.Spot,
location_end: usize,
@@ -32,10 +33,19 @@ pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void {
gpa.free(rows);
}
+/// 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| {
+ gpa.free(row.path);
+ gpa.free(row.colors);
+ }
+ rows.deinit(gpa);
+}
+
pub fn equalRows(a: []const Row, b: []const Row) bool {
if (a.len != b.len) return false;
for (a, b) |left, right| {
- if (left.kind != right.kind or left.declaration != right.declaration or !std.meta.eql(left.at, right.at) or
+ if (left.kind != right.kind or left.declaration != right.declaration or left.mark_occurrence != right.mark_occurrence or left.source_column != right.source_column or !std.meta.eql(left.at, right.at) or
left.location_end != right.location_end or left.code_start != right.code_start or
!std.mem.eql(u8, left.path, right.path) or !std.mem.eql(u8, left.colors, right.colors)) return false;
}
@@ -120,7 +130,8 @@ pub fn parse(line: []const u8) Parsed {
return parseRaw(line);
}
-fn parseRaw(line: []const u8) Parsed {
+/// Native producer rows have one separator byte before untouched source.
+pub fn parseRaw(line: []const u8) Parsed {
var at: usize = 1;
while (at < line.len) : (at += 1) {
if (line[at] != ':' or at + 1 == line.len or !std.ascii.isDigit(line[at + 1])) continue;
@@ -149,6 +160,8 @@ const Pending = struct {
original: ?usize,
hidden: bool = false,
declaration: bool = false,
+ mark_occurrence: bool = true,
+ source_column: usize = 0,
depth: usize = 0,
colors: []const u8 = &.{},
@@ -160,7 +173,9 @@ const Pending = struct {
const Source = struct { path: []const u8, bytes: []const u8 };
-fn sourceText(p: *pardes.Pardes, arena: std.mem.Allocator, dir: []const u8, path: []const u8) ?Source {
+/// A file's text as a look from `dir` finds it: an open pane's, unsaved
+/// edits and all, else the disk's. Null for a PDF or one not there.
+pub fn sourceText(p: *pardes.Pardes, arena: std.mem.Allocator, dir: []const u8, path: []const u8) ?Source {
if (std.ascii.eqlIgnoreCase(std.fs.path.extension(path), ".pdf")) return null;
const lexical = std.fs.path.resolvePosix(arena, &.{ dir, path }) catch return null;
var pathbuf: [4096]u8 = undefined;
@@ -173,6 +188,229 @@ fn sourceText(p: *pardes.Pardes, arena: std.mem.Allocator, dir: []const u8, path
return .{ .path = lexical, .bytes = arena.dupe(u8, bytes) catch return null };
}
+/// Byte boundary after the guides in a call tree, independent of source indentation.
+pub fn treePrefix(line: []const u8) struct { end: usize, branch: bool } {
+ var at: usize = 0;
+ while (at < line.len) {
+ for ([_][]const u8{ "╰──┬ ", "├──╮ ", "╰──╮ ", "├─── ", "╰─── ", "├──┬ ", "└──┬ ", "└─── " }) |branch| {
+ if (std.mem.startsWith(u8, line[at..], branch)) return .{ .end = at + branch.len, .branch = true };
+ }
+ if (std.mem.startsWith(u8, line[at..], "├─ ") or std.mem.startsWith(u8, line[at..], "└─ ") or std.mem.startsWith(u8, line[at..], "╰─ "))
+ return .{ .end = at + "└─ ".len, .branch = false };
+ if (std.mem.startsWith(u8, line[at..], "│")) {
+ at += "│".len;
+ } else if (line[at] == ' ') {
+ at += 1;
+ } else break;
+ }
+ return .{ .end = at, .branch = false };
+}
+
+/// The same tree columns carried across a soft-wrapped display row. This is
+/// presentation only: source bytes and Look addresses remain untouched.
+pub fn treeContinuation(line: []const u8, buffer: []u8, max_cells: usize) []const u8 {
+ const prefix = line[0..treePrefix(line).end];
+ var out: std.Io.Writer = .fixed(buffer);
+ var it = (std.unicode.Utf8View.init(prefix) catch return "").iterator();
+ var cells: usize = 0;
+ var end: usize = 0;
+ while (it.nextCodepoint()) |cp| {
+ if (cells >= max_cells) break;
+ out.writeAll(if (cp == '│' or cp == '├' or cp == '┬' or cp == '╮') "│" else " ") catch break;
+ cells += 1;
+ if (cp != ' ') end = out.buffered().len;
+ }
+ if (end == 0) return "";
+ // Keep one cell between the continuation and the wrapped source text.
+ if (end < out.buffered().len) end += 1;
+ return out.buffered()[0..end];
+}
+
+const Resolved = struct { code: []const u8 = "", colors: []const u8 = &.{}, source_row: usize = 0 };
+
+/// Each Callstack row's code (its call site's line, or a caller's header) and
+/// colors, by input line. A page spans thousands of files, so they are taken
+/// a file at a time: grouped by path, each file read, line-indexed and
+/// analyzed once, its rows' code and colors copied out, and the file let go
+/// before the next. Row by row, the cache compared the whole source each
+/// time and past its 64 entries parsed it again, every lookup walked the
+/// file from its top, and every source stayed in the arena to the end.
+fn resolveCallstack(p: *pardes.Pardes, arena: std.mem.Allocator, dir: []const u8, input: []const u8) ![]Resolved {
+ const mark = @import("lsp/lsp.zig").callstack_match;
+ var all: std.ArrayList([]const u8) = .empty;
+ var by_path: std.StringArrayHashMapUnmanaged(std.ArrayList(u32)) = .empty;
+ var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, input, "\n"), '\n');
+ while (lines.next()) |raw| {
+ const line = if (std.mem.startsWith(u8, raw, mark)) raw[mark.len..] else raw;
+ const index: u32 = @intCast(all.items.len);
+ try all.append(arena, line);
+ const target = parseRaw(line[treePrefix(line).end..]);
+ if (target.at.line == 0) continue;
+ const entry = try by_path.getOrPut(arena, target.path);
+ if (!entry.found_existing) entry.value_ptr.* = .empty;
+ try entry.value_ptr.append(arena, index);
+ }
+ const resolved = try arena.alloc(Resolved, all.items.len);
+ @memset(resolved, .{});
+ var file_arena: std.heap.ArenaAllocator = .init(p.gpa);
+ defer file_arena.deinit();
+ for (by_path.keys(), by_path.values()) |path, indexes| {
+ _ = file_arena.reset(.retain_capacity);
+ const fa = file_arena.allocator();
+ const source = sourceText(p, fa, dir, path);
+ var cached: Cache.Hit = .{ .analysis = .{} };
+ defer cached.deinit(p.tree_sitter_gpa);
+ // Where each line starts, the file's length closing the last.
+ var starts: std.ArrayList(usize) = .empty;
+ if (source) |text| {
+ cached = p.locations_cache.get(p.tree_sitter_gpa, text.path, text.bytes, true, true) catch .{ .analysis = .{} };
+ try starts.append(fa, 0);
+ var at: usize = 0;
+ while (std.mem.indexOfScalarPos(u8, text.bytes, at, '\n')) |nl| : (at = nl + 1) try starts.append(fa, nl + 1);
+ }
+ for (indexes.items) |index| {
+ const line = all.items[index];
+ const prefix = treePrefix(line);
+ const target = parseRaw(line[prefix.end..]);
+ var code = line[prefix.end + target.code_start ..];
+ var colors: []const u8 = &.{};
+ var source_row = target.at.line - 1;
+ if (source) |text| {
+ const row = target.at.line - 1;
+ if (prefix.branch) {
+ // Choose the innermost header containing the server's name range.
+ var chosen: ?syntax.ContextDeclaration = null;
+ for (cached.analysis.declarations) |declaration| {
+ if (declaration.start_line <= row and row <= declaration.header_end_line) {
+ if (chosen == null or declaration.end_byte - declaration.start_byte < chosen.?.end_byte - chosen.?.start_byte)
+ chosen = declaration;
+ }
+ }
+ if (chosen) |header| {
+ code = text.bytes[header.start_byte..header.header_end_byte];
+ source_row = header.start_line;
+ } else {
+ const fallback = indexedLine(text.bytes, starts.items, row);
+ if (fallback.len > 0) code = fallback;
+ }
+ } else if (code.len == 0) {
+ // A call site the language worker sent bare (it reads no
+ // sources for Callstack): its line, from the source here.
+ code = indexedLine(text.bytes, starts.items, row);
+ }
+ const offset = if (source_row < starts.items.len) starts.items[source_row] else text.bytes.len;
+ if (offset + code.len <= cached.analysis.colors.len and
+ std.mem.eql(u8, code, text.bytes[offset..][0..code.len]))
+ colors = cached.analysis.colors[offset..][0..code.len];
+ code = try arena.dupe(u8, code);
+ colors = try arena.dupe(u8, colors);
+ }
+ resolved[index] = .{ .code = code, .colors = colors, .source_row = source_row };
+ }
+ }
+ return resolved;
+}
+
+/// modal.lineSlice through a line index (`starts`, as resolveCallstack builds it).
+fn indexedLine(text: []const u8, starts: []const usize, row: usize) []const u8 {
+ if (row >= starts.len or starts[row] >= text.len) return "";
+ const end = if (row + 1 < starts.len) starts[row + 1] - 1 else text.len;
+ return text[starts[row]..end];
+}
+
+/// Enrich caller nodes on the UI thread, using the same source/declaration cache
+/// as LocationsContext. The LSP worker only supplies relationships and addresses.
+pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !Result {
+ const arena = p.scratch.allocator();
+ var out: std.Io.Writer.Allocating = .init(p.gpa);
+ errdefer out.deinit();
+ var rows: std.ArrayList(Row) = .empty;
+ errdefer freeRowList(p.gpa, &rows);
+ const stacked = p.settings.callstack_layout == .stacked;
+ const resolved = try resolveCallstack(p, arena, dir, input);
+ var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, input, "\n"), '\n');
+ var index: usize = 0;
+ while (lines.next()) |raw| : (index += 1) {
+ const mark = @import("lsp/lsp.zig").callstack_match;
+ const focus = std.mem.startsWith(u8, raw, mark);
+ const line = if (focus) raw[mark.len..] else raw;
+ const prefix = treePrefix(line);
+ const target = parseRaw(line[prefix.end..]);
+ if (target.at.line == 0) {
+ try out.writer.print("{s}\n", .{line});
+ try appendMetadata(p.gpa, &rows, .{ .path = "", .at = .{}, .label = "", .code = "", .original = null }, .context, 0, line.len, false);
+ continue;
+ }
+ const code = resolved[index].code;
+ const colors = resolved[index].colors;
+ var source_row = resolved[index].source_row;
+ const recursion = prefix.branch and std.mem.endsWith(u8, line, " ↻ recursion");
+ const first_line = code[0 .. std.mem.indexOfScalar(u8, code, '\n') orelse code.len];
+ const source_indent = if (stacked) first_line.len - std.mem.trimStart(u8, first_line, " \t").len else 0;
+ if (stacked) {
+ const end = prefix.end + target.end;
+ try out.writer.print("{s}\n", .{line[0..end]});
+ try appendMetadata(p.gpa, &rows, .{ .path = target.path, .at = target.at, .label = "", .code = "", .original = null, .mark_occurrence = false }, .match, end, end, false);
+ }
+ var header_lines = std.mem.splitScalar(u8, code, '\n');
+ var first = true;
+ var color_offset: usize = 0;
+ while (header_lines.next()) |code_line| {
+ const start = out.written().len;
+ if (stacked) {
+ var guide_buf: [1024]u8 = undefined;
+ const guide = treeContinuation(line, &guide_buf, std.math.maxInt(usize));
+ try out.writer.writeAll(guide);
+ const width = pardes.panes.File.displayWidth(line[0..prefix.end]);
+ try out.writer.splatByteAll(' ', width -| pardes.panes.File.displayWidth(guide));
+ } else if (first) {
+ try out.writer.writeAll(line[0 .. prefix.end + target.end]);
+ try out.writer.writeByte(' ');
+ } else {
+ const branch_start = prefix.end - "└──┬ ".len;
+ try out.writer.writeAll(line[0..branch_start]);
+ try out.writer.writeAll(if (std.mem.startsWith(u8, line[branch_start..], "├")) "│ " else " ");
+ try out.writer.writeAll(if (std.mem.endsWith(u8, line[0..prefix.end], "┬ ") or std.mem.endsWith(u8, line[0..prefix.end], "╮ ")) "│ " else " ");
+ try out.writer.splatByteAll(' ', pardes.panes.File.displayWidth(line[prefix.end..][0..target.end]) + 1);
+ }
+ const code_start = out.written().len - start;
+ const trim = @min(source_indent, code_line.len - std.mem.trimStart(u8, code_line, " \t").len);
+ try out.writer.writeAll(code_line[trim..]);
+ const recursion_note = recursion and first and !std.mem.endsWith(u8, code_line, " ↻ recursion");
+ if (recursion_note) try out.writer.writeAll(" ↻ recursion");
+ try out.writer.writeByte('\n');
+ try appendMetadata(p.gpa, &rows, .{
+ .path = target.path,
+ .at = if (first) target.at else .{ .line = source_row + 1 },
+ .label = "",
+ .code = code_line[trim..],
+ .source_column = trim,
+ .original = null,
+ .mark_occurrence = focus and !prefix.branch,
+ .colors = if (colors.len >= color_offset + code_line.len) colors[color_offset + trim ..][0 .. code_line.len - trim] else &.{},
+ }, if (first) (if (stacked) .preview else .match) else .context, if (first and !stacked) prefix.end + target.end else 0, code_start, true);
+ // Recursion notes are prose, outside the source snapshot.
+ if (recursion_note) {
+ const added = " ↻ recursion".len;
+ const row = &rows.items[rows.items.len - 1];
+ if (row.colors.len > 0) {
+ const expanded = try p.gpa.alloc(u8, row.colors.len + added);
+ @memcpy(expanded[0..row.colors.len], row.colors);
+ @memset(expanded[row.colors.len..], 0);
+ p.gpa.free(row.colors);
+ row.colors = expanded;
+ }
+ }
+ color_offset += code_line.len + 1;
+ source_row += 1;
+ first = false;
+ }
+ }
+ const owned_rows = try rows.toOwnedSlice(p.gpa);
+ errdefer freeRows(p.gpa, owned_rows);
+ return .{ .content = try out.toOwnedSlice(), .rows = owned_rows, .anchor = null };
+}
+
/// Inputs are sorted results. Expand each source group, merge context with
/// matches, then align groups of eight matches. All temporary source data
/// stays in the caller's scratch arena; returned text and rows are owned.
@@ -206,7 +444,7 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us
// Muted declaration headers do not need a source-color pass.
// Exact bytes include unsaved edits and virtual/remote sources.
var cached = p.locations_cache.get(p.tree_sitter_gpa, text.path, source, p.locations_config.tscontext, p.locations_config.context > 0) catch
- @import("locations_cache.zig").Result{ .analysis = syntax.analyzeSource(arena, text.path, source, p.locations_config.tscontext, p.locations_config.context > 0) catch .{} };
+ Cache.Hit{ .analysis = syntax.analyzeSource(arena, text.path, source, p.locations_config.tscontext, p.locations_config.context > 0) catch .{} };
defer cached.deinit(p.tree_sitter_gpa);
const analysis = cached.analysis;
const source_colors = analysis.colors;
@@ -261,13 +499,7 @@ pub fn format(p: *pardes.Pardes, dir: []const u8, input: []const u8, anchor: ?us
var out: std.Io.Writer.Allocating = .init(p.gpa);
errdefer out.deinit();
var rows: std.ArrayList(Row) = .empty;
- errdefer {
- for (rows.items) |row| {
- p.gpa.free(row.path);
- p.gpa.free(row.colors);
- }
- rows.deinit(p.gpa);
- }
+ errdefer freeRowList(p.gpa, &rows);
var mapped = anchor;
if (p.locations_config.layout == .stacked) {
for (pending.items, 0..) |row, index| {
@@ -333,7 +565,7 @@ fn appendMetadata(gpa: std.mem.Allocator, rows: *std.ArrayList(Row), pending: Pe
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, .path = path, .at = pending.at, .location_end = location_end, .code_start = code_start, .colors = 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 });
}
const alignment_entries = 8;
@@ -393,3 +625,328 @@ test "locations native results retain the first source tab" {
try std.testing.expectEqualStrings("some directory/a.zig", row.path);
try std.testing.expectEqualStrings("\t value", text[row.code_start..]);
}
+
+// ---- the Locations setting ----
+
+pub const Config = struct {
+ pub const Layout = enum { @"inline", stacked };
+ context: u16 = 0,
+ tscontext: bool = false,
+ tslocations: bool = true,
+ layout: Layout = .stacked,
+
+ /// Apply only supplied fields. A malformed token rejects the whole update.
+ /// Repeating a field uses its last supplied value.
+ pub fn parse(current: Config, text: []const u8) !Config {
+ var result = current;
+ var tokens = std.mem.tokenizeAny(u8, text, " \t\r\n");
+ while (tokens.next()) |token| {
+ const colon = std.mem.indexOfScalar(u8, token, ':') orelse return error.ExpectedKeyValue;
+ const key = token[0..colon];
+ const value = token[colon + 1 ..];
+ var found = false;
+ const info = @typeInfo(Config).@"struct";
+ inline for (info.field_names, info.field_types) |name, T| {
+ if (std.mem.eql(u8, key, name)) {
+ @field(result, name) = switch (@typeInfo(T)) {
+ .bool => if (std.mem.eql(u8, value, "on")) true else if (std.mem.eql(u8, value, "off")) false else return error.ExpectedOnOrOff,
+ .int => std.fmt.parseInt(T, value, 10) catch return error.InvalidContext,
+ .@"enum" => std.meta.stringToEnum(T, value) orelse return error.InvalidLayout,
+ else => @compileError("unsupported location setting type"),
+ };
+ found = true;
+ }
+ }
+ if (!found) return error.UnknownLocationSetting;
+ }
+ return result;
+ }
+
+ pub fn write(config: Config, writer: *std.Io.Writer) !void {
+ const info = @typeInfo(Config).@"struct";
+ inline for (info.field_names, info.field_types, 0..) |name, T, index| {
+ if (index > 0) try writer.writeByte(' ');
+ try writer.writeAll(name ++ ":");
+ const value = @field(config, name);
+ switch (@typeInfo(T)) {
+ .bool => try writer.writeAll(if (value) "on" else "off"),
+ .int => try writer.print("{d}", .{value}),
+ .@"enum" => try writer.writeAll(@tagName(value)),
+ else => @compileError("unsupported location setting type"),
+ }
+ }
+ }
+};
+
+test "LocationsConfig reflection round trip and partial updates" {
+ var config: Config = .{};
+ config = try config.parse("context:5 tscontext:on");
+ config = try config.parse("tslocations:off");
+ try std.testing.expectEqual(Config{ .context = 5, .tscontext = true, .tslocations = false }, config);
+ var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
+ defer out.deinit();
+ try config.write(&out.writer);
+ try std.testing.expectEqualStrings("context:5 tscontext:on tslocations:off layout:stacked", out.written());
+ try std.testing.expectEqual(config, try (Config{}).parse(out.written()));
+ try std.testing.expectEqual(config, try config.parse(" \n\t"));
+ try std.testing.expectEqual(@as(u16, 2), (try config.parse("context:1 context:2")).context);
+ try std.testing.expectEqual(Config.Layout.@"inline", (try config.parse("layout:inline")).layout);
+}
+
+test "LocationsConfig rejects invalid updates atomically" {
+ const config: Config = .{ .context = 3, .tscontext = true };
+ try std.testing.expectError(error.UnknownLocationSetting, config.parse("context:5 unknown:on"));
+ try std.testing.expectError(error.ExpectedOnOrOff, config.parse("context:5 tscontext:true"));
+ try std.testing.expectError(error.ExpectedKeyValue, config.parse("context"));
+ try std.testing.expectError(error.InvalidLayout, config.parse("context:5 layout:sideways"));
+ for ([_][]const u8{ "context:", "context:-1", "context:65536", "context:five" }) |input|
+ try std.testing.expectError(error.InvalidContext, config.parse(input));
+ try std.testing.expectEqual(@as(u16, 3), config.context);
+}
+
+// ---- the analysis cache ----
+
+/// Owned source snapshots keep analysis valid across filesystem changes and
+/// borrowed editor buffers. Entries are ordered from least to most recent.
+pub const Cache = struct {
+ /// A cache hit borrows its analysis; an entry too large to retain transfers
+ /// ownership instead. Call deinit after consuming either result.
+ pub const Hit = struct {
+ analysis: syntax.SourceAnalysis,
+ owned: bool = false,
+
+ pub fn deinit(result: *Hit, gpa: std.mem.Allocator) void {
+ if (result.owned) result.analysis.deinit(gpa);
+ result.* = undefined;
+ }
+ };
+
+ pub const max_bytes = 64 * 1024 * 1024;
+ pub const max_entries = 64;
+
+ const Entry = struct {
+ path: []u8,
+ source: []u8,
+ analysis: syntax.SourceAnalysis,
+ declarations_ready: bool,
+ colors_ready: bool,
+
+ fn size(entry: Entry) usize {
+ return entry.path.len + entry.source.len + entry.analysis.colors.len +
+ entry.analysis.declarations.len * @sizeOf(syntax.ContextDeclaration);
+ }
+
+ fn deinit(entry: *Entry, gpa: std.mem.Allocator) void {
+ gpa.free(entry.path);
+ gpa.free(entry.source);
+ entry.analysis.deinit(gpa);
+ }
+ };
+
+ entries: [max_entries]Entry = undefined,
+ len: usize = 0,
+ bytes: usize = 0,
+ analyses: usize = 0,
+ hits: usize = 0,
+
+ pub fn deinit(cache: *Cache, gpa: std.mem.Allocator) void {
+ for (cache.entries[0..cache.len]) |*entry| entry.deinit(gpa);
+ cache.* = .{};
+ }
+
+ /// Borrowed slices remain valid until the next get or cache deinit; an
+ /// owned result remains valid until Hit.deinit. All calls for this
+ /// cache must use the same allocator. Unsupported languages
+ /// are cached too, including their empty analysis arrays.
+ pub fn get(cache: *Cache, gpa: std.mem.Allocator, path: []const u8, source: []const u8, want_declarations: bool, want_colors: bool) !Hit {
+ return cache.getBounded(gpa, path, source, want_declarations, want_colors, max_bytes);
+ }
+
+ fn getBounded(cache: *Cache, gpa: std.mem.Allocator, path: []const u8, source: []const u8, want_declarations: bool, want_colors: bool, budget: usize) !Hit {
+ if (source.len > budget or path.len > budget - source.len) return error.SourceTooLarge;
+ var previous: ?usize = null;
+ var declarations = want_declarations;
+ var colors = want_colors;
+ for (cache.entries[0..cache.len], 0..) |entry, index| {
+ if (!std.mem.eql(u8, entry.path, path)) continue;
+ previous = index;
+ if (!std.mem.eql(u8, entry.source, source)) break;
+ if ((!want_declarations or entry.declarations_ready) and (!want_colors or entry.colors_ready)) {
+ // Moving the small ownership record keeps the underlying
+ // allocations intact and avoids timestamp/overflow state.
+ std.mem.copyForwards(Entry, cache.entries[index .. cache.len - 1], cache.entries[index + 1 .. cache.len]);
+ cache.entries[cache.len - 1] = entry;
+ cache.hits +|= 1;
+ return .{ .analysis = entry.analysis };
+ }
+ declarations = declarations or entry.declarations_ready;
+ colors = colors or entry.colors_ready;
+ break;
+ }
+ // A supported full-source color map needs one byte per source byte.
+ // Reject that known minimum before parsing, so the caller's uncached
+ // path does not repeat an expensive analysis for oversized inputs.
+ const input_bytes = path.len + source.len;
+ if (colors and syntax.supportsPath(path) and source.len > budget - input_bytes) return error.SourceTooLarge;
+ var analysis = try syntax.analyzeSource(gpa, path, source, declarations, colors);
+ errdefer analysis.deinit(gpa);
+ cache.analyses +|= 1;
+ const analysis_bytes = analysis.colors.len + analysis.declarations.len * @sizeOf(syntax.ContextDeclaration);
+ if (analysis_bytes > budget - input_bytes) return .{ .analysis = analysis, .owned = true };
+ 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 = analysis,
+ .declarations_ready = declarations,
+ .colors_ready = colors,
+ };
+ // Commit only after every allocation succeeds. A failed refresh leaves
+ // the previous cached snapshot usable by the next request.
+ if (previous) |index| cache.remove(gpa, index);
+ while (cache.len == max_entries or cache.bytes > budget - next.size()) cache.remove(gpa, 0);
+ cache.entries[cache.len] = next;
+ cache.len += 1;
+ cache.bytes += next.size();
+ 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);
+ std.mem.copyForwards(Entry, cache.entries[index .. cache.len - 1], cache.entries[index + 1 .. cache.len]);
+ cache.len -= 1;
+ }
+};
+
+test "locations cache validates exact source and path and upgrades analysis" {
+ const gpa = std.testing.allocator;
+ syntax.start(gpa);
+ defer syntax.stop();
+ var cache: Cache = .{};
+ defer cache.deinit(gpa);
+ const source = "const Thing = struct {\n value: u32,\n};\n";
+ _ = try cache.get(gpa, "a.zig", source, true, false);
+ try std.testing.expectEqual(@as(usize, 1), cache.analyses);
+ _ = try cache.get(gpa, "a.zig", source, true, false);
+ try std.testing.expectEqual(@as(usize, 1), cache.analyses);
+ try std.testing.expectEqual(@as(usize, 1), cache.hits);
+ _ = try cache.get(gpa, "a.zig", source, false, true);
+ try std.testing.expectEqual(@as(usize, 2), cache.analyses);
+ try std.testing.expect(cache.entries[0].declarations_ready and cache.entries[0].colors_ready);
+ _ = try cache.get(gpa, "a.zig", source, true, true);
+ try std.testing.expectEqual(@as(usize, 2), cache.analyses);
+ _ = try cache.get(gpa, "a.zig", "const Thing = struct {\n other: u32,\n};\n", true, true);
+ try std.testing.expectEqual(@as(usize, 3), cache.analyses);
+ try std.testing.expectEqual(@as(usize, 1), cache.len);
+ _ = try cache.get(gpa, "a.txt", source, true, true);
+ try std.testing.expectEqual(@as(usize, 4), cache.analyses);
+ try std.testing.expectEqual(@as(usize, 2), cache.len);
+}
+
+test "locations cache bounds memory and entries and evicts least recently used" {
+ const gpa = std.testing.allocator;
+ var cache: Cache = .{};
+ defer cache.deinit(gpa);
+ for (0..Cache.max_entries) |index| {
+ var path: [32]u8 = undefined;
+ _ = try cache.get(gpa, try std.fmt.bufPrint(&path, "{d}.unknown", .{index}), "source", false, false);
+ }
+ _ = try cache.get(gpa, "0.unknown", "source", false, false);
+ _ = try cache.get(gpa, "new.unknown", "source", false, false);
+ try std.testing.expectEqual(Cache.max_entries, cache.len);
+ try std.testing.expectEqualStrings("2.unknown", cache.entries[0].path);
+ const count = cache.analyses;
+ _ = try cache.get(gpa, "0.unknown", "source", false, false);
+ try std.testing.expectEqual(count, cache.analyses);
+ cache.deinit(gpa);
+ _ = try cache.getBounded(gpa, "a.unknown", "source", false, false, 40);
+ _ = try cache.getBounded(gpa, "b.unknown", "source", false, false, 40);
+ _ = try cache.getBounded(gpa, "c.unknown", "source", false, false, 40);
+ try std.testing.expectEqual(@as(usize, 2), cache.len);
+ try std.testing.expect(cache.bytes <= 40);
+ try std.testing.expectEqualStrings("b.unknown", cache.entries[0].path);
+ try std.testing.expectError(error.SourceTooLarge, cache.getBounded(gpa, "large.unknown", &@as([41]u8, @splat('x')), false, false, 40));
+ try std.testing.expectEqual(@as(usize, 2), cache.len);
+ if (syntax.supportsPath("large.zig")) {
+ const before = cache.analyses;
+ try std.testing.expectError(error.SourceTooLarge, cache.getBounded(gpa, "large.zig", &@as([25]u8, @splat('x')), true, true, 40));
+ try std.testing.expectEqual(before, cache.analyses);
+ }
+ cache.deinit(gpa);
+ try std.testing.expectEqual(@as(usize, 0), cache.bytes);
+ try std.testing.expectEqual(@as(usize, 0), cache.len);
+}
+
+test "locations cache keeps its previous snapshot after allocation failure" {
+ var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{});
+ const gpa = failing.allocator();
+ var cache: Cache = .{};
+ defer cache.deinit(gpa);
+ _ = try cache.get(gpa, "a.unknown", "old bytes", false, false);
+ const retained = cache.bytes;
+ failing.fail_index = failing.alloc_index;
+ try std.testing.expectError(error.OutOfMemory, cache.get(gpa, "a.unknown", "new bytes", false, false));
+ try std.testing.expectEqual(@as(usize, 1), cache.len);
+ try std.testing.expectEqual(retained, cache.bytes);
+ try std.testing.expectEqualStrings("old bytes", cache.entries[0].source);
+ // This succeeds with allocations still disabled because the old entry
+ // remains valid and the hit path moves ownership records only.
+ _ = try cache.get(gpa, "a.unknown", "old bytes", false, false);
+ try std.testing.expectEqual(@as(usize, 1), cache.hits);
+}
+
+test "locations cache transfers oversized completed analysis without reparsing" {
+ if (!syntax.supportsPath("large.zig")) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ syntax.start(gpa);
+ defer syntax.stop();
+ var cache: Cache = .{};
+ defer cache.deinit(gpa);
+ const source = "const Thing = struct {\n value: u32,\n};\n";
+ const budget = "large.zig".len + source.len;
+ var result = try cache.getBounded(gpa, "large.zig", source, true, false, budget);
+ defer result.deinit(gpa);
+ try std.testing.expect(result.owned);
+ try std.testing.expect(result.analysis.declarations.len > 0);
+ try std.testing.expectEqual(@as(usize, 1), cache.analyses);
+ try std.testing.expectEqual(@as(usize, 0), cache.len);
+ try std.testing.expectEqual(@as(usize, 0), cache.bytes);
+}