diff options
Diffstat (limited to 'test/session.zig')
| -rw-r--r-- | test/session.zig | 34 |
1 files changed, 34 insertions, 0 deletions
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)); } |
