summaryrefslogtreecommitdiff
path: root/src/lsp
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 10:48:36 -0300
committerGabriel Schneider <[email protected]>2026-11-05 10:57:08 -0300
commitfc02325789b5a6622f9e577c9a45e65806c516b6 (patch)
tree65fdeed6959aef6afe0db5d18df3649aef0d2a34 /src/lsp
parent856bf4a8ed36cc6ec38dc9001f753a8838106da2 (diff)
downloadpardes-fc02325789b5a6622f9e577c9a45e65806c516b6.tar.gz
pardes-fc02325789b5a6622f9e577c9a45e65806c516b6.zip
Callstack a plain tree with a depth: CallstackLayout and CallstackCorners go with the inline layout and the square corners (each snippet below its location, the corners rounded, as both defaulted to), and the 100,000-row paging goes: a tree bigger than one answer holds stops there, its last row saying so as at the memory limit, and Callstack run in +Callstack asks nothing (only a file's pane asks a language server) where it showed the next page (lsp.callstack_more, the core's callstack_more, callstackMore and lspRequestAt, the walk's start and cut paths); CallstackDepth stays, as the reference documents it and the user's init sets it; treePrefix reads only the glyphs the tree draws, and the tests that fed it others take the drawn ones; callstack-inline.snap goes with its layout and callstack.snap snaps its style in its stead (its golden gains that section, the text byte-identical); the reference, the guide, the changelog's entry and both builtins lists say so
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/lsp')
-rw-r--r--src/lsp/lsp.zig6
-rw-r--r--src/lsp/lsp_client.zig81
2 files changed, 19 insertions, 68 deletions
diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig
index 2781077a..190c05f8 100644
--- a/src/lsp/lsp.zig
+++ b/src/lsp/lsp.zig
@@ -26,17 +26,11 @@ pub const Kind = enum {
};
pub const callstack_match = "@callstack-match ";
-/// The last row of a Callstack page that stopped full: `<path> <callers>`,
-/// the entry indices from the top the next page starts at (`3.0.17`), and
-/// how many callers this page holds.
-pub const callstack_more = "@callstack-more ";
-pub const CallstackCorners = enum { rounded, square };
pub const callstack_default_depth: u8 = 8;
pub const callstack_max_depth: u8 = 64;
pub const Req = struct {
callstack_depth: u8 = callstack_default_depth,
- callstack_corners: CallstackCorners = .rounded,
kind: Kind,
path: []const u8,
source: [:0]const u8,
diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig
index f2c186a8..40c287b0 100644
--- a/src/lsp/lsp_client.zig
+++ b/src/lsp/lsp_client.zig
@@ -545,7 +545,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
};
const pos = posOf(req.source, req.offset, c.caps.enc);
- var cx: Cx = .{ .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = req.kind == .callstack, .callstack_corners = req.callstack_corners };
+ var cx: Cx = .{ .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = req.kind == .callstack };
for (req.others) |doc| try cx.files.append(arena, .{ .path = doc.path, .text = doc.source });
tr.note("server {s} (pid {d}), root {s}, {s} columns", .{
@tagName(c.state), c.pid, c.root, @tagName(c.caps.enc),
@@ -659,7 +659,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
n += 1;
}
},
- .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget, req.arg),
+ .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget),
.incoming_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .incoming, tr),
.outgoing_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr),
.supertypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .supers, tr),
@@ -914,17 +914,8 @@ const CallTree = struct {
narrated: bool = false,
siblings: [lsp.callstack_max_depth]bool = @splat(false),
rendered: usize = 0,
- /// Paging. A page starts at `start`, a path of entry indices from the
- /// top (empty: the start), skipping what is before it at each level;
- /// `rows` and `bytes` count what rendering takes, and the walk stops
- /// before their caps at `cut`, which the next page starts at (`more`).
- /// The ancestors on the path are shown again on it, as context.
- start: [lsp.callstack_max_depth]u32 = undefined,
- start_len: usize = 0,
- at: [lsp.callstack_max_depth]u32 = undefined,
- cut: [lsp.callstack_max_depth]u32 = undefined,
- cut_len: usize = 0,
- more: bool = false,
+ /// The rows rendering the tree takes: the walk stops before `row_cap`,
+ /// as at the memory limit (`out_of_memory`).
rows: usize = 0,
row_cap: usize = max_rows - 16,
/// The bytes those rows take, over-estimated (the uri for the path it
@@ -1013,7 +1004,7 @@ fn validCaller(entry: std.json.Value) bool {
rangeOf(get(from, "selectionRange") orelse get(from, "range")) != null;
}
-fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator, arg: []const u8) Err!void {
+fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator) Err!void {
if (!c.caps.call_hier) return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name});
const b = try atPos(arena, uri, pos);
const prepared = (try call(c, io, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return;
@@ -1035,25 +1026,15 @@ fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx,
.si = si,
.started_ms = nowMs(),
.budget = budget,
- // The page ends while the answer's writer has room for the rest.
+ // The walk stops while the answer's writer has room for the rest.
.byte_cap = if (cx.fixed_out) (cx.out.buffer.len - cx.out.end) -| (1 << 20) else std.math.maxInt(usize),
};
- var steps = std.mem.tokenizeScalar(u8, std.mem.trim(u8, arg, " \n"), '.');
- while (steps.next()) |step| {
- if (tree.start_len == tree.start.len) break;
- tree.start[tree.start_len] = std.fmt.parseInt(u32, step, 10) catch break;
- tree.start_len += 1;
- }
const incoming = try hierarchyResult(c, io, parse, item, "callHierarchy/incomingCalls", deadline);
- const count = try collectCallers(c, io, arena, if (budget != null) &parse_fba else null, item, incoming, &tree, 0, tree.start_len > 0);
+ const count = try collectCallers(c, io, arena, if (budget != null) &parse_fba else null, item, incoming, &tree, 0);
if (tree.narrated) post(si, .always, "Callstack: {d} callers", .{tree.collected});
- if (count == 0 and tree.start_len == 0) try cx.out.writeAll("No callers reported\n");
+ if (count == 0) try cx.out.writeAll("No callers reported\n");
try renderCallers(cx, c.caps.enc, &tree, tree.roots.items, 0);
- if (tree.more) {
- try cx.out.writeAll(lsp.callstack_more);
- for (tree.cut[0..tree.cut_len], 0..) |index, level| try cx.out.print("{s}{d}", .{ if (level == 0) "" else ".", index });
- try cx.out.print(" {d}\n", .{tree.collected});
- } else if (tree.out_of_time) {
+ if (tree.out_of_time) {
try cx.out.writeAll("… the language server ran out of time; continue from a function above with Callstack\n");
} else if (tree.out_of_memory) {
try cx.out.writeAll("… stopped at the query's memory limit; continue from a function above with Callstack\n");
@@ -1064,28 +1045,16 @@ fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx,
/// No caller count and no overall deadline: the walk narrates itself on the
/// message row after a second, and it stops when a request goes unanswered
-/// (what was collected is shown), when the page is full (the rest is the
-/// next page), or when the shell cancels it for a newer query.
-fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.heap.FixedBufferAllocator, focus: std.json.Value, incoming: ?std.json.Value, tree: *CallTree, depth: usize, on_path: bool) Err!usize {
+/// or the answer is full (what was collected is shown, its last row saying
+/// so), or when the shell cancels it for a newer query.
+fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.heap.FixedBufferAllocator, focus: std.json.Value, incoming: ?std.json.Value, tree: *CallTree, depth: usize) Err!usize {
var count: usize = 0;
- for (items(incoming), 0..) |entry, index| {
- // A later page skips what the pages before it walked.
- const resuming = on_path and depth < tree.start_len;
- if (resuming and index < tree.start[depth]) continue;
- tree.at[depth] = @intCast(index);
+ for (items(incoming)) |entry| {
if (tree.budget) |b| if (b.buffer.len - b.end_index < callstack_reserve) {
tree.out_of_memory = true;
};
- if (!tree.more and (tree.rows >= tree.row_cap or tree.bytes >= tree.byte_cap or tree.out_of_memory)) {
- // The page is full: the next goes on here. Never where this
- // one began, which would page in place: past that entry.
- tree.more = true;
- tree.cut_len = depth + 1;
- @memcpy(tree.cut[0..tree.cut_len], tree.at[0..tree.cut_len]);
- if (tree.cut_len == tree.start_len and std.mem.eql(u32, tree.cut[0..tree.cut_len], tree.start[0..tree.start_len]))
- tree.cut[depth] += 1;
- }
- if (tree.out_of_time or tree.more) break;
+ if (tree.rows >= tree.row_cap or tree.bytes >= tree.byte_cap) tree.out_of_memory = true;
+ if (tree.out_of_time or tree.out_of_memory) break;
if (!validCaller(entry)) continue;
const from = get(entry, "from").?;
const cycle = sameItem(focus, from) or for (tree.path[0..depth]) |ancestor| {
@@ -1113,15 +1082,14 @@ fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.h
break :blk null;
},
// A response bigger than the parse region: this caller
- // stops here, the page with it.
+ // stops here, the walk with it.
error.OutOfMemory => blk: {
tree.out_of_memory = true;
break :blk null;
},
else => return e,
};
- const deeper = resuming and index == tree.start[depth] and depth + 1 < tree.start_len;
- if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(c, io, arena, parse, focus, ancestors, tree, depth + 1, deeper);
+ if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(c, io, arena, parse, focus, ancestors, tree, depth + 1);
}
}
if (children == 0) try tree.addPath(arena, tree.path[0 .. depth + 1], cycle);
@@ -1162,8 +1130,7 @@ fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: u
const more = index + 1 < children.len;
try callPrefix(cx.out, tree, depth);
const has_children = node.children.items.len > 0 or node.ranges.items.len > 0;
- const rounded = cx.callstack_corners == .rounded;
- try cx.out.writeAll(if (more) "├" else if (rounded) "╰" else "└");
+ try cx.out.writeAll(if (more) "├" else "╰");
try cx.out.writeAll(if (has_children) "──┬ " else "─── ");
const name = flat(cx.arena, node.call.name);
const label = if (node.cycle) try std.fmt.allocPrint(cx.arena, "{s} ↻ recursion", .{name}) else name;
@@ -1182,7 +1149,7 @@ fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: u
}
if (site.focus) try cx.out.writeAll(lsp.callstack_match);
try callPrefix(cx.out, tree, depth + 1);
- try cx.out.writeAll(if (node.children.items.len > 0 or site_index + 1 < node.ranges.items.len) "├─ " else if (rounded) "╰─ " else "└─ ");
+ try cx.out.writeAll(if (node.children.items.len > 0 or site_index + 1 < node.ranges.items.len) "├─ " else "╰─ ");
try emitAt(cx, enc, node.call.uri, site.range, "");
}
try renderCallers(cx, enc, tree, node.children.items, depth + 1);
@@ -1280,7 +1247,6 @@ const Cx = struct {
no_text: bool = false,
rows: usize = 0,
relative_parents: bool = false,
- callstack_corners: lsp.CallstackCorners = .rounded,
files: std.ArrayList(struct { path: []const u8, text: []const u8 }) = .empty,
fn text(cx: *Cx, path: []const u8) ?[]const u8 {
@@ -2576,15 +2542,6 @@ test "caller trees reverse incoming paths and merge shared callers and sites" {
try std.testing.expect(std.mem.indexOf(u8, text, "page_left();").? < std.mem.indexOf(u8, text, "page_right();").?);
try std.testing.expect(std.mem.indexOf(u8, text, "├──┬ test.c:4:") != null);
try std.testing.expect(std.mem.indexOf(u8, text, "╰──┬ test.c:1:") != null);
- cx.callstack_corners = .square;
- tree.rendered = 0;
- const square_start = out.written().len;
- try renderCallers(&cx, .utf8, &tree, tree.roots.items, 0);
- const square = out.written()[square_start..];
- try std.testing.expect(std.mem.indexOf(u8, square, "├──┬ test.c:4:") != null);
- try std.testing.expect(std.mem.indexOf(u8, square, "└──┬ test.c:1:") != null);
- try std.testing.expect(std.mem.indexOf(u8, square, "╰") == null);
- try std.testing.expect(std.mem.indexOf(u8, square, "╮") == null);
var recursive: CallTree = .{};
try recursive.addPath(arena, &.{ entries[0], entries[0] }, true);
try std.testing.expect(!recursive.nodes.items[recursive.roots.items[0]].cycle);