From 06c9201b3bea985a8d9658a9a95f09b11cb00535 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 19:27:41 -0300 Subject: lsp_client: SPC l a passes the server's own diagnostics that touch the caret back in the request's context, as the protocol has it (CodeActionContext.diagnostics), so a quick fix a server offers for a diagnostic alone comes (VIDEO2's B8, agent V5: on clangd's error at V8's src/torque/types.cc:163:53, `b.ToString()` with `b` a pointer, `SPC l a` answered `CodeAction: not found` even 5 s after landing, where Helix, LazyVim and Doom list `change '.' to '->'` first); THE CAUSE: the codeAction request sent `"context":{"diagnostics":[]}` (dev/lsp/lsp_client.zig:1078), and clangd offers its fix-its only for the diagnostics a client passes back; THE FIX: the request carries, as the server gave them, the diagnostics whose range holds the caret (start <= caret <= end, the server's columns on both sides), read as `]d` reads them: pulled when the server pulls (LSP 3.17), else its last publish, waited for up to diag_ms when a didChange postdates it; that read is one function now (diagnosticsOf, the old `diagnostics` and renderStore's per-file branch folded into it; its values own their strings, the store's copy may go while the mutex is let go); a pulling server costs one textDocument/diagnostic round trip more per SPC l a; no core, dump, digest, frame or hot path touched (a language server's worker); RUN (TEST FIRST): test/lspmock.zig gains LSPMOCK_MODE=push (clangd's way: no diagnosticProvider, one error published at each didOpen and didChange) and offers `change '.' to '->'` first for each diagnostic passed back; behaviour/language_servers.zig "SPC l a passes back the server's own diagnostics at the caret ..." makes the mock C's server in that mode, opens main.c at the `.` of `return b.n;`, and asks `▶ 1 change '.' to '->'` and its digit giving `b->n`: fails on ab9fa40a7bbb (`not on the screen: ▶ 1 change '.' to '->'`, the rows the mock's three), passes now; the existing SPC l a test (a pulling server, the caret off its diagnostic) lists the same three rows; a user hit it on every clangd fix-it (and any server whose quick fixes hang on a diagnostic: rust-analyzer's, gopls's); the real check (clangd 23.1.5, this tree's Debug tui in a private session, .scratch/walkui/CA/demo): see CA's report; GATES (Debug tui): behaviour-test language-servers 14/14, the whole 290/290 (+1), core-test 1172/1347 (175 skipped, as on the base); load 2.5 to 12.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- dev/lsp/lsp_client.zig | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'dev') 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); } -- cgit v1.3