summaryrefslogtreecommitdiff
path: root/test/behaviour/language_servers.zig
blob: 7f1eef73b7a96589148cda2b53ecd03df7ede09d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! docs/typ/setup.typ, Language servers <language-servers>: a file's
//! language words ask the nearest `index.scip` above it first (SCIP,
//! read by dev/lsp/Scip.zig) and its language server when the index has
//! nothing to say, here the scripted one (test/lspmock.zig). The indexes
//! are made by the protobuf writer below, but for the one a real indexer
//! wrote (test/scipfixture).
const std = @import("std");
const Session = @import("session").Session;

const lib_rs =
    \\pub trait Shape {
    \\    fn area(&self) -> u32;
    \\}
    \\/// Adds two numbers.
    \\pub fn add(a: u32, b: u32) -> u32 {
    \\    a + b
    \\}
    \\pub struct Square;
    \\impl Shape for Square {
    \\    fn area(&self) -> u32 { add(2, 2) }
    \\}
    \\
;
const main_rs =
    \\mod lib;
    \\use lib::add;
    \\fn main() {
    \\    let x = add(1, 2);
    \\    let _ = x;
    \\}
    \\
;

/// One occurrence: its document (0 main.rs, 1 lib.rs), line and columns,
/// symbol, roles, and the node a definition names.
const Occ = struct { doc: u8, line: u32, from: u32, to: u32, sym: []const u8, roles: u32 = 0, node: ?[4]u32 = null };
/// One symbol's information, in the document defining it.
const Sym = struct { doc: u8, sym: []const u8, kind: u32, name: []const u8, sig: []const u8 = "", docs: []const u8 = "", implements: []const u8 = "" };

const occs = [_]Occ{
    .{ .doc = 0, .line = 1, .from = 9, .to = 12, .sym = "t . add().", .roles = 2 },
    .{ .doc = 0, .line = 2, .from = 3, .to = 7, .sym = "t . main().", .roles = 1, .node = .{ 2, 0, 5, 1 } },
    .{ .doc = 0, .line = 3, .from = 8, .to = 9, .sym = "local 0", .roles = 1 },
    .{ .doc = 0, .line = 3, .from = 12, .to = 15, .sym = "t . add()." },
    .{ .doc = 0, .line = 4, .from = 12, .to = 13, .sym = "local 0" },
    .{ .doc = 1, .line = 0, .from = 10, .to = 15, .sym = "t . Shape#", .roles = 1, .node = .{ 0, 0, 2, 1 } },
    .{ .doc = 1, .line = 1, .from = 7, .to = 11, .sym = "t . Shape#area().", .roles = 1, .node = .{ 1, 4, 1, 26 } },
    .{ .doc = 1, .line = 4, .from = 7, .to = 10, .sym = "t . add().", .roles = 1, .node = .{ 3, 0, 6, 1 } },
    .{ .doc = 1, .line = 4, .from = 11, .to = 12, .sym = "local 0", .roles = 1 },
    .{ .doc = 1, .line = 7, .from = 11, .to = 17, .sym = "t . Square#", .roles = 1, .node = .{ 7, 0, 7, 18 } },
    .{ .doc = 1, .line = 8, .from = 5, .to = 10, .sym = "t . Shape#" },
    .{ .doc = 1, .line = 8, .from = 15, .to = 21, .sym = "t . Square#" },
    .{ .doc = 1, .line = 9, .from = 7, .to = 11, .sym = "t . Square#area().", .roles = 1, .node = .{ 9, 4, 9, 39 } },
    .{ .doc = 1, .line = 9, .from = 28, .to = 31, .sym = "t . add()." },
};
const syms = [_]Sym{
    .{ .doc = 0, .sym = "t . main().", .kind = 17, .name = "main", .sig = "fn main()" },
    .{ .doc = 0, .sym = "local 0", .kind = 61, .name = "x", .sig = "let x: u32" },
    .{ .doc = 1, .sym = "t . Shape#", .kind = 53, .name = "Shape", .sig = "pub trait Shape" },
    .{ .doc = 1, .sym = "t . Shape#area().", .kind = 70, .name = "area", .sig = "fn area(&self) -> u32" },
    .{ .doc = 1, .sym = "t . add().", .kind = 17, .name = "add", .sig = "pub fn add(a: u32, b: u32) -> u32", .docs = "Adds two numbers." },
    .{ .doc = 1, .sym = "local 0", .kind = 37, .name = "a", .sig = "a: u32" },
    .{ .doc = 1, .sym = "t . Square#", .kind = 49, .name = "Square", .implements = "t . Shape#" },
    .{ .doc = 1, .sym = "t . Square#area().", .kind = 26, .name = "area", .sig = "fn area(&self) -> u32", .implements = "t . Shape#area()." },
};

