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
|
//! docs/typ/setup.typ, Language servers <language-servers>: a file's
//! language words ask the nearest `index.scip` above it first (SCIP,
//! read by src/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();
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\nShape\n", "lib.rs:2:8-11\narea\n", "lib.rs:5:8-10\nadd\n", "lib.rs:8:12-17\nSquare\n", "lib.rs:10:8-11\narea\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();
// `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();
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})));
}
|