summaryrefslogtreecommitdiff
path: root/test/lspmock.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/lspmock.zig')
-rw-r--r--test/lspmock.zig31
1 files changed, 23 insertions, 8 deletions
diff --git a/test/lspmock.zig b/test/lspmock.zig
index 6f52f568..03ade4aa 100644
--- a/test/lspmock.zig
+++ b/test/lspmock.zig
@@ -18,6 +18,10 @@
//! busy at the handshake, leave a child running in the server's
//! group, as a build a server started does; its pid said in
//! `lspmock-busy.pid` in the server's directory (its root)
+//! push diagnostics pushed, never pulled, as clangd has them: no
+//! diagnosticProvider, and at each didOpen and didChange one
+//! error published at line 1, characters 12..13 (the `.` of
+//! ` return b.n;`)
const std = @import("std");
const libc = std.c;
@@ -25,7 +29,7 @@ extern "c" fn getenv(name: [*:0]const u8) ?[*:0]const u8;
extern "c" fn fork() libc.pid_t;
extern "c" fn pause() c_int;
-var mode: enum { normal, mute, die, utf16, busy } = .normal;
+var mode: enum { normal, mute, die, utf16, busy, push } = .normal;
/// stdout, written with plain libc write(2): the transport is a socketpair
/// and the frames are small, so partial writes are retried and nothing else
@@ -124,6 +128,7 @@ pub fn main(init: std.process.Init) !void {
if (std.mem.eql(u8, s, "die")) mode = .die;
if (std.mem.eql(u8, s, "utf16")) mode = .utf16;
if (std.mem.eql(u8, s, "busy")) mode = .busy;
+ if (std.mem.eql(u8, s, "push")) mode = .push;
}
const gpa = std.heap.page_allocator;
@@ -210,10 +215,11 @@ fn handle(a: std.mem.Allocator, raw: []const u8) void {
\\"documentFormattingProvider":true,"codeActionProvider":{{"resolveProvider":true}},
\\"completionProvider":{{}},
\\"callHierarchyProvider":true,"typeHierarchyProvider":true,
- \\"diagnosticProvider":{{"interFileDependencies":false,"workspaceDiagnostics":false}},
- \\"workspace":{{"workspaceFolders":{{"supported":true}}}}}},
+ \\{s}"workspace":{{"workspaceFolders":{{"supported":true}}}}}},
\\"serverInfo":{{"name":"lspmock"}}}}
- , .{enc});
+ , .{ enc, if (mode == .push) "" else
+ \\"diagnosticProvider":{"interFileDependencies":false,"workspaceDiagnostics":false},
+ });
return;
}
if (std.mem.eql(u8, method, "initialized")) {
@@ -226,6 +232,8 @@ fn handle(a: std.mem.Allocator, raw: []const u8) void {
return;
}
if (std.mem.eql(u8, method, "textDocument/didOpen")) noteOpened(reqUri(params));
+ if (mode == .push and (std.mem.eql(u8, method, "textDocument/didOpen") or std.mem.eql(u8, method, "textDocument/didChange")))
+ notify(a, "textDocument/publishDiagnostics", "{{\"uri\":\"{s}\",\"diagnostics\":[{{\"range\":{s},\"severity\":1,\"source\":\"clang\",\"message\":\"member reference type 'struct s *' is a pointer; did you mean to use '->'?\"}}]}}", .{ reqUri(params), range(a, 1, 12, 13) });
if (id == null) return; // didOpen/didChange/$/cancelRequest: nothing to say
if (mode == .mute) return; // swallow every request after the handshake
@@ -290,10 +298,17 @@ fn handle(a: std.mem.Allocator, raw: []const u8) void {
} else if (std.mem.eql(u8, method, "textDocument/formatting")) {
reply(a, rid, "[{{\"range\":{s},\"newText\":\"// formatted by mock\\n\"}}]", .{range(a, 0, 0, 0)});
} else if (std.mem.eql(u8, method, "textDocument/codeAction")) {
- // A title alone (a command's), one with its edit (a line put in
- // at the top), and one whose edit is resolved when asked (its
- // `data` the document's uri).
- reply(a, rid, "[{{\"title\":\"mock action: extract\"}},{{\"title\":\"mock action: add a line\",\"edit\":{{\"changes\":{{\"{s}\":[{{\"range\":{s},\"newText\":\"// added by mock\\n\"}}]}}}}}},{{\"title\":\"mock action: rename add to sum\",\"data\":\"{s}\"}}]", .{ uri, range(a, 0, 0, 0), uri });
+ // First a quick fix for each diagnostic the editor passes back, as
+ // clangd offers its fix-its for those alone (`.` to `->` over the
+ // diagnostic's range); then a title alone (a command's), one with
+ // its edit (a line put in at the top), and one whose edit is
+ // resolved when asked (its `data` the document's uri).
+ var fixes: std.ArrayList(u8) = .empty;
+ if (get(get(params, "context"), "diagnostics")) |passed| if (passed == .array) for (passed.array.items) |dg| {
+ const at = std.json.Stringify.valueAlloc(a, get(dg, "range") orelse continue, .{}) catch return;
+ fixes.print(a, "{{\"title\":\"change '.' to '->'\",\"kind\":\"quickfix\",\"edit\":{{\"changes\":{{\"{s}\":[{{\"range\":{s},\"newText\":\"->\"}}]}}}}}},", .{ uri, at }) catch return;
+ };
+ reply(a, rid, "[{s}{{\"title\":\"mock action: extract\"}},{{\"title\":\"mock action: add a line\",\"edit\":{{\"changes\":{{\"{s}\":[{{\"range\":{s},\"newText\":\"// added by mock\\n\"}}]}}}}}},{{\"title\":\"mock action: rename add to sum\",\"data\":\"{s}\"}}]", .{ fixes.items, uri, range(a, 0, 0, 0), uri });
} else if (std.mem.eql(u8, method, "codeAction/resolve")) {
// main.rs's two calls of `add` (L3, L4) renamed.
const doc = str(get(params, "data")) orelse uri;