/// A protobuf message, its fields appended in order.
const Msg = struct {
    b: std.ArrayList(u8) = .empty,

    fn varint(m: *Msg, a: std.mem.Allocator, v: u64) void {
        var x = v;
        while (x >= 0x80) : (x >>= 7) m.b.append(a, @as(u8, @truncate(x)) | 0x80) catch @panic("oom");
        m.b.append(a, @truncate(x)) catch @panic("oom");
    }

    fn int(m: *Msg, a: std.mem.Allocator, num: u64, v: u64) void {
        m.varint(a, num << 3);
        m.varint(a, v);
    }

    fn bytes(m: *Msg, a: std.mem.Allocator, num: u64, v: []const u8) void {
        m.varint(a, num << 3 | 2);
        m.varint(a, v.len);
        m.b.appendSlice(a, v) catch @panic("oom");
    }
};

/// The index of main.rs and lib.rs: typed ranges, as new indexers write
/// them (single-line ranges, a definition's node a multi-line one).
fn index(a: std.mem.Allocator) []const u8 {
    var ix: Msg = .{};
    for ([_][]const u8{ "main.rs", "lib.rs" }, 0..) |path, d| {
        var doc: Msg = .{};
        doc.bytes(a, 1, path);
        for (occs) |o| if (o.doc == d) {
            var m: Msg = .{};
            var r: Msg = .{};
            for ([_]u64{ o.line, o.from, o.to }, 1..) |v, n| r.int(a, n, v);
            m.bytes(a, 8, r.b.items);
            m.bytes(a, 2, o.sym);
            if (o.roles != 0) m.int(a, 3, o.roles);
            if (o.node) |node| {
                var e: Msg = .{};
                for (node, 1..) |v, n| e.int(a, n, v);
                m.bytes(a, 11, e.b.items);
            }
            doc.bytes(a, 2, m.b.items);
        };
        for (syms) |s| if (s.doc == d) {
            var m: Msg = .{};
            m.bytes(a, 1, s.sym);
            if (s.docs.len > 0) m.bytes(a, 3, s.docs);
            if (s.implements.len > 0) {
                var rel: Msg = .{};
                rel.bytes(a, 1, s.implements);
                rel.int(a, 3, 1);
                m.bytes(a, 4, rel.b.items);
            }
            m.int(a, 5, s.kind);
            m.bytes(a, 6, s.name);
            if (s.sig.len > 0) {
                var sig: Msg = .{};
                sig.bytes(a, 5, s.sig);
                m.bytes(a, 7, sig.b.items);
            }
            doc.bytes(a, 3, m.b.items);
        };
        doc.int(a, 6, 1);
        ix.bytes(a, 2, doc.b.items);
    }
    return ix.b.items;
}

/// main.rs, lib.rs and their index in a directory of their own, main.rs
/// shown with its cursor at `at` (`4:13`, its call of add); its serial.
fn open(s: *Session, tmp: *std.testing.TmpDir, at: []const u8) !u32 {
    const io = std.testing.io;
    try tmp.dir.writeFile(io, .{ .sub_path = "main.rs", .data = main_rs });
    try tmp.dir.writeFile(io, .{ .sub_path = "lib.rs", .data = lib_rs });
    try tmp.dir.writeFile(io, .{ .sub_path = "index.scip", .data = index(s.arena.allocator()) });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(io, &buf)];
    return s.lookFile(s.at("{s}/main.rs:{s}", .{ dir, at }));
}

fn has(hay: []const u8, needle: []const u8) bool {
    return std.mem.indexOf(u8, hay, needle) != null;
}

/// The body of the pane /index names with `suffix` (`+Hover`).
fn bodyOf(s: *Session, suffix: []const u8) ![]const u8 {
    var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n');
    while (rows.next()) |line| {
        // serial kind dirty name column
        const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue];
        if (std.mem.endsWith(u8, name, suffix)) return s.read(s.at("/pane/{s}/body", .{line[0..std.mem.indexOfScalar(u8, line, ' ').?]}));
    }
    return error.NoSuchPane;
}

test "language-servers: Def asks the index first, and lands on the definition it names" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Def\n");
    const lib = try s.focused();
    try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{lib})), "/lib.rs\n"));
    // `add` on lib.rs's fifth line, where the scripted server would have
    // said its second.
    try std.testing.expectEqual(.{ @as(u32, 76), @as(u32, 79) }, try s.range(s.at("/pane/{d}/dot", .{lib})));
}

