summaryrefslogtreecommitdiff
path: root/src/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lsp')
-rw-r--r--src/lsp/lsp.zig17
-rw-r--r--src/lsp/lsp_client.zig761
2 files changed, 542 insertions, 236 deletions
diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig
index 4a70efe5..1c1b57a2 100644
--- a/src/lsp/lsp.zig
+++ b/src/lsp/lsp.zig
@@ -26,6 +26,10 @@ 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;
@@ -46,6 +50,9 @@ pub const Req = struct {
/// For `explain`: the query it narrates, read from `arg` (`<kind>
/// <arg>`) by `explaining`; a definition when none was asked before.
explains: Kind = .definition,
+ /// The fixed region `arena` allocates from, when it is one: a walk that
+ /// can grow (Callstack) reads how much is left and stops in time.
+ budget: ?*const std.heap.FixedBufferAllocator = null,
};
pub const Doc = struct { path: []const u8, source: [:0]const u8 };
@@ -102,18 +109,18 @@ pub fn speaks(path: []const u8) bool {
return false;
}
-pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void {
+pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void {
if (req.kind == .status) {
try out.print("backend: {s}\n", .{backend_name});
- inline for (backends) |b| try b.query(gpa, arena, req, out);
+ inline for (backends) |b| try b.query(io, gpa, arena, req, out);
return;
}
inline for (backends) |b| {
if (b.speaks(req.path) and b.supports.contains(req.kind))
- return b.query(gpa, arena, req, out);
+ return b.query(io, gpa, arena, req, out);
}
if (req.kind == .explain and backends.len > 0)
- return backends[0].query(gpa, arena, req, out);
+ return backends[0].query(io, gpa, arena, req, out);
// Nothing asked: said why, where an empty answer would read as nothing
// found.
inline for (backends) |b| if (b.speaks(req.path))
@@ -217,6 +224,6 @@ test "a question no backend answers for a file says why rather than find nothing
var buf: [256]u8 = undefined;
var out: std.Io.Writer = .fixed(&buf);
// A file no server speaks for is answered, not left empty.
- try query(std.testing.allocator, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out);
+ try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out);
try std.testing.expectStringStartsWith(out.buffered(), "@none ");
}
diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig
index b396879b..e356c979 100644
--- a/src/lsp/lsp_client.zig
+++ b/src/lsp/lsp_client.zig
@@ -8,9 +8,10 @@
//! worker thread, and a language server costs tens of milliseconds to start
//! and MINUTES to index a large workspace. So a server is spawned lazily on
//! the first query that needs it, kept for the life of the editor, and each
-//! spec gets at most one — `conns[i]` guards itself with a pthread mutex
-//! because the gui shell detaches its workers and a superseded query can still
-//! be inside `run` when the next arrives.
+//! spec gets at most one — `conns[i]` guards itself with an `Io.Mutex`
+//! because a canceled query can still be inside `run` when the next arrives.
+//! Every wait is an `Io` cancelation point: a shell cancels the worker of a
+//! query it no longer wants, and the worker stops there.
//!
//! ONE READER THREAD PER SERVER, and it is not optional. The first design
//! pumped the socket only while a query waited, which works for a server that
@@ -28,7 +29,8 @@
//! under a thread still polling it.
//!
//! Nothing here may wedge the editor:
-//! - every write and every mailbox wait is bounded by a deadline,
+//! - every write is bounded by a deadline, and every mailbox wait by a
+//! deadline or, while the server narrates progress, by cancelation,
//! - a query the server does not answer in time returns no rows and sends
//! `$/cancelRequest`; the server is NOT killed for being busy (an indexing
//! server is busy for minutes and the status row says so) — but three
@@ -58,20 +60,10 @@ extern "c" fn access(path: [*:0]const u8, mode: c_int) c_int;
extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8;
extern "c" fn usleep(usec: c_uint) c_int;
extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int;
-extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]u8;
// test-only libc (the seam's own tests build a real directory tree)
extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
extern "c" fn mkdir(path: [*:0]const u8, mode: libc.mode_t) c_int;
extern "c" fn system(cmd: [*:0]const u8) c_int;
-// pthread condattr surface, absent from std.c: what makes the mailbox waits
-// tick on CLOCK_MONOTONIC (linux) or a relative timeout (darwin).
-const pthread_condattr_t = extern struct { data: [8]u8 align(@alignOf(usize)) = @splat(0) };
-extern "c" fn pthread_condattr_init(attr: *pthread_condattr_t) c_int;
-extern "c" fn pthread_condattr_setclock(attr: *pthread_condattr_t, clock: c_int) c_int;
-extern "c" fn pthread_condattr_destroy(attr: *pthread_condattr_t) c_int;
-extern "c" fn pthread_cond_init(cond: *libc.pthread_cond_t, attr: ?*const pthread_condattr_t) c_int;
-extern "c" fn pthread_cond_timedwait_relative_np(cond: *libc.pthread_cond_t, mutex: *libc.pthread_mutex_t, reltime: *const libc.timespec) c_int;
-const CLOCK_MONOTONIC: c_int = 1; // linux ABI; the setclock call is linux-only
const X_OK: c_int = 1;
const POLLIN: i16 = 0x001;
@@ -182,21 +174,35 @@ fn specFor(path: []const u8) ?usize {
return null;
}
-// Deadlines. Requests are bounded because tty.zig cancels-and-joins the
-// previous worker on a new keypress, so the worst UI stall a wedged wait can
-// cause is one req_ms. Timeouts do NOT kill the server — an indexing
-// rust-analyzer legitimately sits on a `gd` for longer than anyone will wait,
-// and the message row is already narrating why.
+// Deadlines. A shell cancels-and-joins the previous worker on a new query,
+// and its waits are cancelation points: so a request needs no deadline to
+// keep the editor responsive, and waits for its answer. Past req_ms the
+// message row says the server is still at it; only a server silent and idle
+// (no $/progress) for wedge_ms has failed to answer. Timeouts do NOT kill the
+// server — an indexing rust-analyzer legitimately sits on a `gd` for minutes.
+// The handshake runs protected from cancelation, so it keeps its deadline.
const init_ms = 8_000; // spawn + initialize handshake
-const req_ms = 4_000; // one request/response round trip
+const req_ms = 4_000; // a request past this is said to be still working
+const wedge_ms = 300_000; // silent and idle this long: not answering (rust-analyzer takes ~1 min on `default`'s callers in agave)
const diag_ms = 1_200; // wait for a publishDiagnostics push after didChange
const reply_ms = 2_000; // our answers to server-to-client requests
+const reply_queue_bytes = 64 << 10; // replies waiting for the socket (Conn.replies)
+const parse_kept = 1 << 20; // what the reader's parse arena keeps between messages
+const max_diag_files = 256; // files a server's pushed diagnostics are kept for
+const max_diag_bytes = 32 << 20; // and their bytes
const wedged_strikes = 3; // consecutive timeouts before a restart
-const max_rows = 2000;
+/// Rows one answer may hold, and its writer's bytes: carved from the query's
+/// fixed region (memory.limits.arena.lsp_query) with the rest of its memory.
+const max_rows = 100_000;
+/// What a Callstack walk leaves of its query's region for rendering the
+/// tree it found: past this it stops and says so.
+const callstack_reserve = 32 << 20;
+/// What rendering leaves of the answer's writer for its closing line.
+const row_room = 4096;
const max_doc_bytes = 8 << 20;
-const Err = error{ Dead, Timeout, Protocol, OutOfMemory, NoServer, Backoff, WriteFailed };
+const Err = error{ Dead, Timeout, Protocol, OutOfMemory, NoServer, Backoff, WriteFailed, Canceled };
/// Long-lived state outlives every query arena and cannot borrow the caller's
/// gpa (a different one shows up in the harness than in the shell), so
@@ -251,8 +257,22 @@ const State = enum(u8) {
};
const Conn = struct {
- mu: libc.pthread_mutex_t = .{},
- cond: libc.pthread_cond_t = .{},
+ mu: std.Io.Mutex = .init,
+ cond: std.Io.Condition = .init,
+ /// Frames go out whole: a query's requests and the reader's replies
+ /// share the socket, and interleaved halves are a parse error that ends
+ /// the server.
+ write_mu: std.Io.Mutex = .init,
+ /// Replies the reader made while a query's write held the socket, sent
+ /// by that writer once its own message is out (releaseWrite): the reader
+ /// never waits to write, since a server blocked on its output reads
+ /// nothing more of ours.
+ /// Two halves: the reader appends to `replies[filling]`, and the writer
+ /// flips them and sends the other, with no copy and no wait.
+ replies: [2][reply_queue_bytes]u8 = undefined,
+ replies_len: [2]usize = .{ 0, 0 },
+ filling: u1 = 0,
+ replies_mu: std.Io.Mutex = .init,
state: State = .off,
/// bumped per spawn. A reader thread that sees a different gen than its
/// own is reading a corpse and exits; a waiter that sees one stops waiting.
@@ -264,9 +284,6 @@ const Conn = struct {
/// monotonic ms when the last handshake completed — a crash shortly
/// after "ready" counts as a handshake failure for backoff purposes
ready_at_ms: i64 = 0,
- /// linux: the zeroed condvar defaults to REALTIME; re-initialized with a
- /// monotonic condattr before its first wait (under the mutex)
- cond_monotonic: bool = false,
timeouts: u8 = 0,
pid: libc.pid_t = -1,
sock: c_int = -1,
@@ -278,6 +295,8 @@ const Conn = struct {
extra_roots: std.ArrayList([]u8) = .empty,
docs: std.ArrayList(Doc) = .empty,
diags: std.ArrayList(DiagSet) = .empty,
+ /// The bytes `diags` holds, kept under max_diag_bytes.
+ diag_bytes: usize = 0,
/// the response mailbox: one request outstanding per connection
want_id: u32 = 0,
resp: ?[]u8 = null,
@@ -285,13 +304,12 @@ const Conn = struct {
progress: i32 = 0,
title: [48]u8 = @splat(0),
title_len: u8 = 0,
+ /// The server says it is still loading its workspace (rust-analyzer's
+ /// serverStatus, not quiescent): an empty answer now is not one.
+ loading: bool = false,
+ /// its index in `conns`, for what it posts
+ si: usize = 0,
- fn lock(c: *Conn) void {
- _ = libc.pthread_mutex_lock(&c.mu);
- }
- fn unlock(c: *Conn) void {
- _ = libc.pthread_mutex_unlock(&c.mu);
- }
fn alive(c: *const Conn) bool {
return c.state == .starting or c.state == .ready;
}
@@ -406,10 +424,10 @@ const Trace = struct {
// ------------------------------------------------------------------- query
-pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void {
+pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void {
_ = gpa;
const req = lsp.explaining(asked);
- if (req.kind == .status) return status(req, out);
+ if (req.kind == .status) return status(io, req, out);
var tr: Trace = .{ .on = req.kind == .explain };
const si = specFor(req.path) orelse {
@@ -421,13 +439,14 @@ pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, o
};
tr.note("file {s} -> {s} (languageId {s})", .{ std.fs.path.basename(req.path), specs[si].name, specs[si].lang });
- const scratch_buf = try arena.alloc(u8, max_rows * 512);
+ // Raw: `alloc` fills a safe build's new memory, all 51 MB of it, per query.
+ const scratch_buf = (arena.rawAlloc(max_rows * 512, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0 .. max_rows * 512];
var scratch: std.Io.Writer = .fixed(scratch_buf);
const t0 = nowUs();
var err_name: []const u8 = "";
var failure: ?anyerror = null;
- answer(arena, si, req, &scratch, &tr) catch |e| {
+ answer(io, arena, si, req, &scratch, &tr) catch |e| {
failure = e;
err_name = @errorName(e);
tr.note("ERROR: {s}", .{err_name});
@@ -443,9 +462,9 @@ pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, o
// again, not that it is not there.
if (failure) |err| if (err == error.Backoff) {
const c = &conns[si];
- c.lock();
+ c.mu.lockUncancelable(io);
const left_ms = c.retry_after_ms -| nowMs();
- c.unlock();
+ c.mu.unlock(io);
return lsp.none(out, "{s} failed recently; retry in {d}s", .{ specs[si].name, @divFloor(left_ms + 999, 1000) });
};
if (failure) |err| return err;
@@ -464,17 +483,34 @@ fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us
try out.print("\n{d} row(s) in {d}us\n", .{ rows, us });
}
-fn answer(arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void {
+fn answer(io: std.Io, arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void {
const c = &conns[si];
- c.lock();
- defer c.unlock();
- const g = try ensure(c, si, arena, req, tr);
- run(c, si, arena, req, out, tr) catch |e| {
+ try c.mu.lock(io);
+ defer c.mu.unlock(io);
+ const g = try ensure(c, io, si, arena, req, tr);
+ const before = out.end;
+ try attempt(c, io, si, arena, req, out, tr, g);
+ // A server still loading its workspace answers at once, with nothing
+ // (rust-analyzer before its index): that is no answer. Wait until it says
+ // it is loaded, the message row saying so, and ask again.
+ const answered = out.buffered()[before..];
+ if (c.loading and c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) {
+ tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name});
+ post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name});
+ while (c.loading and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500);
+ out.end = before;
+ try attempt(c, io, si, arena, req, out, tr, g);
+ }
+}
+
+/// One run of a query, and what its failure says of the server.
+fn attempt(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace, g: u32) Err!void {
+ run(c, io, si, arena, req, out, tr) catch |e| {
switch (e) {
// transport-level: this server is gone; forget it, and the next
// query respawns. Gen-checked so a respawn that happened while we
// waited is not the one we kill.
- error.Dead, error.Protocol => shutdownIf(c, g),
+ error.Dead, error.Protocol => shutdownIf(c, io, g),
error.Timeout => {
// Slow is only wedged when the server is IDLE. One with
// active `$/progress` work (rust-analyzer running cargo
@@ -487,18 +523,19 @@ fn answer(arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer
c.timeouts +|= 1;
if (c.timeouts >= wedged_strikes and c.gen == g) {
post(si, .always, "{s} not answering — restarting", .{specs[si].name});
- shutdownIf(c, g);
+ shutdownIf(c, io, g);
}
}
},
- error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed => {},
+ // Canceled: the shell asked something newer, not a wedged server.
+ error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed, error.Canceled => {},
}
return e;
};
c.timeouts = 0;
}
-fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void {
+fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void {
// `explain` narrates the last query asked (a definition, `gd`, when
// none was) — dispatch below on the effective kind so the trace
// follows the code that query really runs.
@@ -510,18 +547,18 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
var uri: std.ArrayList(u8) = .empty;
try uriOf(&uri, arena, req.path);
- try syncDoc(c, si, arena, uri.items, req.source);
+ try syncDoc(c, io, si, arena, uri.items, req.source);
// Every other open file this server answers for, before a query whose
// answer reaches them: zls renames only in the documents it has.
if (lsp.reachesOtherFiles(kind)) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path)) {
var other: std.ArrayList(u8) = .empty;
try uriOf(&other, arena, doc.path);
- try syncDoc(c, si, arena, other.items, doc.source);
+ try syncDoc(c, io, si, arena, other.items, doc.source);
tr.note("synced {s} first", .{std.fs.path.basename(doc.path)});
};
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, .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, .callstack_corners = req.callstack_corners };
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),
@@ -540,7 +577,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
var b = try atPos(arena, uri.items, pos);
if (kind == .references) try app(&b, arena, ",\"context\":{\"includeDeclaration\":true}");
tr.note("-> {s} @ {d}:{d}", .{ method, pos.line + 1, pos.ch + 1 });
- const result = (try call(c, arena, method, b.items, deadline)) orelse {
+ const result = (try call(c, io, arena, method, b.items, deadline)) orelse {
tr.note("<- empty (no result, an error response, or null)", .{});
return;
};
@@ -549,12 +586,12 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
},
.select_refs => {
const b = try atPos(arena, uri.items, pos);
- const result = (try call(c, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return;
for (items(result)) |h| try emitRange(&cx, c.caps.enc, uri.items, get(h, "range"), "");
},
.hover => {
const b = try atPos(arena, uri.items, pos);
- const result = (try call(c, arena, "textDocument/hover", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/hover", b.items, deadline)) orelse return;
const contents = get(result, "contents") orelse return;
const text = str(contents) orelse str(get(contents, "value")) orelse blk: {
for (items(contents)) |m| if (str(m) orelse str(get(m, "value"))) |t| break :blk t;
@@ -569,26 +606,26 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
try app(&b, arena, "\"textDocument\":{\"uri\":");
try jstr(&b, arena, uri.items);
try app(&b, arena, "}");
- const result = (try call(c, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return;
try walkSymbols(&cx, c.caps.enc, uri.items, result, 0);
},
.workspace_symbols => {
var b: std.ArrayList(u8) = .empty;
try app(&b, arena, "\"query\":");
try jstr(&b, arena, req.arg);
- const result = (try call(c, arena, "workspace/symbol", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "workspace/symbol", b.items, deadline)) orelse return;
for (items(result)) |sym| {
const loc = get(sym, "location") orelse continue;
try emitRange(&cx, c.caps.enc, str(get(loc, "uri")) orelse continue, get(loc, "range"), str(get(sym, "name")) orelse "");
}
},
- .diagnostics => try diagnostics(c, arena, &cx, uri.items, deadline),
- .workspace_diagnostics => try workspaceDiagnostics(c, arena, &cx, deadline),
+ .diagnostics => try diagnostics(c, io, arena, &cx, uri.items, deadline),
+ .workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline),
.rename => {
var b = try atPos(arena, uri.items, pos);
try app(&b, arena, ",\"newName\":");
try jstr(&b, arena, req.arg);
- const result = (try call(c, arena, "textDocument/rename", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/rename", b.items, deadline)) orelse return;
// Other open files that say the old name, should the server
// answer for this file alone (zls renaming at a declaration does,
// whatever it was told): applied here, they would no longer build.
@@ -603,7 +640,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
try app(&b, arena, "\"textDocument\":{\"uri\":");
try jstr(&b, arena, uri.items);
try app(&b, arena, "},\"options\":{\"tabSize\":4,\"insertSpaces\":true}");
- const result = (try call(c, arena, "textDocument/formatting", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/formatting", b.items, deadline)) orelse return;
try formatEdits(&cx, c.caps.enc, result, req.source);
},
.code_action => {
@@ -611,7 +648,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
try app(&b, arena, "\"textDocument\":{\"uri\":");
try jstr(&b, arena, uri.items);
try b.print(arena, "}},\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch });
- const result = (try call(c, arena, "textDocument/codeAction", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return;
for (items(result)) |ca| {
const title = str(get(ca, "title")) orelse continue;
try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title));
@@ -621,7 +658,7 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
.completion => {
var b = try atPos(arena, uri.items, pos);
try app(&b, arena, ",\"context\":{\"triggerKind\":1}");
- const result = (try call(c, arena, "textDocument/completion", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/completion", b.items, deadline)) orelse return;
const list = if (get(result, "items")) |it| items(it) else items(result);
const here = lsp.rel(cx.base, cx.cur_path);
var n: usize = 0;
@@ -643,11 +680,11 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io
n += 1;
}
},
- .callstack => try callstack(c, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth),
- .incoming_calls => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .incoming, tr),
- .outgoing_calls => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr),
- .supertypes => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .supers, tr),
- .subtypes => try hierarchy(c, si, arena, &cx, uri.items, pos, deadline, .subs, tr),
+ .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget, req.arg),
+ .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),
+ .subtypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .subs, tr),
.status, .explain => unreachable,
}
}
@@ -687,13 +724,13 @@ fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth:
/// Pull when the server does (LSP 3.17), the push store otherwise. The store
/// path waits briefly for a publish that postdates the didChange we just
/// sent, so `]d` right after an edit sees the new truth, not the old one.
-fn diagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void {
+fn diagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void {
if (c.caps.pull) {
var b: std.ArrayList(u8) = .empty;
try app(&b, arena, "\"textDocument\":{\"uri\":");
try jstr(&b, arena, uri);
try app(&b, arena, "}");
- const result = (try call(c, arena, "textDocument/diagnostic", b.items, deadline)) orelse return;
+ const result = (try call(c, io, arena, "textDocument/diagnostic", b.items, deadline)) orelse return;
for (items(get(result, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena);
return;
}
@@ -702,13 +739,13 @@ fn diagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, dea
stale = d.stale;
break;
};
- if (stale) waitFresh(c, uri, nowMs() + diag_ms);
+ if (stale) try waitFresh(c, io, uri, nowMs() + diag_ms);
try renderStore(c, arena, cx, uri);
}
-fn workspaceDiagnostics(c: *Conn, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void {
+fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void {
if (c.caps.pull_workspace) {
- const result = (try call(c, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return;
+ const result = (try call(c, io, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return;
for (items(get(result, "items"))) |per| {
const uri = str(get(per, "uri")) orelse continue;
for (items(get(per, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena);
@@ -741,7 +778,7 @@ fn renderStore(c: *Conn, arena: std.mem.Allocator, cx: *Cx, only_uri: ?[]const u
/// Block (mutex released) until the reader marks `uri` fresh or the deadline
/// passes. The reader broadcasts on every publishDiagnostics.
-fn waitFresh(c: *Conn, uri: []const u8, deadline: i64) void {
+fn waitFresh(c: *Conn, io: std.Io, uri: []const u8, deadline: i64) error{Canceled}!void {
const g = c.gen;
while (c.gen == g and c.alive()) {
var fresh = false;
@@ -750,7 +787,7 @@ fn waitFresh(c: *Conn, uri: []const u8, deadline: i64) void {
break;
};
if (fresh) return;
- if (!timedWait(c, deadline)) return;
+ if (!try timedWait(c, io, deadline)) return;
}
}
@@ -873,18 +910,21 @@ fn sortEdits(edits: []PutEdit) void {
/// A bounded static hierarchy. Reuse the prepared item verbatim (including
/// server data), and retain every call-site range rather than just a declaration.
-fn hierarchyResult(c: *Conn, arena: std.mem.Allocator, item: std.json.Value, method: []const u8, deadline: i64) Err!?std.json.Value {
+fn hierarchyResult(c: *Conn, io: std.Io, arena: std.mem.Allocator, item: std.json.Value, method: []const u8, deadline: i64) Err!?std.json.Value {
if (jsonDepth(item, 0) > 96) return null;
var body: std.ArrayList(u8) = .empty;
try app(&body, arena, "\"item\":");
try app(&body, arena, std.json.Stringify.valueAlloc(arena, item, .{}) catch return error.OutOfMemory);
- return call(c, arena, method, body.items, deadline);
+ return call(c, io, arena, method, body.items, deadline);
}
const CallTree = struct {
- const Site = struct { range: std.json.Value, focus: bool };
+ /// What a node keeps of the server's item: its own copies, so the
+ /// response it came in is freed once its subtree is walked.
+ const Call = struct { uri: []const u8, name: []const u8, at: Range };
+ const Site = struct { range: Range, focus: bool };
const Node = struct {
- item: std.json.Value,
+ call: Call,
ranges: std.ArrayList(Site) = .empty,
children: std.ArrayList(usize) = .empty,
cycle: bool = false,
@@ -893,10 +933,39 @@ const CallTree = struct {
roots: std.ArrayList(usize) = .empty,
path: [lsp.callstack_max_depth]std.json.Value = undefined,
depth_limit: usize = lsp.callstack_default_depth,
- visited: usize = 0,
limited: bool = false,
+ out_of_time: bool = false,
+ out_of_memory: bool = false,
+ budget: ?*const std.heap.FixedBufferAllocator = null,
+ si: usize = 0,
+ collected: usize = 0,
+ started_ms: i64 = 0,
+ 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,
+ rows: usize = 0,
+ row_cap: usize = max_rows - 16,
+ /// The bytes those rows take, over-estimated (the uri for the path it
+ /// becomes), kept under the answer writer's room.
+ bytes: usize = 0,
+ byte_cap: usize = std.math.maxInt(usize),
+
+ /// A row of a caller at `depth`: its tree columns, the uri the path is
+ /// made from, the name, and room for the address.
+ fn rowBytes(depth: usize, caller: Call) usize {
+ return 5 * (depth + 2) + caller.uri.len + caller.name.len + 40;
+ }
/// Incoming paths arrive nearest-first. Insert them outermost-first,
/// sharing common caller prefixes and retaining all their call sites.
@@ -909,11 +978,14 @@ const CallTree = struct {
const item = get(entry, "from").?;
const children = if (parent) |id| tree.nodes.items[id].children.items else tree.roots.items;
const found = for (children) |id| {
- if (sameCall(tree.nodes.items[id].item, item)) break id;
+ if (sameCall(tree.nodes.items[id].call, item)) break id;
} else null;
+ const depth = path.len - 1 - at;
const id = found orelse blk: {
const id = tree.nodes.items.len;
- try tree.nodes.append(arena, .{ .item = item });
+ try tree.nodes.append(arena, .{ .call = try callOf(arena, item) });
+ tree.rows += 1;
+ tree.bytes += rowBytes(depth, tree.nodes.items[id].call);
if (parent) |pid| {
try tree.nodes.items[pid].children.append(arena, id);
} else try tree.roots.append(arena, id);
@@ -924,23 +996,39 @@ const CallTree = struct {
for (items(get(entry, "fromRanges"))) |range| {
const parsed = rangeOf(range) orelse continue;
for (node.ranges.items) |*other| {
- if (std.meta.eql(parsed, rangeOf(other.range).?)) {
+ if (std.meta.eql(parsed, other.range)) {
other.focus = other.focus or at == 0;
break;
}
- } else try node.ranges.append(arena, .{ .range = range, .focus = at == 0 });
+ } else {
+ try node.ranges.append(arena, .{ .range = parsed, .focus = at == 0 });
+ tree.rows += 1;
+ tree.bytes += rowBytes(depth + 1, .{ .uri = node.call.uri, .name = "", .at = parsed });
+ }
}
parent = id;
}
}
};
-fn sameCall(a: std.json.Value, b: std.json.Value) bool {
- const au = str(get(a, "uri")) orelse return false;
+fn callOf(arena: std.mem.Allocator, item: std.json.Value) Err!CallTree.Call {
+ return .{
+ .uri = try arena.dupe(u8, str(get(item, "uri")).?),
+ .name = try arena.dupe(u8, str(get(item, "name")) orelse "(unnamed)"),
+ .at = rangeOf(get(item, "selectionRange") orelse get(item, "range")).?,
+ };
+}
+
+fn sameCall(a: CallTree.Call, b: std.json.Value) bool {
const bu = str(get(b, "uri")) orelse return false;
- const ar = rangeOf(get(a, "selectionRange") orelse get(a, "range")) orelse return false;
const br = rangeOf(get(b, "selectionRange") orelse get(b, "range")) orelse return false;
- return std.mem.eql(u8, au, bu) and std.meta.eql(ar, br);
+ return std.mem.eql(u8, a.uri, bu) and std.meta.eql(a.at, br);
+}
+
+fn sameItem(a: std.json.Value, b: std.json.Value) bool {
+ const au = str(get(a, "uri")) orelse return false;
+ const ar = rangeOf(get(a, "selectionRange") orelse get(a, "range")) orelse return false;
+ return sameCall(.{ .uri = au, .name = "", .at = ar }, b);
}
fn callPrefix(out: *std.Io.Writer, tree: *const CallTree, depth: usize) Err!void {
@@ -954,44 +1042,115 @@ fn validCaller(entry: std.json.Value) bool {
rangeOf(get(from, "selectionRange") orelse get(from, "range")) != null;
}
-fn callstack(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: 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, arg: []const u8) 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, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return;
+ const prepared = (try call(c, io, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return;
if (items(prepared).len == 0) return lsp.none(cx.out, "no function at this position", .{});
+ // The rows come from the tree alone: the core reads the sources.
+ cx.no_text = true;
+ // Each response is parsed into its own part of the region, freed once
+ // its subtree is walked; only the path being walked is live JSON.
+ // Raw, as query()'s scratch: `alloc` would fill all ~100 MB of it.
+ const parse_len = if (budget) |fixed| (fixed.buffer.len - fixed.end_index) / 2 else 0;
+ var parse_fba: std.heap.FixedBufferAllocator = .init(if (budget != null)
+ (arena.rawAlloc(parse_len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..parse_len]
+ else
+ &.{});
+ const parse = if (budget != null) parse_fba.allocator() else arena;
for (items(prepared)) |item| {
- var tree: CallTree = .{ .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth) };
- const incoming = try hierarchyResult(c, arena, item, "callHierarchy/incomingCalls", deadline);
- const count = try collectCallers(c, arena, item, incoming, deadline, &tree, 0);
- if (count == 0) try cx.out.writeAll("No callers reported\n");
+ var tree: CallTree = .{
+ .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth),
+ .si = si,
+ .started_ms = nowMs(),
+ .budget = budget,
+ // The page ends 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);
+ 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");
try renderCallers(cx, c.caps.enc, &tree, tree.roots.items, 0);
- if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n");
+ 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) {
+ 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");
+ } else if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n");
tr.note("callstack: {d} callers", .{tree.rendered});
}
}
-fn collectCallers(c: *Conn, arena: std.mem.Allocator, focus: std.json.Value, incoming: ?std.json.Value, deadline: i64, tree: *CallTree, depth: usize) Err!usize {
+/// 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 {
var count: usize = 0;
- for (items(incoming)) |entry| {
- if (tree.visited >= 64) {
- tree.limited = true;
- break;
+ 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);
+ 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 (!validCaller(entry)) continue;
const from = get(entry, "from").?;
- const cycle = sameCall(focus, from) or for (tree.path[0..depth]) |ancestor| {
- if (sameCall(get(ancestor, "from").?, from)) break true;
+ const cycle = sameItem(focus, from) or for (tree.path[0..depth]) |ancestor| {
+ if (sameItem(get(ancestor, "from").?, from)) break true;
} else false;
tree.path[depth] = entry;
- tree.visited += 1;
count += 1;
+ tree.collected += 1;
+ if (nowMs() - tree.started_ms >= 1000) {
+ tree.narrated = true;
+ post(tree.si, .chatty, "Callstack: {d} callers\u{2026}", .{tree.collected});
+ }
var children: usize = 0;
if (!cycle) {
- if (depth + 1 == tree.depth_limit or tree.visited >= 64) {
+ if (depth + 1 == tree.depth_limit) {
tree.limited = true;
} else {
- const ancestors = try hierarchyResult(c, arena, from, "callHierarchy/incomingCalls", deadline);
- children = try collectCallers(c, arena, focus, ancestors, deadline, tree, depth + 1);
+ const mark = if (parse) |fixed| fixed.end_index else 0;
+ defer if (parse) |fixed| {
+ fixed.end_index = mark;
+ };
+ const ancestors = hierarchyResult(c, io, if (parse) |fixed| fixed.allocator() else arena, from, "callHierarchy/incomingCalls", nowMs() + req_ms) catch |e| switch (e) {
+ error.Timeout => blk: {
+ tree.out_of_time = true;
+ break :blk null;
+ },
+ // A response bigger than the parse region: this caller
+ // stops here, the page 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 (children == 0) try tree.addPath(arena, tree.path[0 .. depth + 1], cycle);
@@ -999,51 +1158,61 @@ fn collectCallers(c: *Conn, arena: std.mem.Allocator, focus: std.json.Value, inc
return count;
}
+/// Test callers sink below the rest; each group keeps the usual order.
+// ponytail: substring heuristic, "latest.zig" or "contest" count as tests too
+fn testCaller(caller: CallTree.Call) bool {
+ return std.ascii.findIgnoreCase(caller.name, "test") != null or
+ std.ascii.findIgnoreCase(std.fs.path.basenamePosix(caller.uri), "test") != null;
+}
+
fn callerLess(tree: *const CallTree, a: usize, b: usize) bool {
- const left = tree.nodes.items[a].item;
- const right = tree.nodes.items[b].item;
- const uri_order = std.mem.order(u8, str(get(left, "uri")).?, str(get(right, "uri")).?);
+ const left = tree.nodes.items[a].call;
+ const right = tree.nodes.items[b].call;
+ if (testCaller(left) != testCaller(right)) return testCaller(right);
+ const uri_order = std.mem.order(u8, left.uri, right.uri);
if (uri_order != .eq) return uri_order == .lt;
- return callRangeLess({}, get(left, "selectionRange") orelse get(left, "range").?, get(right, "selectionRange") orelse get(right, "range").?);
+ return rangeLess(left.at, right.at);
}
-fn callRangeLess(_: void, a: std.json.Value, b: std.json.Value) bool {
- const left = rangeOf(a).?;
- const right = rangeOf(b).?;
+fn rangeLess(left: Range, right: Range) bool {
return left.sl < right.sl or (left.sl == right.sl and left.sc < right.sc);
}
fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: usize) Err!void {
std.mem.sort(usize, children, @as(*const CallTree, tree), callerLess);
for (children, 0..) |id, index| {
- if (tree.rendered >= 64) {
+ // emitRange's row ceiling and the writer's room, met here before a
+ // prefix is half written
+ if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) {
tree.limited = true;
return;
}
const node = &tree.nodes.items[id];
- const item = node.item;
- const uri = str(get(item, "uri")).?;
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 (has_children) "──┬ " else "─── ");
- const name = flat(cx.arena, str(get(item, "name")) orelse "(unnamed)");
+ const name = flat(cx.arena, node.call.name);
const label = if (node.cycle) try std.fmt.allocPrint(cx.arena, "{s} ↻ recursion", .{name}) else name;
- try emitRange(cx, enc, uri, get(item, "selectionRange") orelse get(item, "range"), label);
+ try emitAt(cx, enc, node.call.uri, node.call.at, label);
tree.rendered += 1;
tree.siblings[depth] = more;
std.mem.sort(CallTree.Site, node.ranges.items, {}, struct {
fn less(_: void, a: CallTree.Site, b: CallTree.Site) bool {
- return callRangeLess({}, a.range, b.range);
+ return rangeLess(a.range, b.range);
}
}.less);
for (node.ranges.items, 0..) |site, site_index| {
+ if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) {
+ tree.limited = true;
+ return;
+ }
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 emitRange(cx, enc, uri, site.range, "");
+ try emitAt(cx, enc, node.call.uri, site.range, "");
}
try renderCallers(cx, enc, tree, node.children.items, depth + 1);
}
@@ -1054,7 +1223,7 @@ const Hier = enum { incoming, outgoing, supers, subs };
/// The two-step hierarchy kinds: prepare at the cursor, then follow every
/// item the server returned (usually one). Gated on the server capability so
/// an old server costs zero round trips.
-fn hierarchy(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void {
+fn hierarchy(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void {
const call_side = h == .incoming or h == .outgoing;
if (call_side and !c.caps.call_hier) {
tr.note("STOP: {s} does not advertise callHierarchyProvider", .{specs[si].name});
@@ -1072,9 +1241,9 @@ fn hierarchy(c: *Conn, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []cons
.subs => "typeHierarchy/subtypes",
};
const b = try atPos(arena, uri, pos);
- const prepared = (try call(c, arena, prepare, b.items, deadline)) orelse return;
+ const prepared = (try call(c, io, arena, prepare, b.items, deadline)) orelse return;
for (items(prepared)) |item| {
- const result = (try hierarchyResult(c, arena, item, follow, deadline)) orelse return;
+ const result = (try hierarchyResult(c, io, arena, item, follow, deadline)) orelse return;
for (items(result)) |entry| switch (h) {
.incoming => {
// one row per CALL SITE, under the caller's name — that is
@@ -1134,6 +1303,10 @@ const Cx = struct {
cur_path: []const u8,
cur_src: []const u8,
out: *std.Io.Writer,
+ /// `out` is the query's fixed answer buffer, whose room is checked.
+ fixed_out: bool = false,
+ /// Rows without their source text (Callstack: the core reads it).
+ no_text: bool = false,
rows: usize = 0,
relative_parents: bool = false,
callstack_corners: lsp.CallstackCorners = .rounded,
@@ -1169,11 +1342,16 @@ const Cx = struct {
/// columns, and a single-line range becomes the `path:LINE:COL-ENDCOL` form a
/// look SELECTS. `note` overrides the source line as the row's text.
fn emitRange(cx: *Cx, enc: Enc, uri: []const u8, range: ?std.json.Value, note: []const u8) Err!void {
+ return emitAt(cx, enc, uri, rangeOf(range) orelse return, note);
+}
+
+fn emitAt(cx: *Cx, enc: Enc, uri: []const u8, r: Range, note: []const u8) Err!void {
if (cx.rows >= max_rows) return;
const path = pathOf(cx.arena, uri) orelse return;
if (path.len == 0 or path[0] != '/') return; // rows promise absolute-or-rel-from-base
- const r = rangeOf(range) orelse return;
- const src = cx.text(path);
+ // Without sources (Callstack) the asking file is still known: its rows'
+ // columns convert from utf-16 as ever.
+ const src = if (cx.no_text and !std.mem.eql(u8, path, cx.cur_path)) null else cx.text(path);
var bol: usize = 0;
var eol: usize = 0;
@@ -1323,7 +1501,8 @@ fn transportPair(sv: *[2]libc.fd_t) bool {
/// Spawn-or-return, and tell an existing server about a new project root.
/// Returns the generation the caller's whole query is bound to.
-fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trace) Err!u32 {
+fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trace) Err!u32 {
+ c.si = si;
if (c.state == .disabled) {
tr.note("STOP: {s} is disabled for this session (no binary; set {s})", .{ specs[si].name, specs[si].env });
return error.NoServer;
@@ -1341,7 +1520,7 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac
try app(&b, arena, "{\"jsonrpc\":\"2.0\",\"method\":\"workspace/didChangeWorkspaceFolders\",\"params\":{\"event\":{\"added\":[{\"name\":\"root\",\"uri\":\"");
try uriOf(&b, arena, root);
try app(&b, arena, "\"}],\"removed\":[]}}}");
- try frame(c.sock, b.items, nowMs() + reply_ms);
+ try send(c, io, b.items, nowMs() + wedge_ms);
const owned = sa.dupe(u8, root) catch return error.OutOfMemory;
c.extra_roots.append(sa, owned) catch sa.free(owned);
}
@@ -1430,12 +1609,13 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac
if (c.resp) |r| sa.free(r);
c.resp = null;
c.progress = 0;
+ c.loading = false;
c.timeouts = 0;
forgetDocs(c);
if (c.root.len > 0) sa.free(c.root);
c.root = owned_root;
- const th = std.Thread.spawn(.{}, reader, .{ si, g, sv[0] }) catch {
+ const th = std.Thread.spawn(.{}, reader, .{ io, si, g, sv[0] }) catch {
// no reader means nobody would ever close the fd our way; do it here,
// before anything else can see the conn
_ = libc.close(sv[0]);
@@ -1450,8 +1630,14 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac
post(si, .always, "{s} starting — {s}", .{ specs[si].name, root });
tr.note("spawned {s} (pid {d}) at {s}", .{ specs[si].name, pid, root });
- handshake(c, arena, root) catch |e| {
- shutdownIf(c, g);
+ // A start runs to the end however soon the query that began it is
+ // canceled: killing a half-started server would only make the next
+ // query start it again. The cancel lands at the next wait instead.
+ const protection = io.swapCancelProtection(.blocked);
+ const started = handshake(c, io, arena, root);
+ _ = io.swapCancelProtection(protection);
+ started catch |e| {
+ shutdownIf(c, io, g);
// NOT a session disable. A handshake that misses the deadline is
// routinely environmental — a rustup shim deciding to download the
// project's pinned toolchain before launching the real server was
@@ -1477,7 +1663,7 @@ fn ensure(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trac
return g;
}
-fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void {
+fn handshake(c: *Conn, io: std.Io, arena: std.mem.Allocator, root: []const u8) Err!void {
var b: std.ArrayList(u8) = .empty;
try b.print(arena, "\"processId\":{d},\"clientInfo\":{{\"name\":\"pardes\"}},\"rootUri\":\"", .{@as(u32, @bitCast(libc.getpid()))});
try uriOf(&b, arena, root);
@@ -1509,7 +1695,7 @@ fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void {
for (b.items) |ch| if (ch != '\n') try body.append(arena, ch);
const deadline = nowMs() + init_ms;
- const reply = (try call(c, arena, "initialize", body.items, deadline)) orelse return error.Protocol;
+ const reply = (try call(c, io, arena, "initialize", body.items, deadline)) orelse return error.Protocol;
const caps = get(reply, "capabilities");
c.caps.enc = if (std.mem.eql(u8, str(get(caps, "positionEncoding")) orelse "utf-16", "utf-8")) .utf8 else .utf16;
@@ -1521,7 +1707,7 @@ fn handshake(c: *Conn, arena: std.mem.Allocator, root: []const u8) Err!void {
c.caps.type_hier = provider(get(caps, "typeHierarchyProvider"));
c.caps.folders = if (get(get(get(caps, "workspace"), "workspaceFolders"), "supported")) |s| s == .bool and s.bool else false;
- try frame(c.sock, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline);
+ try send(c, io, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline);
}
/// A server capability that may be `true`, an options object, or absent.
@@ -1536,7 +1722,7 @@ fn provider(v: ?std.json.Value) bool {
/// didOpen the first time a file is seen, didChange when its bytes moved,
/// nothing when they did not — the common case between two presses of gd.
-fn syncDoc(c: *Conn, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void {
+fn syncDoc(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void {
const hash = std.hash.Wyhash.hash(0, src);
var doc: ?*Doc = null;
for (c.docs.items) |*d| if (std.mem.eql(u8, d.uri, uri)) {
@@ -1568,13 +1754,16 @@ fn syncDoc(c: *Conn, si: usize, arena: std.mem.Allocator, uri: []const u8, src:
try jstr(&b, arena, src);
try app(&b, arena, "}}}");
}
- try frame(c.sock, b.items, nowMs() + req_ms);
+ // A server busy with what it was sent last reads no more meanwhile
+ // (clangd parses its compile database at a first open): this waits as
+ // an answer does, for a cancel or a server that has stopped.
+ try send(c, io, b.items, nowMs() + wedge_ms);
}
/// Send a request and wait on the mailbox. Returns the parsed `result`, or
/// null for an error response / null result — both are legal "no answer".
/// Runs with the conn mutex held; the mutex is released inside the wait.
-fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const u8, deadline: i64) Err!?std.json.Value {
+fn call(c: *Conn, io: std.Io, arena: std.mem.Allocator, method: []const u8, params: []const u8, deadline: i64) Err!?std.json.Value {
const g = c.gen;
const id = c.next_id;
c.next_id +%= 1;
@@ -1589,17 +1778,36 @@ fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const
c.want_id = id;
defer c.want_id = 0;
- try frame(c.sock, b.items, deadline);
+ try send(c, io, b.items, if (c.state == .ready) nowMs() + wedge_ms else deadline);
+ // The handshake (still `.starting`) has its deadline. A ready server is
+ // waited for until it answers or the shell cancels this query for a
+ // newer one, so the first query after a start is answered, not lost:
+ // past `deadline` the message row says it is still working, and only
+ // silent and idle for wedge_ms is it not answering.
+ var said = false;
+ var busy_at = nowMs();
while (c.resp == null and c.gen == g and c.alive()) {
- if (!timedWait(c, deadline)) break;
+ const ready = c.state == .ready;
+ const waiting = timedWait(c, io, if (ready) nowMs() + 500 else deadline) catch |e| {
+ cancelRequest(c, io, arena, id);
+ return e;
+ };
+ if (!ready) {
+ if (waiting) continue;
+ break;
+ }
+ if (c.progress > 0) busy_at = nowMs();
+ if (!said and nowMs() >= deadline) {
+ said = true;
+ post(c.si, .always, "{s} is still working on it\u{2026}", .{specs[c.si].name});
+ }
+ if (nowMs() - busy_at >= wedge_ms) break;
}
+ if (said and c.resp != null) post(c.si, .always, "{s} answered", .{specs[c.si].name});
if (c.gen != g or !c.alive()) return error.Dead;
const raw = c.resp orelse {
- // give the server leave to abandon the work we stopped waiting for
- var cb: std.ArrayList(u8) = .empty;
- cb.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"$/cancelRequest\",\"params\":{{\"id\":{d}}}}}", .{id}) catch return error.Timeout;
- frame(c.sock, cb.items, nowMs() + reply_ms) catch {};
+ cancelRequest(c, io, arena, id);
return error.Timeout;
};
c.resp = null;
@@ -1609,49 +1817,23 @@ fn call(c: *Conn, arena: std.mem.Allocator, method: []const u8, params: []const
if (result == .null) return null;
return result;
}
-/// One bounded cond wait. False when the deadline has passed.
-///
-/// The deadline arithmetic everywhere here is CLOCK_MONOTONIC, so the wait
-/// must be too: a REALTIME abstime plus an NTP step backwards would park a
-/// worker far past `req_ms`, violating the seam's bounded-wait promise. On
-/// linux the condvar is lazily initialized with a monotonic condattr; darwin
-/// has no setclock but has a RELATIVE wait, which never consults the wall
-/// clock at all.
-fn timedWait(c: *Conn, deadline: i64) bool {
+/// Give the server leave to abandon the work we stopped waiting for.
+fn cancelRequest(c: *Conn, io: std.Io, arena: std.mem.Allocator, id: u32) void {
+ var cb: std.ArrayList(u8) = .empty;
+ cb.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"$/cancelRequest\",\"params\":{{\"id\":{d}}}}}", .{id}) catch return;
+ send(c, io, cb.items, nowMs() + reply_ms) catch {};
+}
+
+/// One bounded cond wait, and a cancelation point: a worker the shell
+/// cancels for a newer query stops here. False when the deadline has passed.
+fn timedWait(c: *Conn, io: std.Io, deadline: i64) error{Canceled}!bool {
const left = deadline - nowMs();
if (left <= 0) return false;
const wait_ms = @min(left, 500); // tick so gen/state changes are noticed
- if (comptime builtin.os.tag.isDarwin()) {
- const rel: libc.timespec = .{
- .sec = @divTrunc(wait_ms, 1000),
- .nsec = @rem(wait_ms, 1000) * 1_000_000,
- };
- _ = pthread_cond_timedwait_relative_np(&c.cond, &c.mu, &rel);
- return true;
- }
- if (!c.cond_monotonic) {
- // lazily re-initialize the zeroed (REALTIME) condvar with a
- // monotonic clock; under the mutex, and only before the first wait,
- // so no thread can be parked on the old one
- var attr: pthread_condattr_t = undefined;
- if (pthread_condattr_init(&attr) == 0) {
- _ = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
- _ = pthread_cond_init(&c.cond, &attr);
- _ = pthread_condattr_destroy(&attr);
- }
- c.cond_monotonic = true;
- }
- var now: libc.timespec = undefined;
- _ = libc.clock_gettime(.MONOTONIC, &now);
- var abs: libc.timespec = .{
- .sec = now.sec + @divTrunc(wait_ms, 1000),
- .nsec = now.nsec + @rem(wait_ms, 1000) * 1_000_000,
+ c.cond.waitTimeout(io, &c.mu, .{ .duration = .{ .raw = .fromMilliseconds(wait_ms), .clock = .awake } }) catch |e| switch (e) {
+ error.Timeout => {},
+ error.Canceled => return error.Canceled,
};
- if (abs.nsec >= 1_000_000_000) {
- abs.sec += 1;
- abs.nsec -= 1_000_000_000;
- }
- _ = libc.pthread_cond_timedwait(&c.cond, &c.mu, &abs);
return true;
}
@@ -1659,7 +1841,7 @@ fn timedWait(c: *Conn, deadline: i64) bool {
/// respawn that happened during the caller's wait must not be killed for its
/// predecessor's crimes. Mutex held. The reader closes the fd; this only
/// shuts the transport down and reaps the child.
-fn shutdownIf(c: *Conn, g: u32) void {
+fn shutdownIf(c: *Conn, io: std.Io, g: u32) void {
if (c.gen != g or c.sock < 0) return;
_ = libc.shutdown(c.sock, 2); // SHUT_RDWR: the reader sees EOF and closes
reap(c.pid);
@@ -1669,7 +1851,7 @@ fn shutdownIf(c: *Conn, g: u32) void {
forgetDocs(c);
if (c.resp) |r| sa.free(r);
c.resp = null;
- _ = libc.pthread_cond_broadcast(&c.cond);
+ c.cond.broadcast(io);
}
/// TERM, a bounded grace, then KILL, to the server's whole process group
@@ -1715,6 +1897,7 @@ fn forgetDocs(c: *Conn) void {
sa.free(d.body);
}
c.diags.clearRetainingCapacity();
+ c.diag_bytes = 0;
}
// ------------------------------------------------------------ reader thread
@@ -1722,20 +1905,22 @@ fn forgetDocs(c: *Conn) void {
/// Owns the read side of one socket for one generation, and is the only
/// closer of that fd. Routes responses to the mailbox, answers server
/// requests, feeds the diagnostics store, narrates progress.
-fn reader(si: usize, g: u32, sock: c_int) void {
+fn reader(io: std.Io, si: usize, g: u32, sock: c_int) void {
const c = &conns[si];
var buf = sa.alloc(u8, 256 * 1024) catch {
- readerExit(c, si, g, sock);
+ readerExit(c, io, si, g, sock);
return;
};
defer sa.free(buf);
var len: usize = 0;
+ var parse_arena: std.heap.ArenaAllocator = .init(sa);
+ defer parse_arena.deinit();
outer: while (true) {
// drain every complete frame already buffered
drain: while (true) switch (frameNext(buf[0..len])) {
.frame => |m| {
- dispatch(c, si, g, sock, buf[m.body_start..m.body_end]);
+ dispatch(c, io, si, g, sock, buf[m.body_start..m.body_end], &parse_arena);
std.mem.copyForwards(u8, buf[0 .. len - m.consumed], buf[m.consumed..len]);
len -= m.consumed;
},
@@ -1761,9 +1946,9 @@ fn reader(si: usize, g: u32, sock: c_int) void {
if (pr == 0) {
// tick: a respawn while the server was silent leaves this thread
// reading a corpse; notice and go
- c.lock();
+ c.mu.lockUncancelable(io);
const stale = c.gen != g;
- c.unlock();
+ c.mu.unlock(io);
if (stale) break :outer;
continue;
}
@@ -1776,11 +1961,11 @@ fn reader(si: usize, g: u32, sock: c_int) void {
if (got == 0) break :outer; // the server exited or shutdownIf spoke
len += @intCast(got);
}
- readerExit(c, si, g, sock);
+ readerExit(c, io, si, g, sock);
}
-fn readerExit(c: *Conn, si: usize, g: u32, sock: c_int) void {
- c.lock();
+fn readerExit(c: *Conn, io: std.Io, si: usize, g: u32, sock: c_int) void {
+ c.mu.lockUncancelable(io);
if (c.gen == g and c.alive()) {
// the server died on its own — shutdownIf never saw it, so the corpse
// is ours to reap and the conn's transport fields are ours to clear
@@ -1800,10 +1985,14 @@ fn readerExit(c: *Conn, si: usize, g: u32, sock: c_int) void {
} else {
post(si, .always, "{s} exited — restarts on the next query", .{specs[si].name});
}
- _ = libc.pthread_cond_broadcast(&c.cond);
+ c.cond.broadcast(io);
}
- c.unlock();
+ c.mu.unlock(io);
+ // A query writes with the mutex let go (send): the fd stays its until
+ // that write is done, and shutdown(2) has already made it fail fast.
+ c.write_mu.lockUncancelable(io);
_ = libc.close(sock);
+ c.write_mu.unlock(io);
}
/// 10s doubling to 2min: the fork-per-keystroke guard's schedule.
@@ -1845,32 +2034,33 @@ fn frameNext(data: []const u8) FrameStep {
/// One message from the server. Parses into a throwaway arena; touches conn
/// state only under the mutex; replies never hold it (bounded writes must not
/// stall the mailbox).
-fn dispatch(c: *Conn, si: usize, g: u32, sock: c_int, raw: []const u8) void {
- var arena_state: std.heap.ArenaAllocator = .init(sa);
- defer arena_state.deinit();
+fn dispatch(c: *Conn, io: std.Io, si: usize, g: u32, sock: c_int, raw: []const u8, arena_state: *std.heap.ArenaAllocator) void {
+ // The reader's one parse arena, reset per message: no mapping per
+ // `$/progress` report, and no more than parse_kept kept between them.
+ defer _ = arena_state.reset(.{ .retain_with_limit = parse_kept });
const arena = arena_state.allocator();
const v = std.json.parseFromSliceLeaky(std.json.Value, arena, raw, .{}) catch return;
if (v != .object) return;
if (str(get(v, "method"))) |method| {
- if (get(v, "id")) |id| return serverRequest(arena, sock, method, id, get(v, "params"));
- return notification(c, si, g, raw, method, get(v, "params"));
+ if (get(v, "id")) |id| return serverRequest(c, io, arena, sock, method, id, get(v, "params"));
+ return notification(c, io, si, g, raw, method, get(v, "params"));
}
// a response: deliver to the one waiting query, if it is still waiting
const id = num(get(v, "id")) orelse return;
- c.lock();
- defer c.unlock();
+ c.mu.lockUncancelable(io);
+ defer c.mu.unlock(io);
if (c.gen == g and c.want_id != 0 and id == c.want_id and c.resp == null) {
c.resp = sa.dupe(u8, raw) catch null;
- _ = libc.pthread_cond_broadcast(&c.cond);
+ c.cond.broadcast(io);
}
}
/// The server asked US something. Everything optional was declined in the
/// handshake, so a null-ish answer is always legal — but it must ARRIVE, or
/// the server blocks forever on its own question.
-fn serverRequest(arena: std.mem.Allocator, sock: c_int, method: []const u8, id: std.json.Value, params: ?std.json.Value) void {
+fn serverRequest(c: *Conn, io: std.Io, arena: std.mem.Allocator, sock: c_int, method: []const u8, id: std.json.Value, params: ?std.json.Value) void {
var b: std.ArrayList(u8) = .empty;
app(&b, arena, "{\"jsonrpc\":\"2.0\",\"id\":") catch return;
switch (id) {
@@ -1890,17 +2080,17 @@ fn serverRequest(arena: std.mem.Allocator, sock: c_int, method: []const u8, id:
} else {
app(&b, arena, ",\"result\":null}") catch return;
}
- frame(sock, b.items, nowMs() + reply_ms) catch {};
+ answerServer(c, io, sock, b.items);
}
-fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8, params: ?std.json.Value) void {
+fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method: []const u8, params: ?std.json.Value) void {
if (std.mem.eql(u8, method, "textDocument/publishDiagnostics")) {
const uri = str(get(params, "uri")) orelse return;
// keep the raw MESSAGE (params included); re-parsing it per query
// beats owning a parsed tree past this arena's life
const body = sa.dupe(u8, raw) catch return;
- c.lock();
- defer c.unlock();
+ c.mu.lockUncancelable(io);
+ defer c.mu.unlock(io);
if (c.gen != g) {
sa.free(body);
return;
@@ -1914,10 +2104,25 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
slot = d;
break;
};
+ // Bounded in files and bytes: a new set that does not fit pushes
+ // out the oldest files' (the server sends a file's again when it
+ // changes); one bigger than the whole store is not kept.
+ if (body.len > max_diag_bytes) {
+ sa.free(body);
+ return;
+ }
if (slot) |d| {
+ c.diag_bytes -= d.body.len;
sa.free(d.body);
d.body = body;
- } else if (c.diags.items.len < 256) {
+ c.diag_bytes += body.len;
+ } else {
+ while (c.diags.items.len > 0 and (c.diags.items.len >= max_diag_files or c.diag_bytes + body.len > max_diag_bytes)) {
+ const old = c.diags.orderedRemove(0);
+ c.diag_bytes -= old.body.len;
+ sa.free(old.uri);
+ sa.free(old.body);
+ }
const owned = sa.dupe(u8, uri) catch {
sa.free(body);
return;
@@ -1927,17 +2132,18 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
sa.free(body);
return;
};
- } else sa.free(body);
- _ = libc.pthread_cond_broadcast(&c.cond); // waitFresh watches this
+ c.diag_bytes += body.len;
+ }
+ c.cond.broadcast(io); // waitFresh watches this
return;
}
if (std.mem.eql(u8, method, "$/progress")) {
const value = get(params, "value") orelse return;
const pkind = str(get(value, "kind")) orelse return;
const msg = str(get(value, "message")) orelse "";
- c.lock();
+ c.mu.lockUncancelable(io);
if (c.gen != g) {
- c.unlock();
+ c.mu.unlock(io);
return;
}
if (std.mem.eql(u8, pkind, "begin")) {
@@ -1948,7 +2154,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
var tbuf: [48]u8 = undefined;
const t = tbuf[0..c.title_len];
@memcpy(t, c.title[0..c.title_len]);
- c.unlock();
+ c.mu.unlock(io);
post(si, .chatty, "{s}: {s}\u{2026} {s}", .{ specs[si].name, t, msg });
return;
}
@@ -1957,7 +2163,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
const t = tbuf[0..c.title_len];
@memcpy(t, c.title[0..c.title_len]);
const pct = num(get(value, "percentage"));
- c.unlock();
+ c.mu.unlock(io);
if (pct) |p|
post(si, .chatty, "{s}: {s} {d}% {s}", .{ specs[si].name, t, p, msg })
else
@@ -1968,7 +2174,7 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
c.progress -= 1;
const idle = c.progress <= 0;
if (idle) c.progress = 0;
- c.unlock();
+ c.mu.unlock(io);
// the setpoint every burst of progress ends on; `.always` + the
// dedupe above means it lands exactly once however the reports raced
if (idle) post(si, .always, "{s}: ready", .{specs[si].name});
@@ -1985,6 +2191,12 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
if (std.mem.eql(u8, method, "experimental/serverStatus")) {
const health = str(get(params, "health")) orelse "ok";
const quiescent = if (get(params, "quiescent")) |q| q == .bool and q.bool else false;
+ c.mu.lockUncancelable(io);
+ if (c.gen == g) {
+ c.loading = !quiescent;
+ c.cond.broadcast(io);
+ }
+ c.mu.unlock(io);
if (!std.mem.eql(u8, health, "ok"))
post(si, .always, "{s}: {s} — {s}", .{ specs[si].name, health, str(get(params, "message")) orelse "" })
else if (quiescent)
@@ -1995,12 +2207,12 @@ fn notification(c: *Conn, si: usize, g: u32, raw: []const u8, method: []const u8
// ------------------------------------------------------------- introspection
-fn status(req: lsp.Req, out: *std.Io.Writer) !void {
+fn status(io: std.Io, req: lsp.Req, out: *std.Io.Writer) !void {
_ = req;
try out.print("\nprotocol servers (lsp-client):\n", .{});
for (&specs, 0..) |*s, i| {
const c = &conns[i];
- c.lock();
+ c.mu.lockUncancelable(io);
const state = c.state;
var rootbuf: [256]u8 = undefined;
const rootlen = @min(c.root.len, rootbuf.len);
@@ -2012,7 +2224,7 @@ fn status(req: lsp.Req, out: *std.Io.Writer) !void {
const pull = c.caps.pull;
const chier = c.caps.call_hier;
const thier = c.caps.type_hier;
- c.unlock();
+ c.mu.unlock(io);
const env_v = if (getenv(s.env)) |v| std.mem.span(v) else null;
// Never spawned is not off: the first query of its files starts it.
try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) });
@@ -2069,21 +2281,80 @@ fn atPos(arena: std.mem.Allocator, uri: []const u8, pos: Pos) Err!std.ArrayList(
return b;
}
-fn frame(sock: c_int, body: []const u8, deadline: i64) Err!void {
+/// A message to the server from a query, the conn's mutex let go while it is
+/// written: the reader goes on taking the server's output meanwhile, which a
+/// server may be waiting to write before it reads more of ours.
+fn send(c: *Conn, io: std.Io, body: []const u8, deadline: i64) Err!void {
+ const sock = c.sock;
+ c.mu.unlock(io);
+ defer c.mu.lockUncancelable(io);
+ return frame(c, io, sock, body, deadline);
+}
+
+fn frame(c: *Conn, io: std.Io, sock: c_int, body: []const u8, deadline: i64) Err!void {
if (sock < 0) return error.Dead;
+ c.write_mu.lockUncancelable(io);
+ defer releaseWrite(c, io, sock);
var hdr: [64]u8 = undefined;
const h = std.fmt.bufPrint(&hdr, "Content-Length: {d}\r\n\r\n", .{body.len}) catch return error.Protocol;
- try sockWrite(sock, h, deadline);
- try sockWrite(sock, body, deadline);
+ var started = false;
+ try sockWrite(io, sock, h, deadline, &started);
+ try sockWrite(io, sock, body, deadline, &started);
}
-fn sockWrite(sock: c_int, bytes: []const u8, deadline: i64) Err!void {
+/// The reader's answer to a server request: written now if the socket is
+/// free, else queued for the query writing (bounded; one that does not fit is
+/// dropped, and the server's request goes unanswered).
+fn answerServer(c: *Conn, io: std.Io, sock: c_int, body: []const u8) void {
+ var hdr: [64]u8 = undefined;
+ const h = std.fmt.bufPrint(&hdr, "Content-Length: {d}\r\n\r\n", .{body.len}) catch return;
+ c.replies_mu.lockUncancelable(io);
+ const half = &c.replies[c.filling];
+ const len = &c.replies_len[c.filling];
+ if (len.* + h.len + body.len <= half.len) {
+ @memcpy(half[len.*..][0..h.len], h);
+ @memcpy(half[len.* + h.len ..][0..body.len], body);
+ len.* += h.len + body.len;
+ }
+ c.replies_mu.unlock(io);
+ if (c.write_mu.tryLock()) releaseWrite(c, io, sock);
+}
+
+/// Lets the socket go, first sending what the reader queued meanwhile, and
+/// again for what it queues while that goes out.
+fn releaseWrite(c: *Conn, io: std.Io, sock: c_int) void {
+ while (true) {
+ c.replies_mu.lockUncancelable(io);
+ const sent = c.filling;
+ const n = c.replies_len[sent];
+ if (n > 0) c.filling ^= 1;
+ c.replies_mu.unlock(io);
+ if (n > 0) {
+ var started = false;
+ sockWrite(null, sock, c.replies[sent][0..n], nowMs() + reply_ms, &started) catch {};
+ c.replies_len[sent] = 0;
+ }
+ c.write_mu.unlock(io);
+ c.replies_mu.lockUncancelable(io);
+ const more = c.replies_len[c.filling] > 0;
+ c.replies_mu.unlock(io);
+ if (!more or !c.write_mu.tryLock()) return;
+ }
+}
+
+/// Writes `bytes`, `started` once any byte of their message is out. Before
+/// then, the deadline and a cancel stop it; after, the message is finished
+/// however long the server takes to read it (bounded by wedge_ms), since
+/// half of one ends the server's reading of every message after it.
+fn sockWrite(io: ?std.Io, sock: c_int, bytes: []const u8, deadline: i64, started: *bool) Err!void {
var off: usize = 0;
while (off < bytes.len) {
- const left = deadline - nowMs();
- if (left <= 0) return error.Timeout;
+ if (!started.*) {
+ if (deadline - nowMs() <= 0) return error.Timeout;
+ if (io) |cancel_io| cancel_io.checkCancel() catch return error.Canceled;
+ } else if (nowMs() - deadline > wedge_ms) return error.Dead;
var pfd = [_]libc.pollfd{.{ .fd = sock, .events = POLLOUT, .revents = 0 }};
- const pr = libc.poll(&pfd, 1, @intCast(@min(left, 1000)));
+ const pr = libc.poll(&pfd, 1, 500);
if (pr < 0) {
if (libc.errno(pr) == .INTR) continue;
return error.Dead;
@@ -2096,6 +2367,7 @@ fn sockWrite(sock: c_int, bytes: []const u8, deadline: i64) Err!void {
return error.Dead;
}
if (n == 0) return error.Dead;
+ started.* = true;
off += @intCast(n);
}
}
@@ -2318,7 +2590,7 @@ test "caller trees reverse incoming paths and merge shared callers and sites" {
try std.testing.expectEqual(@as(usize, 1), tree.roots.items.len);
try std.testing.expectEqual(@as(usize, 4), tree.nodes.items.len);
const root = tree.nodes.items[tree.roots.items[0]];
- try std.testing.expectEqualStrings("main", str(get(root.item, "name")).?);
+ try std.testing.expectEqualStrings("main", root.call.name);
try std.testing.expectEqual(@as(usize, 1), root.ranges.items.len);
const page = tree.nodes.items[root.children.items[0]];
try std.testing.expectEqual(@as(usize, 2), page.children.items.len);
@@ -2355,6 +2627,32 @@ test "caller trees reverse incoming paths and merge shared callers and sites" {
try std.testing.expect(recursive.nodes.items[1].cycle);
}
+test "test callers sort after the rest, each group by place" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const fixture = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"name\":\"test b\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"z\",\"uri\":\"file:///b.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"x\",\"uri\":\"file:///a_test.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"y\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":5,\"character\":0},\"end\":{\"line\":5,\"character\":1}}}]", .{});
+ var tree: CallTree = .{};
+ for (items(fixture)) |item| try tree.nodes.append(arena, .{ .call = try callOf(arena, item) });
+ var order = [_]usize{ 0, 1, 2, 3 };
+ std.mem.sort(usize, &order, @as(*const CallTree, &tree), callerLess);
+ try std.testing.expectEqualSlices(usize, &.{ 3, 1, 0, 2 }, &order);
+}
+
+test "a mailbox wait is a cancelation point: a canceled worker stops waiting" {
+ const io = std.testing.io;
+ var c: Conn = .{};
+ const Waiter = struct {
+ fn run(conn: *Conn, wio: std.Io) error{Canceled}!void {
+ conn.mu.lockUncancelable(wio);
+ defer conn.mu.unlock(wio);
+ while (true) _ = try timedWait(conn, wio, nowMs() + 60_000);
+ }
+ };
+ var future = try io.concurrent(Waiter.run, .{ &c, io });
+ try std.testing.expectError(error.Canceled, future.cancel(io));
+}
+
test "callstack cross-crate paths remain resolvable without absolute path clutter" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
@@ -2584,37 +2882,38 @@ test "a language with no server to start says so, not that it did not answer" {
defer arena.deinit();
var buf: [256]u8 = undefined;
var out: std.Io.Writer = .fixed(&buf);
- try query(std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out);
+ try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out);
try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered());
}
test "Lspinfo before any query says a server is not started yet, not off" {
var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
- try status(.{ .kind = .status, .path = "", .source = "", .offset = 0 }, &out.writer);
+ try status(std.testing.io, .{ .kind = .status, .path = "", .source = "", .offset = 0 }, &out.writer);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null);
}
test "a server that failed a moment ago is said to have failed, and when it is tried again, not to be missing" {
if (getenv("PARDES_LSP_ZIG") != null) return error.SkipZigTest;
+ const io = std.testing.io;
const si = specFor("/x/a.zig").?;
const c = &conns[si];
- c.lock();
+ c.mu.lockUncancelable(io);
const was = .{ c.state, c.retry_after_ms };
c.state = .dead;
c.retry_after_ms = nowMs() + 4500;
- c.unlock();
+ c.mu.unlock(io);
defer {
- c.lock();
+ c.mu.lockUncancelable(io);
c.state = was[0];
c.retry_after_ms = was[1];
- c.unlock();
+ c.mu.unlock(io);
}
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
- try query(std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer);
+ try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "zls failed recently; retry in 5s") != null);
}