summaryrefslogtreecommitdiff
path: root/dev/Lsp.zig
diff options
context:
space:
mode:
Diffstat (limited to 'dev/Lsp.zig')
-rw-r--r--dev/Lsp.zig40
1 files changed, 40 insertions, 0 deletions
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