diff options
Diffstat (limited to 'test/behaviour')
| -rw-r--r-- | test/behaviour/language_servers.zig | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/test/behaviour/language_servers.zig b/test/behaviour/language_servers.zig new file mode 100644 index 00000000..da7b3d59 --- /dev/null +++ b/test/behaviour/language_servers.zig @@ -0,0 +1,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}))); +} |