test "language-servers: Refs lists every occurrence the index has, the import and the definition among them" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Refs\n");
    const rows = try s.read(s.at("/pane/{d}/body", .{try s.focused()}));
    for ([_][]const u8{ "main.rs:2:10 ", "main.rs:4:13 ", "lib.rs:5:8 ", "lib.rs:10:29 " }) |place|
        try std.testing.expect(has(rows, place));
}

test "language-servers: Hover says the signature and the documentation the index has" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Hover\n");
    try std.testing.expectEqualStrings("pub fn add(a: u32, b: u32) -> u32\n\nAdds two numbers.\n", try bodyOf(&s, "+Hover"));
}

test "language-servers: Symbols lists the file's definitions by name, a local's left out" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    // About a jump opening a pane: `Jumps new-pane` (SPEC §6.6).
    try s.write("/ctl", "Jumps new-pane\n");
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/look", .{main}), s.at("{s}/lib.rs\n", .{std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{main})), "\n")).?}));
    try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Symbols\n");
    const rows = try bodyOf(&s, "+Search");
    for ([_][]const u8{ "lib.rs:1:11-15 Shape\n", "lib.rs:2:8-11 area\n", "lib.rs:5:8-10 add\n", "lib.rs:8:12-17 Square\n", "lib.rs:10:8-11 area\n" }) |row|
        try std.testing.expect(has(rows, row));
    try std.testing.expect(!has(rows, "lib.rs:5:12"));
}

test "language-servers: Callers lists each call with the function it is made from, Subtypes the implementations the index relates" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Callers\n");
    const rows = try bodyOf(&s, "+Search");
    try std.testing.expect(has(rows, "main.rs:4:13-15\nmain\n"));
    try std.testing.expect(has(rows, "lib.rs:10:29-31\narea\n"));
    // Shape's one implementation is Square: a jump there, as gi's.
    const lib = try s.lookFile(s.at("{s}/lib.rs:1:11", .{std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{main})), "\n")).?}));
    try s.write(s.at("/pane/{d}/ctl", .{lib}), "Subtypes\n");
    try std.testing.expectEqual(.{ @as(u32, 128), @as(u32, 134) }, try s.range(s.at("/pane/{d}/dot", .{try s.focused()})));
}

test "language-servers: what the index does not know, and a pane with unsaved changes, ask the server" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    // About a jump opening a pane: `Jumps new-pane` (SPEC §6.6).
    try s.write("/ctl", "Jumps new-pane\n");
    // `mod`, which no occurrence covers: the scripted server's answer,
    // lib.rs's second line.
    const main = try open(&s, &tmp, "1:1");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Def\n");
    try std.testing.expectEqual(.{ @as(u32, 25), @as(u32, 28) }, try s.range(s.at("/pane/{d}/dot", .{try s.focused()})));
    try s.write(s.at("/pane/{d}/addr", .{main}), "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "dot=addr\n");
    try s.write(s.at("/pane/{d}/data", .{main}), "");
    try s.write(s.at("/pane/{d}/addr", .{main}), "$");
    try s.write(s.at("/pane/{d}/data", .{main}), "// unsaved\n");
    try s.write(s.at("/pane/{d}/addr", .{main}), "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "dot=addr\n");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Def\n");
    try std.testing.expectEqual(.{ @as(u32, 25), @as(u32, 28) }, try s.range(s.at("/pane/{d}/dot", .{try s.focused()})));
}

test "language-servers: a real indexer's index answers too (rust-analyzer's, test/scipfixture), and Lspinfo says what it holds" {
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    // About a jump opening a pane: `Jumps new-pane` (SPEC §6.6).
    try s.write("/ctl", "Jumps new-pane\n");
    const main = try s.lookFile("test/scipfixture/main.rs:5:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Hover\n");
    try std.testing.expectEqualStrings("pub fn add(a: i32, b: i32) -> i32\n\nAdds two numbers.\n", try bodyOf(&s, "+Hover"));
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Def\n");
    try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{try s.focused()})), "/test/scipfixture/lib.rs\n"));
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Lspinfo\n");
    const info = try bodyOf(&s, "+Lsp");
    try std.testing.expect(std.mem.startsWith(u8, info, "scip: "));
    try std.testing.expect(has(info, "/test/scipfixture/index.scip, 2 KB, 2 documents, 7 symbols"));
}

test "language-servers: Callstack's tree is the index's, each caller the definition holding its call, a function nobody calls a leaf" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const main = try open(&s, &tmp, "4:13");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Callstack\n");
    try s.expect(s.at("/pane/{d}/body", .{try s.focused()}),
        \\├──┬ lib.rs:10:8-11
        \\│  │ fn area(&self) -> u32 { add(2, 2) }
        \\│  ╰─ lib.rs:10:29-31
        \\│     fn area(&self) -> u32 { add(2, 2) }
        \\╰──┬ main.rs:3:4-7
        \\   │ fn main() {
        \\   ╰─ main.rs:4:13-15
        \\      let x = add(1, 2);
        \\
    );
}

