diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/callstack.zig | 61 | ||||
| -rw-r--r-- | test/session.zig | 34 |
3 files changed, 96 insertions, 0 deletions
diff --git a/test/behaviour.zig b/test/behaviour.zig index e19a847f..8f412df6 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -5,6 +5,7 @@ test { _ = @import("behaviour/addresses.zig"); _ = @import("behaviour/builtins.zig"); + _ = @import("behaviour/callstack.zig"); _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); _ = @import("behaviour/event.zig"); diff --git a/test/behaviour/callstack.zig b/test/behaviour/callstack.zig new file mode 100644 index 00000000..42448031 --- /dev/null +++ b/test/behaviour/callstack.zig @@ -0,0 +1,61 @@ +//! docs/typ/guide.typ, Call hierarchy <callstack>, asked of the scripted +//! language server (test/lspmock.zig): `add` in lib.rs is called twice by +//! `main` in main.rs, and `main` by itself. +const std = @import("std"); +const Session = @import("session").Session; + +/// main.rs and lib.rs in a directory of their own, main.rs shown with its +/// cursor on the first call of `add`; its pane's serial. +fn openMain(s: *Session, tmp: *std.testing.TmpDir) !u32 { + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "main.rs", .data = "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 = "lib.rs", .data = "// mock fixture\npub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n" }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + return s.lookFile(s.at("{s}/main.rs:4:13", .{dir})); +} + +test "callstack: Callstack opens +Callstack, the outermost callers first, each with its signature and its call sites, a recursion marked" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initLanguage(std.testing.allocator); + defer s.deinit(); + const main = try openMain(&s, &tmp); + try s.write(s.at("/pane/{d}/ctl", .{main}), "Callstack\n"); + const tree = try s.focused(); + try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/name", .{tree})), "/+Callstack\n")); + try s.expect(s.at("/pane/{d}/body", .{tree}), + \\╰──┬ main.rs:3:4-7 + \\ │ fn main() { + \\ ├─ main.rs:4:13-15 + \\ │ let x = add(1, 2); + \\ ├─ main.rs:5:13-15 + \\ │ let y = add(3, 4); + \\ ╰──┬ main.rs:3:4-7 + \\ │ fn main() { ↻ recursion + \\ ├─ main.rs:4:13-15 + \\ │ let x = add(1, 2); + \\ ╰─ main.rs:5:13-15 + \\ let y = add(3, 4); + \\ + ); +} + +test "callstack: CallstackDepth bounds the walk, its last row inviting a Callstack from a function above" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var s = try Session.initLanguage(std.testing.allocator); + defer s.deinit(); + const main = try openMain(&s, &tmp); + try s.write("/ctl", "CallstackDepth 1\n"); + try s.write(s.at("/pane/{d}/ctl", .{main}), "Callstack\n"); + try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), + \\╰──┬ main.rs:3:4-7 + \\ │ fn main() { + \\ ├─ main.rs:4:13-15 + \\ │ let x = add(1, 2); + \\ ╰─ main.rs:5:13-15 + \\ let y = add(3, 4); + \\… continue from a function above with Callstack + \\ + ); +} diff --git a/test/session.zig b/test/session.zig index 41f2c376..c28c2892 100644 --- a/test/session.zig +++ b/test/session.zig @@ -10,6 +10,8 @@ const Pardes = pardes.Pardes; const Req = pardes.ctlfs.Req; const Reply = pardes.ctlfs.Reply; +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + pub const Session = struct { core: *anyopaque, /// What the calls answered, kept until deinit. @@ -19,6 +21,9 @@ pub const Session = struct { /// The words of the last refusal `refused` answered. said: []const u8 = "", tag: u64 = 0, + /// What the language words' questions are asked of their servers + /// through (initLanguage); none, and they go unanswered. + languages: ?std.Io = null, /// A session showing one file pane holding `text`. pub fn initFile(gpa: std.mem.Allocator, text: []const u8) !Session { @@ -30,6 +35,17 @@ pub const Session = struct { return s; } + /// A session showing one scratch pane that asks language servers as a + /// host does, a `.rs` file's the scripted one (test/lspmock.zig, which + /// answers over a main.rs and the lib.rs beside it). + pub fn initLanguage(gpa: std.mem.Allocator) !Session { + var s = try initFile(gpa, "x\n"); + const mock = try std.Io.Dir.cwd().realPathFileAlloc(std.testing.io, @import("session_options").lspmock, s.arena.allocator()); + _ = setenv("PARDES_LSP_RS", mock, 1); + s.languages = std.testing.io; + return s; + } + /// A session showing its terminal pane, as `pardes` starts. pub fn initTerm(gpa: std.mem.Allocator) !Session { const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); @@ -175,10 +191,28 @@ pub const Session = struct { fn drain(s: *Session) void { while (pardesOf(s).nextEffect()) |e| switch (e) { .save_file, .save_text => s.saved += 1, + .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), else => {}, }; } + /// A language word's question asked of its server as a host asks it + /// (host_io.Lsp), here and at once: its answer the core's next event. + fn askServer(s: *Session, io: std.Io, req: pardes.host_io.Lsp.Request) void { + const p = pardesOf(s); + const Lsp = pardes.host_io.Lsp; + const job = Lsp.snapshot(p.gpa, p, req) catch return p.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + var rows: ?[]u8 = null; + Lsp.work(io, p.gpa, job, &rows, struct { + fn keep(ctx: ?*anyopaque, _: u32, answer: ?[]u8) void { + const into: *?[]u8 = @ptrCast(@alignCast(ctx.?)); + into.* = answer; + } + }.keep); + defer if (rows) |owned| p.gpa.free(owned); + p.update(.{ .lsp_resp = .{ .id = req.id, .rows = rows } }); + } + fn pardesOf(s: *Session) *Pardes { return @ptrCast(@alignCast(s.core)); } |
