summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dev/lsp/lsp_client.zig42
-rw-r--r--test/behaviour/language_servers.zig22
-rw-r--r--test/lspmock.zig31
3 files changed, 74 insertions, 21 deletions
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig
index d90f655a..10a3a3ce 100644
--- a/dev/lsp/lsp_client.zig
+++ b/dev/lsp/lsp_client.zig
@@ -1051,7 +1051,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
try emitRange(&cx, c.caps.enc, str(get(loc, "uri")) orelse continue, get(loc, "range"), str(get(sym, "name")) orelse "");
}
},
- .diagnostics => try diagnostics(c, io, arena, &cx, uri.items, deadline),
+ .diagnostics => for (try diagnosticsOf(c, io, arena, uri.items, deadline)) |dg| try emitDiag(&cx, c.caps.enc, uri.items, dg, arena),
.workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline),
.rename => {
var b = try atPos(arena, uri.items, pos);
@@ -1075,7 +1075,19 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
},
.code_action => {
var b = try docParams(arena, uri.items);
- try b.print(arena, ",\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch });
+ try b.print(arena, ",\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[", .{ pos.line, pos.ch, pos.line, pos.ch });
+ // The server's own diagnostics that touch the caret, as it gave
+ // them (the protocol's CodeActionContext): a server offers its
+ // quick fixes for those alone (clangd's fix-its).
+ var passed: usize = 0;
+ for (try diagnosticsOf(c, io, arena, uri.items, deadline)) |dg| {
+ const r = rangeOf(get(dg, "range")) orelse continue;
+ if (pos.line < r.sl or pos.line > r.el or (pos.line == r.sl and pos.ch < r.sc) or (pos.line == r.el and pos.ch > r.ec)) continue;
+ if (passed > 0) try b.append(arena, ',');
+ try b.appendSlice(arena, try std.json.Stringify.valueAlloc(arena, dg, .{}));
+ passed += 1;
+ }
+ try b.appendSlice(arena, "]}");
const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return;
// Each action a row, its edit of this file after it as `@put`
// records (the core's picker previews and applies them, Pick.zig);
@@ -1179,15 +1191,16 @@ fn symbolKind(lsp_kind: i64) SymbolKind {
};
}
-/// Pull when the server does (LSP 3.17), the push store otherwise. The store
-/// path waits briefly for a publish that postdates the didChange we just
-/// sent, so `]d` right after an edit sees the new truth, not the old one.
-fn diagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, deadline: i64) Err!void {
+/// The server's diagnostics of `uri`, as it gave them: pulled when it pulls
+/// (LSP 3.17), its last publish otherwise, waited for briefly when the
+/// didChange we just sent postdates it, so `]d` right after an edit sees the
+/// new truth, not the old one. Their strings the arena's: the store's copy
+/// may go while the mutex is let go.
+fn diagnosticsOf(c: *Conn, io: std.Io, arena: std.mem.Allocator, uri: []const u8, deadline: i64) Err![]std.json.Value {
if (c.caps.pull) {
const b = try docParams(arena, uri);
- const result = (try call(c, io, arena, "textDocument/diagnostic", b.items, deadline)) orelse return;
- for (items(get(result, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena);
- return;
+ const result = (try call(c, io, arena, "textDocument/diagnostic", b.items, deadline)) orelse return &.{};
+ return items(get(result, "items"));
}
var stale = true;
for (c.docs.items) |d| if (std.mem.eql(u8, d.uri, uri)) {
@@ -1195,7 +1208,11 @@ fn diagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, uri: []c
break;
};
if (stale) try waitFresh(c, io, uri, nowMs() + diag_ms);
- try renderStore(c, arena, cx, uri);
+ for (c.diags.items) |d| if (std.mem.eql(u8, d.uri, uri)) {
+ const v = std.json.parseFromSliceLeaky(std.json.Value, arena, d.body, .{ .allocate = .alloc_always }) catch return &.{};
+ return items(get(get(v, "params"), "diagnostics"));
+ };
+ return &.{};
}
fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void {
@@ -1207,7 +1224,7 @@ fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx,
}
return;
}
- try renderStore(c, arena, cx, null);
+ try renderStore(c, arena, cx);
}
/// One diagnostic row: `severity: message`, at the diagnostic's own range.
@@ -1223,9 +1240,8 @@ fn emitDiag(cx: *Cx, enc: Enc, uri: []const u8, dg: std.json.Value, arena: std.m
try emitRange(cx, enc, uri, get(dg, "range"), msg);
}
-fn renderStore(c: *Conn, arena: std.mem.Allocator, cx: *Cx, only_uri: ?[]const u8) Err!void {
+fn renderStore(c: *Conn, arena: std.mem.Allocator, cx: *Cx) Err!void {
for (c.diags.items) |d| {
- if (only_uri) |u| if (!std.mem.eql(u8, d.uri, u)) continue;
const v = std.json.parseFromSliceLeaky(std.json.Value, arena, d.body, .{}) catch continue;
for (items(get(get(v, "params"), "diagnostics"))) |dg| try emitDiag(cx, c.caps.enc, d.uri, dg, arena);
}
diff --git a/test/behaviour/language_servers.zig b/test/behaviour/language_servers.zig
index d1b160ee..7f1eef73 100644
--- a/test/behaviour/language_servers.zig
+++ b/test/behaviour/language_servers.zig
@@ -324,6 +324,28 @@ test "language-servers: SPC l a lists the server's code actions in the pane's ta
try s.expect(body, before);
}
+test "language-servers: SPC l a passes back the server's own diagnostics at the caret, as the protocol has it, so a quick fix the server offers for one alone (clangd's `change '.' to '->'`) is listed, first, and its digit applies it" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var s = try Session.initLanguage(std.testing.allocator);
+ defer s.deinit();
+ // C's server the scripted one, as clangd is: its diagnostics pushed,
+ // never pulled.
+ _ = setenv("LSPMOCK_MODE", "push", 1);
+ defer _ = setenv("LSPMOCK_MODE", "", 1);
+ _ = setenv("PARDES_LSP_C", std.c.getenv("PARDES_LSP_RS").?, 1);
+ defer _ = setenv("PARDES_LSP_C", "", 1);
+ const before = "int f(struct s *b) {\n return b.n;\n}\n";
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "main.c", .data = before });
+ var buf: [4096]u8 = undefined;
+ const main = try s.lookFile(s.at("{s}/main.c:2:13", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]}));
+ try s.keys(" la");
+ _ = try s.where("▶ 1 change '.' to '->'");
+ _ = try s.where(" 2 mock action: extract");
+ try s.keys("1");
+ try s.expect(s.at("/pane/{d}/body", .{main}), "int f(struct s *b) {\n return b->n;\n}\n");
+}
+
test "language-servers: a server's Symbols are the Symbols picker's rows: types, functions, fields, then the rest, each its kind's mark and its name in the ones it is in (UX DESIGN N)" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
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;