summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/Host.zig3
-rw-r--r--dev/Inbox.zig11
-rw-r--r--dev/Lsp.zig40
-rw-r--r--dev/gui/gui.zig8
-rw-r--r--dev/lsp/lsp_client.zig88
-rw-r--r--dev/tui/tui.zig8
6 files changed, 151 insertions, 7 deletions
diff --git a/dev/Host.zig b/dev/Host.zig
index 52205518..0a175754 100644
--- a/dev/Host.zig
+++ b/dev/Host.zig
@@ -54,6 +54,9 @@ pub const VTable = struct {
/// startDetached. Null once it runs, else why not.
start: ?*const fn (ctx: ?*anyopaque, line: []const u8, why: []u8) ?[]const u8 = null,
lsp: ?*const fn (ctx: ?*anyopaque, req: Lsp.Request) void = null,
+ /// Start the language server of the file `pane` opened, and tell it of
+ /// the file, on a worker (Lsp.open).
+ lsp_open: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null,
pipe: ?*const fn (ctx: ?*anyopaque, id: u32) void = null,
/// Start the streaming search of this generation on a worker
/// (pardes.Stream.start); absent, it runs on the loop.
diff --git a/dev/Inbox.zig b/dev/Inbox.zig
index bc7c7e8a..a0b9494d 100644
--- a/dev/Inbox.zig
+++ b/dev/Inbox.zig
@@ -67,6 +67,9 @@ gpa: std.mem.Allocator,
lsp_allocator: std.mem.Allocator,
/// The query in flight; the UI thread's alone (spawnLsp, teardown).
lsp_task: ?Lsp.Task = null,
+/// The server start a file's opening asked for (openLsp); the UI
+/// thread's alone.
+lsp_opener: Lsp.Opener = .{},
/// The `|` pipes' and the addons' jobs' and draws' workers in flight, each
/// posting its answer here (`pushPipe`, `pushJob`); the loop's thread's
/// alone: joined as their answers land (`land`), canceled at the end.
@@ -290,6 +293,8 @@ pub fn deinit(q: *Inbox) void {
// The workers in flight end first: what they still deliver is dropped.
if (q.lsp_task) |*t| t.future.cancel(q.io);
q.lsp_task = null;
+ if (q.lsp_opener.future) |*f| f.cancel(q.io);
+ q.lsp_opener.future = null;
q.pipe_tasks.cancelAll(q.io);
q.job_tasks.cancelAll(q.io);
q.close();
@@ -343,6 +348,12 @@ pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void {
Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows);
}
+/// The language server of the file `pane` opened started, on a worker of
+/// its own (Lsp.open).
+pub fn openLsp(q: *Inbox, core: *const pardes.Pardes, pane: pardes.Pane.Id) void {
+ Lsp.open(q.io, q.lsp_allocator, core, &q.lsp_opener, pane);
+}
+
/// A worker's answer taken off the queue, to the core (whichever frontend
/// took it), its worker joined: a language server's answer or status line,
/// a pipe's, a search's rows, an addon's job or draw, a handover's end.
diff --git a/dev/Lsp.zig b/dev/Lsp.zig
index b799c9f6..d7638c83 100644
--- a/dev/Lsp.zig
+++ b/dev/Lsp.zig
@@ -138,6 +138,46 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, task: *?T
task.* = .{ .id = req.id, .future = future };
}
+/// The server start a file's opening asks for (`open`), one at a time:
+/// `busy` while its worker runs.
+pub const Opener = struct {
+ future: ?std.Io.Future(void) = null,
+ busy: std.atomic.Value(bool) = .init(false),
+};
+
+/// Pane `pane`'s file just opened: the server of its language started and
+/// told of it (lsp_client.open) on a worker of its own, so its first query
+/// finds both done (that query paid a spawn and a handshake, 2.5 s of zls's
+/// over the comparison video's project). Not through `start`, whose cancel
+/// of the query in flight waits out a handshake on the loop, nor `work`,
+/// whose fixed region is the queries'. One at a time: a file opened while
+/// one is under way is let go, and told at its first query (syncDoc); one
+/// done is awaited (at once) before the next.
+pub fn open(io: std.Io, gpa: std.mem.Allocator, core: *const pardes.Pardes, opener: *Opener, pane: pardes.Pane.Id) void {
+ if (opener.busy.load(.acquire)) return;
+ if (opener.future) |*f| f.await(io);
+ opener.future = null;
+ const p = core.panes[pane.index()] orelse return;
+ const f = p.file orelse return;
+ if (!pardes.lsp.speaks(f.path)) return;
+ const job = snapshot(gpa, core, .{ .id = 0, .kind = .definition, .pane = pane, .offset = 0, .arg = "" }) catch return;
+ opener.busy.store(true, .release);
+ opener.future = io.concurrent(tell, .{ io, gpa, job, &opener.busy }) catch {
+ opener.busy.store(false, .release);
+ return job.free(gpa);
+ };
+}
+
+fn tell(io: std.Io, gpa: std.mem.Allocator, job: *Job, busy: *std.atomic.Value(bool)) void {
+ defer busy.store(false, .release);
+ defer job.free(gpa);
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ // A server that failed to start says so itself (`ensure`); one not
+ // started for want of memory is started by the file's first query.
+ pardes.lsp.open(io, arena.allocator(), .{ .kind = job.kind, .path = job.path, .source = job.source, .offset = 0, .root = job.root }) catch {};
+}
+
pub fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void {
defer job.free(gpa);
// The query's memory is the fixed region memory.zig reserved at
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index a478db03..7a87c110 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -2648,6 +2648,7 @@ const Shell = struct {
.open_link = openLink,
.start = host_io.startDetached,
.lsp = lsp,
+ .lsp_open = lspOpen,
.pipe = pipe,
.search = search,
.hand_over = handOver,
@@ -3223,6 +3224,13 @@ fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
}
+/// Not held for `threads_ok`: the boot's file and a restored session's are
+/// opened before `start`.
+fn lspOpen(ctx: ?*anyopaque, pane: pardes.Pane.Id) void {
+ const s = shellOf(ctx);
+ s.queue.openLsp(s.core, pane);
+}
+
fn pipe(ctx: ?*anyopaque, id: u32) void {
const s = shellOf(ctx);
if (s.threads_ok) return selection_pipe.start(s.io, s.gpa, s.core, &s.queue.pipe_tasks, id, s.queue, Queue.pushPipe);
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig
index a53ff57f..be5069ae 100644
--- a/dev/lsp/lsp_client.zig
+++ b/dev/lsp/lsp_client.zig
@@ -6,9 +6,10 @@
//!
//! THE PROCESS LIFECYCLE IS THE DESIGN. `lsp.query` is a synchronous call on a
//! 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 an `Io.Mutex`
+//! and MINUTES to index a large workspace. So a server is spawned when a
+//! file of its language opens in a pane (`open`, on a worker of its own) or
+//! by 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 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.
@@ -40,7 +41,8 @@
//! (ignoring SIGPIPE process-wide would be inherited by every pty shell we
//! fork and would change what `yes | head` does in a pane),
//! - a binary missing from PATH costs one cached probe, disables the spec
-//! for the session, and says so ONCE on the message row.
+//! for the session, and says so ONCE on the message row (a query's; a
+//! file opened says nothing).
//!
//! Position encoding is negotiated to utf-8 and the SERVER'S ANSWER is
//! believed, not our request; the utf-16 conversion is implemented in both
@@ -478,8 +480,8 @@ fn status(io: std.Io, path: []const u8, out: *std.Io.Writer) !void {
const thier = c.caps.type_hier;
c.mu.unlock(io);
const env_v = if (libc.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) });
+ // Never spawned is not off: a file of its opened starts it.
+ try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (a file of its opened starts it)" else @tagName(state) });
if (state == .ready or state == .starting) {
try out.print(" root {s} {s}", .{ root, @tagName(enc) });
if (pull) try out.print(" pull-diags", .{});
@@ -757,6 +759,36 @@ pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.
try out.writeAll(scratch.buffered());
}
+/// A file opened in a pane (dev/Lsp.zig open): the server of its language
+/// started as its first query would start it (`ensure`, which says
+/// `starting` and `ready` on the message row) and told of the file
+/// (didOpen), so that query finds both done. A server not installed is
+/// disabled without a word: the file's first query says it is not found,
+/// not every file opened. Nothing in the query log: none was asked.
+/// PARDES_LSP_OPEN=off starts none (the process harnesses' default), and a
+/// test binary starts none unless it says `on` (the behaviour harness's
+/// language sessions, their servers the mock): no test starts a server of
+/// this machine's by opening a file.
+pub fn open(io: std.Io, arena: std.mem.Allocator, req: lsp.Req) Err!void {
+ const said = if (libc.getenv("PARDES_LSP_OPEN")) |v| std.mem.span(v) else "";
+ if (std.mem.eql(u8, said, "off") or (builtin.is_test and !std.mem.eql(u8, said, "on"))) return;
+ const si = specFor(req.path) orelse return;
+ const c = &conns[si];
+ try c.mu.lock(io);
+ defer c.mu.unlock(io);
+ if (c.state == .disabled) return;
+ var exe_buf: [4096:0]u8 = undefined;
+ if (!c.alive() and binOf(&specs[si], &exe_buf) == null) {
+ c.state = .disabled;
+ return;
+ }
+ var tr: Trace = .{};
+ _ = try ensure(c, io, si, arena, req, &tr);
+ var uri: std.ArrayList(u8) = .empty;
+ try uriOf(&uri, arena, req.path);
+ try syncDoc(c, io, si, arena, uri.items, req.source);
+}
+
fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us: u64) std.Io.Writer.Error!void {
if (req.kind != .explain) return;
try out.print("lsp explain — the {s} query at byte {d} of {s}\n\n", .{
@@ -3162,11 +3194,53 @@ test "a language with no server to start says so, not that it did not answer" {
try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered());
}
+test "a file opened whose server is not installed disables it without a word, at every file's open; a query of it then says it is not found" {
+ const host_io = @import("../host_io.zig");
+ const io = std.testing.io;
+ const si = specFor("/x/a.py") orelse return;
+ const c = &conns[si];
+ const was_state = c.state;
+ defer c.state = was_state;
+ // A test binary's files start servers only where it says so.
+ const names = [_][*:0]const u8{ "PARDES_LSP_PY", "PARDES_LSP_OPEN" };
+ var was_buf: [names.len][4096:0]u8 = undefined;
+ var was: [names.len]?[:0]const u8 = undefined;
+ for (names, &was, &was_buf) |name, *w, *b| w.* = if (libc.getenv(name)) |v| try std.fmt.bufPrintSentinel(b, "{s}", .{std.mem.span(v)}, 0) else null;
+ defer for (names, was) |name, w| {
+ _ = if (w) |v| host_io.Shell.setenv(name, v.ptr, 1) else host_io.testUnsetenv(name);
+ };
+ _ = host_io.Shell.setenv("PARDES_LSP_PY", "/nonexistent/pyright-langserver", 1);
+ _ = host_io.Shell.setenv("PARDES_LSP_OPEN", "on", 1);
+ const Said = struct {
+ var n: usize = 0;
+ fn post(_: ?*anyopaque, _: []const u8) void {
+ n += 1;
+ }
+ };
+ Said.n = 0;
+ // A word said before is never said twice in a row (`post`): forgotten.
+ sink_last_len[si] = 0;
+ setStatusSink(null, Said.post);
+ defer setStatusSink(null, null);
+ c.state = .off;
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ for ([_][]const u8{ "/x/a.py", "/x/b.py" }) |path| {
+ try open(io, arena.allocator(), .{ .kind = .definition, .path = path, .source = "", .offset = 0, .root = "/x" });
+ try std.testing.expectEqual(State.disabled, c.state);
+ }
+ try std.testing.expectEqual(@as(usize, 0), Said.n);
+ var buf: [256]u8 = undefined;
+ var out: std.Io.Writer = .fixed(&buf);
+ try query(io, 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(std.testing.io, "/x/notes.txt", &out.writer);
- try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null);
+ try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (a file of its opened 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" {
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index aa1f8849..f7b9cae8 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -1328,6 +1328,7 @@ const Shell = struct {
.open_link = openLink,
.start = host_io.startDetached,
.lsp = lsp,
+ .lsp_open = lspOpen,
.pipe = pipe,
.search = search,
.job = job,
@@ -2047,6 +2048,13 @@ const Shell = struct {
s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
}
+ /// Not held for `threads_ok`: the boot's file and a restored
+ /// session's are opened before `start`.
+ fn lspOpen(ctx: ?*anyopaque, pane: pardes.Pane.Id) void {
+ const s = of(ctx);
+ s.ptys.queue.openLsp(s.core, pane);
+ }
+
fn pipe(ctx: ?*anyopaque, id: u32) void {
const s = of(ctx);
if (!s.threads_ok) {