test "language-servers: Index runs the language's indexer at the project's root, in a command pane" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "Cargo.toml", .data = "[package]\n" });
    const main = try open(&s, &tmp, "1:1");
    try s.write(s.at("/pane/{d}/ctl", .{main}), "Index\n");
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    try std.testing.expect(std.mem.startsWith(u8, try bodyOf(&s, dir), s.at("% cd '{s}' && rust-analyzer scip .", .{dir})));
}

test "language-servers: SPC l a lists the server's code actions in the pane's tag by their digits, the one under the cursor shown as its edit, and its digit applies it, one the server left to resolve resolved first" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    const before = "mod lib;\n\nfn main() {\n    let x = add(1, 2);\n    let y = add(3, 4);\n    let _ = x + y;\n}\n";
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "main.rs", .data = before });
    var buf: [4096]u8 = undefined;
    const main = try s.lookFile(s.at("{s}/main.rs:4:13", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}));
    const body = s.at("/pane/{d}/body", .{main});
    try s.keys(" la");
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Lsp") == null);
    _ = try s.where("▶ 1 mock action: extract");
    _ = try s.where("  2 mock action: add a line");
    _ = try s.where("  3 mock action: rename add to sum");
    // The third, its edit asked for with codeAction/resolve: the two calls.
    try s.keys("\u{F800}\u{F800}");
    _ = try s.where("let x = sum(1, 2);");
    try s.expect(body, before);
    try s.keys("3");
    try s.expect(body, "mod lib;\n\nfn main() {\n    let x = sum(1, 2);\n    let y = sum(3, 4);\n    let _ = x + y;\n}\n");
    try s.keys("u");
    try s.expect(body, before);
}

test "language-servers: SPC l a passes back the server's own diagnostics at the caret, as the protocol has it, so a quick fix the server offers for one alone (clangd's `change '.' to '->'`) is listed, first, and its digit applies it" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    // C's server the scripted one, as clangd is: its diagnostics pushed,
    // never pulled.
    _ = setenv("LSPMOCK_MODE", "push", 1);
    defer _ = setenv("LSPMOCK_MODE", "", 1);
    _ = setenv("PARDES_LSP_C", std.c.getenv("PARDES_LSP_RS").?, 1);
    defer _ = setenv("PARDES_LSP_C", "", 1);
    const before = "int f(struct s *b) {\n    return b.n;\n}\n";
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "main.c", .data = before });
    var buf: [4096]u8 = undefined;
    const main = try s.lookFile(s.at("{s}/main.c:2:13", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}));
    try s.keys(" la");
    _ = try s.where("▶ 1 change '.' to '->'");
    _ = try s.where("  2 mock action: extract");
    try s.keys("1");
    try s.expect(s.at("/pane/{d}/body", .{main}), "int f(struct s *b) {\n    return b->n;\n}\n");
}

test "language-servers: a server's Symbols are the Symbols picker's rows: types, functions, fields, then the rest, each its kind's mark and its name in the ones it is in (UX DESIGN N)" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var s = try Session.initLanguage(std.testing.allocator);
    defer s.deinit();
    try s.write("/ctl", "Jumps new-pane\n");
    // No index: the server is asked.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "shapes.rs", .data = "const LIMIT: u32 = 4;\n\nfn main() {}\npub struct Shape {\n    side: u32,\n    fn area() {}\n}\nenum Kind {\n    Round,\n}\nmod lib;\n" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    const shapes = try s.lookFile(s.at("{s}/shapes.rs:1:1", .{dir}));
    try s.write(s.at("/pane/{d}/ctl", .{shapes}), "Symbols\n");
    const rows = try bodyOf(&s, "+Search");
    var at: usize = 0;
    for ([_][]const u8{ "shapes.rs:4:12-16 t Shape\n", "shapes.rs:8:6-9 t Kind\n", "shapes.rs:3:4-7 f main\n", "shapes.rs:6:8-11 f Shape.area\n", "shapes.rs:5:5-8 m Shape.side\n", "shapes.rs:9:5-9 m Kind.Round\n", "shapes.rs:1:7-11 c LIMIT\n", "shapes.rs:11:5-7 n lib\n" }) |row| {
        const found = std.mem.indexOfPos(u8, rows, at, row) orelse {
            std.debug.print("{s} not after {d} in\n{s}\n", .{ row, at, rows });
            return error.TestUnexpectedResult;
        };
        at = found + row.len;
    }
}

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"));
}