summaryrefslogtreecommitdiff
path: root/dev/lsp/lsp_client.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/lsp/lsp_client.zig')
-rw-r--r--dev/lsp/lsp_client.zig88
1 files changed, 81 insertions, 7 deletions
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" {