summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Effect.zig6
-rw-r--r--core/File.zig8
-rw-r--r--core/dump.zig6
-rw-r--r--core/lsp/lsp.zig6
-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
-rw-r--r--docs/typ/reference.typ2
-rw-r--r--docs/typ/setup.typ6
-rw-r--r--runtime/macos.zig6
-rw-r--r--test/behaviour/language_servers.zig57
-rw-r--r--test/daemon.py3
-rw-r--r--test/fs.py2
-rw-r--r--test/process.zig5
-rw-r--r--test/selfmount.py2
-rw-r--r--test/session.zig19
-rw-r--r--test/tty.zig5
20 files changed, 274 insertions, 17 deletions
diff --git a/core/Effect.zig b/core/Effect.zig
index 3bca04c9..72244f2b 100644
--- a/core/Effect.zig
+++ b/core/Effect.zig
@@ -64,6 +64,10 @@ pub const Effect = union(enum) {
/// `arg` up to 128 bytes, or for `explain` the query it explains,
/// `<kind> <arg>`.
lsp: struct { id: u32, kind: lsp.Kind, pane: Pane.Id, offset: u32, arg: Buf(160) },
+ /// A file opened in pane `pane` (File.open, a session's revived pane):
+ /// the server its language speaks to started and told of it, off the
+ /// loop (dev/Lsp.zig open), so its first query finds it ready.
+ lsp_open: struct { pane: Pane.Id },
/// Snapshot the matching request with pipeRequest(id), then run it away
/// from the UI/event loop and answer with pipe_resp.
pipe: struct { id: u32 },
@@ -216,6 +220,8 @@ fn performInner(p: *Pardes, e: Effect) void {
},
.lsp => |q| p.tape.host(p, .lsp, .{.{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }}) orelse
p.update(.{ .lsp_resp = .{ .id = q.id, .rows = "" } }),
+ // No workers here (a test, a board): no server starts before a query.
+ .lsp_open => |o| _ = p.tape.host(p, .lsp_open, .{o.pane}),
.pipe => |q| p.tape.host(p, .pipe, .{q.id}) orelse
p.update(.{ .pipe_resp = .{ .id = q.id, .success = false, .outputs = &.{} } }),
// No workers here (a test, a board): it runs on the loop.
diff --git a/core/File.zig b/core/File.zig
index 0f9178ac..d16b0c73 100644
--- a/core/File.zig
+++ b/core/File.zig
@@ -922,7 +922,13 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane {
pane.body.cur_pinned = true;
pane.body.cur_row = @intCast(scroll);
p.installPane(id, pane);
- if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } });
+ if (filesystem.localPath(path) != null) {
+ p.emit(.{ .watch = .{ .pane = .at(id), .on = true } });
+ // Its language's server started now, off the loop: the first
+ // question asked of it finds it ready. Not a picker's preview's,
+ // which opens no pane (Pick.preview).
+ p.emit(.{ .lsp_open = .{ .pane = .at(id) } });
+ }
return pane;
}
diff --git a/core/dump.zig b/core/dump.zig
index 2dde9c8c..d7c44b71 100644
--- a/core/dump.zig
+++ b/core/dump.zig
@@ -718,8 +718,9 @@ fn reviveSession(p: *Pardes, previous: ?*Pardes, resumed: bool) !void {
}
/// A loaded pane given to this host: a terminal a new shell in its old
-/// one's directory under its history, a file watched again (one changed on
-/// disk since comes back as it is now, one unsaved is marked changed), a
+/// one's directory under its history, a file watched again and its
+/// language's server started (one changed on disk since comes back as it
+/// is now, one unsaved is marked changed), a
/// kind's pane opened again (a PDF a build before the pdf addon dumped, as
/// the addon's).
fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void {
@@ -762,6 +763,7 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool
} else |_| {}
}
p.emit(.{ .watch = .{ .pane = .at(id), .on = true } });
+ p.emit(.{ .lsp_open = .{ .pane = .at(id) } });
// Its file gone from disk since the dump: its text is nowhere else, so
// it is said deleted, and Del, Exit and Restore ask first. One never on
// disk (a name not yet saved) comes back as it was.
diff --git a/core/lsp/lsp.zig b/core/lsp/lsp.zig
index 7a636f72..00ffc98b 100644
--- a/core/lsp/lsp.zig
+++ b/core/lsp/lsp.zig
@@ -96,6 +96,12 @@ pub fn query(io: std.Io, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer
return client.query(io, arena, req, out);
}
+/// A file opened in a pane: its language's server started and told of it
+/// (lsp_client.open), on a host's worker.
+pub fn open(io: std.Io, arena: std.mem.Allocator, req: Req) !void {
+ return client.open(io, arena, req);
+}
+
/// The one line the client answers with when it cannot answer at all, and
/// why: the core says it rather than "nothing found".
pub fn none(out: *std.Io.Writer, comptime fmt: []const u8, args: anytype) std.Io.Writer.Error!void {
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) {
diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ
index 42b44fc9..a08d3acc 100644
--- a/docs/typ/reference.typ
+++ b/docs/typ/reference.typ
@@ -295,7 +295,7 @@ A line written to #file("exec") is #word("Exec"):
index the file asks (`scip: <path>, <size>, <documents>, <symbols>,
indexed <when>, <n> files newer`, or `scip: no index.scip above <dir>;
Index runs <indexer>`), which server serves it and its state (`not
- started yet` until its first query starts it, `<server> failed recently;
+ started yet` until a file of its opened starts it, `<server> failed recently;
retry in Ns` while it backs off), and what answered each query lately
(`scip`, or the server);
#word("Lspwhy") narrates the last query step by step (in `+Lsp`). The
diff --git a/docs/typ/setup.typ b/docs/typ/setup.typ
index c9556c61..3ef67da5 100644
--- a/docs/typ/setup.typ
+++ b/docs/typ/setup.typ
@@ -119,8 +119,10 @@ script on that `PATH` is a word you can click.
= Language servers <language-servers>
-pardes starts a language server for a file it knows, searching `PATH` first
-and then `~/.local/bin` (desktop launchers may leave it out of `PATH`):
+pardes starts a language server when a file it knows opens in a pane (a
+picker's preview starts none), searching `PATH` first and then
+`~/.local/bin` (desktop launchers may leave it out of `PATH`), so the
+file's first question finds it ready:
#pairs(
[`.zig`], [`zls` (`PARDES_LSP_ZIG`)],
diff --git a/runtime/macos.zig b/runtime/macos.zig
index 7e0f50e7..6a6d39a2 100644
--- a/runtime/macos.zig
+++ b/runtime/macos.zig
@@ -1628,6 +1628,7 @@ const vtable: pardes.Host.VTable = .{
.open_link = openLink,
.start = host_io.startDetached,
.lsp = lspRequest,
+ .lsp_open = lspOpen,
.pipe = pipeRequest,
};
@@ -1640,6 +1641,11 @@ fn lspRequest(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
st.inbox.startLsp(st.core, req);
}
+fn lspOpen(ctx: ?*anyopaque, pane: pardes.Pane.Id) void {
+ const st = hostState(ctx);
+ st.inbox.openLsp(st.core, pane);
+}
+
fn pipeRequest(ctx: ?*anyopaque, id: u32) void {
const st = hostState(ctx);
selection_pipe.start(st.io, st.gpa, st.core, &st.pipe_tasks, id, &st.inbox, Inbox.pushPipe);
diff --git a/test/behaviour/language_servers.zig b/test/behaviour/language_servers.zig
index 13e42fa6..79b556f4 100644
--- a/test/behaviour/language_servers.zig
+++ b/test/behaviour/language_servers.zig
@@ -323,3 +323,60 @@ test "language-servers: SPC l a lists the server's code actions in the pane's ta
try s.keys("u");
try s.expect(body, before);
}
+
+extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
+
+/// The row of Lspinfo's report on the server `name` (`zls`), asked of the
+/// pane with the keyboard, and how many queries its client has answered.
+fn serverRow(s: *Session, name: []const u8) !struct { []const u8, []const u8 } {
+ try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Lspinfo\n");
+ const info = try bodyOf(s, "+Lsp");
+ const at = (std.mem.indexOf(u8, info, s.at("\n {s} ", .{name})) orelse return error.NoSuchServer) + 1;
+ const row = info[at .. std.mem.indexOfScalarPos(u8, info, at, '\n') orelse info.len];
+ const log = std.mem.indexOf(u8, info, "client queries (") orelse return error.NoQueryLog;
+ return .{ row, info[log .. std.mem.indexOfPos(u8, info, log, " total") orelse return error.NoQueryLog] };
+}
+
+test "language-servers: a file of a server's language opened in a pane starts that server and tells it of the file, with no query asked; a picker's preview of one starts none, a second file no second server, and a server not installed is found silently" {
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ // A work tree of its own: Find searches it, not the one the test runs in.
+ try tmp.dir.createDir(io, ".git", .default_dir);
+ try tmp.dir.writeFile(io, .{ .sub_path = "a.zig", .data = "const a = 1;\n" });
+ try tmp.dir.writeFile(io, .{ .sub_path = "b.zig", .data = "const b = 2;\n" });
+ try tmp.dir.writeFile(io, .{ .sub_path = "c.py", .data = "c = 3\n" });
+ try tmp.dir.writeFile(io, .{ .sub_path = "d.py", .data = "d = 4\n" });
+ try tmp.dir.writeFile(io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(io, &buf)];
+ var s = try Session.initLanguage(std.testing.allocator);
+ defer s.deinit();
+ _ = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
+ // a.zig previewed by Find's picker (the session's jumps walk): its text
+ // shown, its server (the scripted one, the session's zls) not started.
+ try s.keys(" ffa.zig");
+ s.tick();
+ try std.testing.expect(has(try s.screen(), "const a = 1;"));
+ try s.keys("\x1b");
+ const before, const asked = try serverRow(&s, "zls");
+ try std.testing.expect(has(before, "not started yet"));
+ // Opened: started and told of a.zig, no query asked of it.
+ _ = try s.lookFile(s.at("{s}/a.zig", .{dir}));
+ const opened, const asked_open = try serverRow(&s, "zls");
+ try std.testing.expect(has(opened, "ready") and has(opened, "docs 1"));
+ try std.testing.expectEqualStrings(asked, asked_open);
+ // A second file: the same server told of it too (a second would know one).
+ _ = try s.lookFile(s.at("{s}/b.zig", .{dir}));
+ const second, _ = try serverRow(&s, "zls");
+ try std.testing.expect(has(second, "ready") and has(second, "docs 2"));
+ // Not installed: the language's server disabled at the first file's
+ // open, with no word at either (its first query says it is not found).
+ _ = setenv("PARDES_LSP_PY", "/nonexistent/pyright-langserver", 1);
+ _ = try s.lookFile(s.at("{s}/c.py", .{dir}));
+ _ = try s.lookFile(s.at("{s}/d.py", .{dir}));
+ const missing, const asked_missing = try serverRow(&s, "pyright");
+ try std.testing.expect(has(missing, "disabled"));
+ try std.testing.expectEqualStrings(asked, asked_missing);
+ try std.testing.expect(!has(try s.screen(), "not found"));
+}
diff --git a/test/daemon.py b/test/daemon.py
index 3dfb0710..95a4da75 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -92,6 +92,9 @@ def base_env():
of the user's) or a pager; under `--with-pair`, PARDES_PAIR set in it, as
for a user who exported it: every mode run with the variable."""
env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k}
+ # No language server starts at a file's open (lsp_client.open): none of
+ # this machine's.
+ env['PARDES_LSP_OPEN'] = 'off'
if WITH_PAIR:
env['PARDES_PAIR'] = '1'
return env
diff --git a/test/fs.py b/test/fs.py
index a2b67cbc..abf9f1b6 100644
--- a/test/fs.py
+++ b/test/fs.py
@@ -32,6 +32,8 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False
env.update(inherited or {})
for language in ['RS', 'C', 'GO', 'TS', 'PY']:
env['PARDES_LSP_' + language] = ''
+ # No server starts at a file's open (lsp_client.open): none of this machine's.
+ env['PARDES_LSP_OPEN'] = 'off'
fixture = root / (name + '.txt')
fixture.write_bytes(b'initial\n')
address = root / ('pardes-9p-' + (socket_name or name) + '.sock')
diff --git a/test/process.zig b/test/process.zig
index 791a5b8e..fe5d97f0 100644
--- a/test/process.zig
+++ b/test/process.zig
@@ -960,9 +960,12 @@ pub const Child = struct {
// never the user's: the dumps a test makes there would push the
// user's out.
if (root_z != null) _ = unsetenv("XDG_DATA_HOME");
+ // No language server starts at a file's open (lsp_client.open):
+ // the `lsp` cases' zls is this machine's, started by their
+ // first query as before.
for ([_][2][*:0]const u8{
.{ "PARDES_DAEMON", "off" }, .{ "PARDES_LSP_RS", "" }, .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" },
- .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" },
+ .{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" }, .{ "PARDES_LSP_OPEN", "off" },
}) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
if (opts.pty != null and setenv("TERM", "xterm-256color", 1) != 0) _exit(126);
// No test reaches the user's display: a window any of them
diff --git a/test/selfmount.py b/test/selfmount.py
index 61d32523..aa4540b0 100644
--- a/test/selfmount.py
+++ b/test/selfmount.py
@@ -81,6 +81,8 @@ def run(binary):
env.update(PARDES_NOTIME='1', TERM='xterm-256color', PARDES_DUMP=f'/tmp/{name}.dump.zon')
for language in ['RS', 'C', 'GO', 'TS', 'PY']:
env['PARDES_LSP_' + language] = ''
+ # No server starts at a file's open (lsp_client.open): none of this machine's.
+ env['PARDES_LSP_OPEN'] = 'off'
pid, master = pty.fork()
if pid == 0:
os.execve(ns, ['9ns', '--mntgen', '--', binary, '--tty', '--9p=' + name, str(fixture)], env)
diff --git a/test/session.zig b/test/session.zig
index 13798753..9285c13b 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -38,6 +38,8 @@ pub const Session = struct {
/// What the language words' questions are asked of their servers
/// through (initLanguage); none, and they go unanswered.
languages: ?std.Io = null,
+ /// The server start a file's opening asks for (`languages`'s).
+ opener: pardes.host_io.Lsp.Opener = .{},
/// Kinds' jobs taken as a host's worker takes them and held, not run,
/// until `releaseJobs`: a job under way across what a test does
/// meanwhile (a reload).
@@ -62,12 +64,19 @@ pub const Session = struct {
}
/// A session showing one scratch pane that asks language servers as a
- /// host does, a `.rs` file's the scripted one (test/lspmock.zig, which
- /// answers over a main.rs and the lib.rs beside it).
+ /// host does, a `.rs` and a `.zig` file's the scripted one
+ /// (test/lspmock.zig, which answers over a main.rs and the lib.rs beside
+ /// it), every other language's none: no test starts a server of this
+ /// machine's.
pub fn initLanguage(gpa: std.mem.Allocator) !Session {
var s = try initFile(gpa, "x\n");
const mock = try std.Io.Dir.cwd().realPathFileAlloc(std.testing.io, @import("session_options").lspmock, s.arena.allocator());
_ = setenv("PARDES_LSP_RS", mock, 1);
+ _ = setenv("PARDES_LSP_ZIG", mock, 1);
+ for ([_][*:0]const u8{ "PARDES_LSP_C", "PARDES_LSP_GO", "PARDES_LSP_TS", "PARDES_LSP_PY" }) |v| _ = setenv(v, "", 1);
+ // A test binary's opened files start their servers where it says
+ // so (lsp_client.open): here, the mock.
+ _ = setenv("PARDES_LSP_OPEN", "on", 1);
s.languages = std.testing.io;
return s;
}
@@ -295,6 +304,12 @@ pub const Session = struct {
// held while a test holds them (`hold_jobs`).
.job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane.index(), j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(),
.lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }),
+ // A file's server started as a host starts it, and waited for.
+ .lsp_open => |o| if (s.languages) |io| {
+ pardes.host_io.Lsp.open(io, pardesOf(s).gpa, pardesOf(s), &s.opener, o.pane);
+ if (s.opener.future) |*f| f.await(io);
+ s.opener.future = null;
+ },
.watch => |w| {
if (s.watch_fd < 0) s.watch_fd = pardes.file_watch.init(true);
if (s.watch_fd >= 0) _ = pardes.file_watch.applyEffect(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), s.watch_fd, &s.watches, w.pane.index(), w.on, w.mode);
diff --git a/test/tty.zig b/test/tty.zig
index 97b64b7c..cbb4e446 100644
--- a/test/tty.zig
+++ b/test/tty.zig
@@ -128,9 +128,12 @@ pub const Tty = struct {
_ = setenv("PARDES_NOTIME", "1", 1);
_ = setenv("PARDES_TEST_CLOCK", "1", 1);
// Hermetic language clients: `.rs` speaks to the mock, every other
- // spec is off, so a stray `.c` never spawns this machine's clangd.
+ // spec but zls is off, so a stray `.c` never spawns this machine's
+ // clangd, and no server starts at a file's open (lsp_client.open):
+ // the `lsp` cases' zls is started by their first query.
_ = setenv("PARDES_LSP_RS", try arena.dupeSentinel(u8, lspmock, 0), 1);
for ([_][*:0]const u8{ "PARDES_LSP_C", "PARDES_LSP_GO", "PARDES_LSP_TS", "PARDES_LSP_PY" }) |v| _ = setenv(v, "", 1);
+ _ = setenv("PARDES_LSP_OPEN", "off", 1);
const dump = try std.fmt.allocPrintSentinel(arena, "{s}/dump.zon", .{base}, 0);
_ = setenv("PARDES_DUMP", dump, 1);
if (replay) {