summaryrefslogtreecommitdiff
path: root/src/locations.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/locations.zig')
-rw-r--r--src/locations.zig135
1 files changed, 98 insertions, 37 deletions
diff --git a/src/locations.zig b/src/locations.zig
index 2828566d..1e4a20cf 100644
--- a/src/locations.zig
+++ b/src/locations.zig
@@ -217,6 +217,98 @@ pub fn treeContinuation(line: []const u8, buffer: []u8, max_cells: usize) []cons
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 {
@@ -232,9 +324,10 @@ pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !R
rows.deinit(p.gpa);
}
const stacked = p.settings.callstack_layout == .stacked;
- var sources: std.StringHashMap(?Source) = .init(arena);
+ const resolved = try resolveCallstack(p, arena, dir, input);
var lines = std.mem.splitScalar(u8, std.mem.trimEnd(u8, input, "\n"), '\n');
- while (lines.next()) |raw| {
+ 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;
@@ -245,41 +338,9 @@ pub fn formatCallstack(p: *pardes.Pardes, dir: []const u8, input: []const u8) !R
try appendMetadata(p.gpa, &rows, .{ .path = "", .at = .{}, .label = "", .code = "", .original = null }, .context, 0, line.len, false);
continue;
}
- const entry = try sources.getOrPut(target.path);
- if (!entry.found_existing) entry.value_ptr.* = sourceText(p, arena, dir, target.path);
- const source = entry.value_ptr.*;
- var cached: Cache.Hit = .{ .analysis = .{} };
- defer cached.deinit(p.tree_sitter_gpa);
- if (source) |text| cached = p.locations_cache.get(p.tree_sitter_gpa, text.path, text.bytes, true, true) catch .{ .analysis = .{} };
- 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 = @import("modal.zig").lineSlice(text.bytes, row);
- if (fallback.len > 0) code = fallback;
- }
- }
- {
- const offset = @import("modal.zig").lineStartOffset(text.bytes, source_row);
- 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];
- }
- }
+ 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;