From aabbccba49772cf73c7e35ae470245e9fda9d6ff Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 9 Nov 2026 02:22:05 -0300 Subject: locality: lsp_client's helpers beside their callers: query's (status with sourceName, indexCommand, nowUs, posOf, fromIndex with emitDefinitions, related and emitOcc), run's (syncDoc, byteCol, hierarchy with hierText), ensure's (rootOf, binOf), byteSpan with strictOffset beside renameEdits, pathOf beside emitAt; the connection block (ensure, handshake, reader) stays together; sam_edit's stepRune before Exec Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/lsp/lsp_client.zig | 2080 ++++++++++++++++++++++++------------------------ src/sam_edit.zig | 12 +- 2 files changed, 1046 insertions(+), 1046 deletions(-) (limited to 'src') diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index 76be6f45..cad75c57 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -411,11 +411,6 @@ fn record(si: usize, req: lsp.Req, us: u64, rows: usize, err: []const u8) void { log_total += 1; } -/// What answered a logged query: a server, or `scip`, the index. -fn sourceName(spec: u8) []const u8 { - return if (spec < specs.len) specs[spec].name else "scip"; -} - fn hideTime() bool { return @import("../Messages.zig").Message.noTime(); } @@ -437,425 +432,827 @@ const Trace = struct { // ------------------------------------------------------------------- query -pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) Err!void { - const req = lsp.explaining(asked); - if (req.kind == .status) return status(io, req.path, out); - if (req.kind == .index) return indexCommand(req, out); - - var tr: Trace = .{ .on = req.kind == .explain }; - // Raw: `alloc` fills a safe build's new memory, all 51 MB of it, per query. - const scratch_buf = (arena.rawAlloc(max_rows * 512, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0 .. max_rows * 512]; - var scratch: std.Io.Writer = .fixed(scratch_buf); +/// What answered a logged query: a server, or `scip`, the index. +fn sourceName(spec: u8) []const u8 { + return if (spec < specs.len) specs[spec].name else "scip"; +} - // The index first: what it answers wants no server. - const t0 = nowUs(); - if (fromIndex(io, arena, req, if (req.kind == .explain) req.explains else req.kind, &scratch, &tr) catch |e| blk: { - tr.note("index: {s}", .{@errorName(e)}); - break :blk false; - }) { - const us = nowUs() -| t0; - const rows = std.mem.count(u8, scratch.buffered(), "\n"); - record(specs.len, req, us, rows, ""); - if (req.kind == .explain) return traceOut(&tr, req, out, rows, us); - return out.writeAll(scratch.buffered()); +fn status(io: std.Io, path: []const u8, out: *std.Io.Writer) !void { + // The index the file would ask first, what it holds and how old it is. + const dir = std.Io.Dir.path.dirname(path) orelse "/"; + if (Scip.find(io, dir)) |found| if (found) |ix| { + var symbols: usize = 0; + for (ix.infos.items) |info| symbols += @intFromBool(info.len > 0); + var newer: usize = 0; + var pbuf: [4096]u8 = undefined; + for (ix.documents) |d| newer += @intFromBool(ix.stale(io, std.fmt.bufPrint(&pbuf, "{s}/{s}", .{ ix.root(), d.path }) catch continue)); + try out.print("scip: {s}, {d} KB, {d} documents, {d} symbols", .{ ix.path(), (ix.size + 999) / 1000, ix.documents.len, symbols }); + if (!hideTime()) { + const secs: std.time.epoch.EpochSeconds = .{ .secs = @intCast(@divTrunc(ix.mtime, std.time.ns_per_s)) }; + const day = secs.getEpochDay().calculateYearDay(); + const md = day.calculateMonthDay(); + try out.print(", indexed {d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} UTC", .{ day.year, md.month.numeric(), md.day_index + 1, secs.getDaySeconds().getHoursIntoDay(), secs.getDaySeconds().getMinutesIntoHour() }); + } + try out.print(", {d} files newer\n", .{newer}); + } else { + try out.print("scip: no index.scip above {s}", .{dir}); + if (specFor(path)) |si| try out.print("; Index runs {s}", .{if (libc.getenv(indexer_envs[si])) |v| std.mem.span(v) else specs[si].indexer}); + try out.writeByte('\n'); + } else |e| try out.print("scip: index.scip above {s}: {s}\n", .{ dir, @errorName(e) }); + try out.print("language servers:\n", .{}); + for (&specs, 0..) |*s, i| { + const c = &conns[i]; + c.mu.lockUncancelable(io); + const state = c.state; + var rootbuf: [256]u8 = undefined; + const rootlen = @min(c.root.len, rootbuf.len); + @memcpy(rootbuf[0..rootlen], c.root[0..rootlen]); + const root = rootbuf[0..rootlen]; + const enc = c.caps.enc; + const ndocs = c.docs.items.len; + const ndiag = c.diags.items.len; + const pull = c.caps.pull; + const chier = c.caps.call_hier; + const thier = c.caps.type_hier; + c.mu.unlock(io); + const env_v = if (libc.getenv(s.env)) |v| std.mem.span(v) else null; + // Never spawned is not off: the first query of its files starts it. + try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) }); + if (state == .ready or state == .starting) { + try out.print(" root {s} {s}", .{ root, @tagName(enc) }); + if (pull) try out.print(" pull-diags", .{}); + if (chier) try out.print(" call-hier", .{}); + if (thier) try out.print(" type-hier", .{}); + try out.print(" docs {d} diag-files {d}", .{ ndocs, ndiag }); + } + if (env_v) |v| try out.print(" [{s}={s}]", .{ s.env, if (v.len == 0) "(disabled)" else v }); + try out.print("\n ", .{}); + for (s.exts) |e| try out.print("{s} ", .{e}); + try out.print("\n", .{}); } - scratch.end = 0; - const si = specFor(req.path) orelse { - tr.note("STOP: no language server spec matches {s}", .{ - if (req.path.len == 0) "a pane with no file" else req.path, - }); - if (req.kind != .explain) return lsp.none(out, "no language server for {s}", .{if (req.path.len == 0) "a pane with no file" else std.Io.Dir.path.basename(req.path)}); - return traceOut(&tr, req, out, 0, 0); - }; - tr.note("file {s} -> {s} (languageId {s})", .{ std.Io.Dir.path.basename(req.path), specs[si].name, specs[si].lang }); - const t1 = nowUs(); - var err_name: []const u8 = ""; - var failure: ?Err = null; - answer(io, arena, si, req, &scratch, &tr) catch |e| { - failure = e; - err_name = @errorName(e); - tr.note("ERROR: {s}", .{err_name}); - }; - const us = nowUs() -| t1; - const rows = std.mem.count(u8, scratch.buffered(), "\n"); - record(si, req, us, rows, err_name); + while (!log_mu.tryLock()) std.atomic.spinLoopHint(); + defer log_mu.unlock(); + try out.print("\nclient queries ({d} total, keeping {d}):\n", .{ log_total, log_cap }); + var shown: usize = 0; + for (0..log_cap) |i| { + const e = &log_buf[(log_next + i) % log_cap]; + if (!e.used) continue; + shown += 1; + if (hideTime()) { + try out.print(" {s:<18} {s:<14} {s:<20} {d:>4} row(s){s}{s}\n", .{ + @tagName(e.kind), sourceName(e.spec), e.file[0..e.file_len], e.rows, + if (e.err_len > 0) " ERROR " else "", e.err[0..e.err_len], + }); + } else { + try out.print(" {s:<18} {s:<14} {s:<20} {d:>7}us {d:>4} row(s){s}{s}\n", .{ + @tagName(e.kind), sourceName(e.spec), e.file[0..e.file_len], e.us, e.rows, + if (e.err_len > 0) " ERROR " else "", e.err[0..e.err_len], + }); + } + } + if (shown == 0) try out.print(" (none yet — press gd in a file with a server, then ask again)\n", .{}); +} - if (req.kind == .explain) return traceOut(&tr, req, out, rows, us); - // No server to start for the language: said, not "did not answer". - if (failure) |err| if (err == error.NoServer) return lsp.none(out, "language server for {s} not found", .{std.Io.Dir.path.extension(req.path)}); - // Installed, but it failed a moment ago: said so, and when it is tried - // again, not that it is not there. - if (failure) |err| if (err == error.Backoff) { - const c = &conns[si]; - c.mu.lockUncancelable(io); - const left_ms = c.retry_after_ms -| nowMs(); - c.mu.unlock(io); - return lsp.none(out, "{s} failed recently; retry in {d}s", .{ specs[si].name, @divFloor(left_ms + 999, 1000) }); - }; - if (failure) |err| return err; - try out.writeAll(scratch.buffered()); +/// The Index word's command line: the language's indexer, at the +/// project's root, where the index it writes answers for every file under +/// it (`PARDES_SCIP_RS` and the like name another indexer). +fn indexCommand(req: lsp.Req, out: *std.Io.Writer) !void { + const si = specFor(req.path) orelse return lsp.none(out, "no indexer for {s}", .{std.Io.Dir.path.basename(req.path)}); + const cmd = if (libc.getenv(indexer_envs[si])) |v| std.mem.span(v) else specs[si].indexer; + try out.writeAll("@run cd '"); + for (rootOf(std.Io.Dir.path.dirname(req.path) orelse "/", specs[si].markers)) |ch| try if (ch == '\'') out.writeAll("'\\''") else out.writeByte(ch); + try out.print("' && {s}\n", .{cmd}); } -fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us: u64) std.Io.Writer.Error!void { - if (req.kind != .explain) return; - try out.print("lsp explain — the {s} query at byte {d} of {s}\n\n", .{ - @tagName(req.explains), req.offset, if (req.path.len == 0) "(no file)" else std.Io.Dir.path.basename(req.path), - }); - try out.writeAll(tr.buf[0..tr.len]); - if (hideTime()) - try out.print("\n{d} row(s)\n", .{rows}) - else - try out.print("\n{d} row(s) in {d}us\n", .{ rows, us }); +fn nowUs() u64 { + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) *| 1_000_000 +| @as(u64, @intCast(ts.nsec)) / 1000; } -fn answer(io: std.Io, arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { - const c = &conns[si]; - try c.mu.lock(io); - defer c.mu.unlock(io); - const g = try ensure(c, io, si, arena, req, tr); - const before = out.end; - try attempt(c, io, si, arena, req, out, tr, g); - // A server still loading its workspace answers at once, with nothing - // (rust-analyzer before its index): that is no answer. Wait until it says - // it is loaded, the message row saying so, and ask again. - const answered = out.buffered()[before..]; - if (c.loading and c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) { - tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name}); - post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name}); - while (c.loading and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500); - out.end = before; - try attempt(c, io, si, arena, req, out, tr, g); +/// Byte offset -> LSP position. `lsp.lineCol` gives the line and the BYTE +/// column; only the column needs re-counting, and only when the server +/// refused utf-8. +fn posOf(src: []const u8, off: u32, enc: Enc) Pos { + const lc = lsp.lineCol(src, off); + if (enc == .utf8) return .{ .line = @intCast(lc.line), .ch = @intCast(lc.col) }; + const bol = @min(off, src.len) - lc.col; + var units: u32 = 0; + var i: usize = bol; + while (i < bol + lc.col) { + const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1; + units += if (l == 4 and enc == .utf16) 2 else 1; + i += l; } + return .{ .line = @intCast(lc.line), .ch = units }; } -/// One run of a query, and what its failure says of the server. -fn attempt(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace, g: u32) Err!void { - run(c, io, si, arena, req, out, tr) catch |e| { - switch (e) { - // transport-level: this server is gone; forget it, and the next - // query respawns. Gen-checked so a respawn that happened while we - // waited is not the one we kill. - error.Dead, error.Protocol => shutdownIf(c, io, g), - error.Timeout => { - // Slow is only wedged when the server is IDLE. One with - // active `$/progress` work (rust-analyzer running cargo - // check over a thousand crates) is demonstrably alive, is - // narrating its own excuse on the message row, and killing - // it would throw the index away right before it pays off. - if (c.progress > 0) { - c.timeouts = 0; - } else { - c.timeouts +|= 1; - if (c.timeouts >= wedged_strikes and c.gen == g) { - post(si, .always, "{s} not answering — restarting", .{specs[si].name}); - shutdownIf(c, io, g); - } - } - }, - // Canceled: the shell asked something newer, not a wedged server. - error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed, error.Canceled => {}, - } - return e; - }; - c.timeouts = 0; +fn emitOcc(cx: *Cx, ix: *const Scip.Index, o: u32, note: []const u8) Err!void { + const occ = ix.occurrences[o]; + try emitPath(cx, ix.documents[occ.doc].enc, try indexPath(cx.arena, ix, occ.doc), occ.range, note); } -fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { - // `explain` narrates the last query asked (a definition, `gd`, when - // none was) — dispatch below on the effective kind so the trace - // follows the code that query really runs. - const kind: lsp.Kind = if (req.kind == .explain) req.explains else req.kind; - - // Both arg-taking kinds are useless without one, and an empty - // workspace/symbol query means "every symbol in the project". - if ((kind == .rename or kind == .workspace_symbols) and req.arg.len == 0) return; - - var uri: std.ArrayList(u8) = .empty; - try uriOf(&uri, arena, req.path); - try syncDoc(c, io, si, arena, uri.items, req.source); - // Every other open file this server answers for, before a query whose - // answer reaches them: zls renames only in the documents it has. - if (lsp.reachesOtherFiles(kind)) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path)) { - var other: std.ArrayList(u8) = .empty; - try uriOf(&other, arena, doc.path); - try syncDoc(c, io, si, arena, other.items, doc.source); - tr.note("synced {s} first", .{std.Io.Dir.path.basename(doc.path)}); +/// A symbol's definitions, as rows: named by the symbol, as a type +/// hierarchy's are, or by their source lines. +fn emitDefinitions(cx: *Cx, ix: *Scip.Index, symbol: u32, named: bool) Err!void { + for (ix.of(symbol)) |o| if (ix.occurrences[o].roles & Scip.role_definition != 0) { + const sig = if (Scip.field(ix.infos.items[symbol], 7)) |f| (Scip.field(f.bytes, 5) orelse Scip.Field{ .num = 0 }).bytes else ""; + const note = if (!named) "" else if (sig.len == 0) displayName(ix, symbol) else try std.fmt.allocPrint(cx.arena, "{s} {s}", .{ displayName(ix, symbol), flat(cx.arena, sig) }); + try emitOcc(cx, ix, o, note); }; +} - const pos = posOf(req.source, req.offset, c.caps.enc); - var cx: Cx = .{ .io = io, .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = req.kind == .callstack }; - for (req.others) |doc| try cx.files.append(arena, .{ .path = doc.path, .text = doc.source }); - tr.note("server {s} (pid {d}), root {s}, {s} columns", .{ - @tagName(c.state), c.pid, c.root, @tagName(c.caps.enc), - }); - const deadline = nowMs() + req_ms; +/// The symbols `symbol`'s relationships name with flag `flag` set +/// (scip.proto Relationship: 2 is_reference, 3 is_implementation, 4 +/// is_type_definition), the ones the index knows. +fn related(a: std.mem.Allocator, ix: *const Scip.Index, symbol: u32, flag: u64) Err![]u32 { + var out: std.ArrayList(u32) = .empty; + var r: Scip.Reader = .{ .bytes = ix.infos.items[symbol] }; + while (r.next() catch null) |f| if (f.num == 4) { + const to = Scip.field(f.bytes, 1) orelse continue; + if ((Scip.field(f.bytes, flag) orelse continue).int == 0) continue; + if (ix.ids.get(to.bytes)) |id| try out.append(a, id); + }; + return out.items; +} +/// The index's answer (Scip.zig), in the rows the server's would make: true +/// when it had one, false to ask the server: no index above the file, a +/// file it does not hold, unsaved changes it has not seen, nothing at the +/// cursor, a symbol it does not know, or a word only a server answers. +fn fromIndex(io: std.Io, arena: std.mem.Allocator, req: lsp.Req, kind: lsp.Kind, out: *std.Io.Writer, tr: *Trace) Err!bool { switch (kind) { - .definition, .declaration, .type_definition, .implementation, .references => { - const method = switch (kind) { - .definition => "textDocument/definition", - .declaration => "textDocument/declaration", - .type_definition => "textDocument/typeDefinition", - .implementation => "textDocument/implementation", - else => "textDocument/references", - }; - var b = try atPos(arena, uri.items, pos); - if (kind == .references) try b.appendSlice(arena, ",\"context\":{\"includeDeclaration\":true}"); - tr.note("-> {s} @ {d}:{d}", .{ method, pos.line + 1, pos.ch + 1 }); - const result = (try call(c, io, arena, method, b.items, deadline)) orelse { - tr.note("<- empty (no result, an error response, or null)", .{}); - return; - }; - try locations(&cx, c.caps.enc, result); - tr.note("<- {d} row(s)", .{cx.rows}); + .definition, .type_definition, .implementation, .references, .select_refs, .hover, .document_symbols, .workspace_symbols, .incoming_calls, .outgoing_calls, .callstack, .supertypes, .subtypes => {}, + else => return false, + } + if (req.dirty) { + tr.note("unsaved changes the index has not seen: the server", .{}); + return false; + } + const dir = std.Io.Dir.path.dirname(req.path) orelse return false; + const ix = (Scip.find(io, dir) catch |e| { + tr.note("index.scip above {s}: {s}", .{ dir, @errorName(e) }); + return false; + }) orelse { + tr.note("no index.scip above {s}", .{dir}); + return false; + }; + const doc = ix.document(req.path) orelse { + tr.note("{s} holds no {s}", .{ ix.path(), std.Io.Dir.path.basename(req.path) }); + return false; + }; + const si = specFor(req.path) orelse 0; + if (ix.stale(io, req.path)) { + tr.note("stale: {s} is newer than the index", .{std.Io.Dir.path.basename(req.path)}); + if (!ix.stale_said) post(si, .always, "{s} is older than {s}: Index makes it again", .{ ix.path(), std.Io.Dir.path.basename(req.path) }); + ix.stale_said = true; + } + const enc = ix.documents[doc].enc; + var cx: Cx = .{ .io = io, .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = kind == .callstack }; + for (req.others) |d| try cx.files.append(arena, .{ .path = d.path, .text = d.source }); + if (kind == .document_symbols or kind == .workspace_symbols) { + for (ix.occurrences, 0..) |o, i| { + if (kind == .document_symbols and o.doc != doc) continue; + // A name, never a local or a whole file's module. + if (o.roles & Scip.role_definition == 0 or o.range.sl != o.range.el or std.mem.startsWith(u8, ix.symbols.items[o.symbol], "local ")) continue; + const name = displayName(ix, o.symbol); + if (kind == .workspace_symbols and std.ascii.findIgnoreCase(name, req.arg) == null) continue; + try emitOcc(&cx, ix, @intCast(i), name); + } + tr.note("<- {d} row(s) from the index", .{cx.rows}); + return cx.rows > 0; + } + const pos = posOf(req.source, req.offset, enc); + const at = ix.at(doc, pos.line, pos.ch) orelse { + tr.note("no occurrence at {d}:{d} in {s}", .{ pos.line + 1, pos.ch + 1, ix.path() }); + return false; + }; + const sym = ix.occurrences[at].symbol; + tr.note("{s}: occurrence '{s}' at {d}:{d}", .{ ix.path(), ix.symbols.items[sym], pos.line + 1, pos.ch + 1 }); + switch (kind) { + .definition => try emitDefinitions(&cx, ix, sym, false), + .type_definition => for (try related(arena, ix, sym, 4)) |to| try emitDefinitions(&cx, ix, to, false), + .supertypes => for (try related(arena, ix, sym, 3)) |to| try emitDefinitions(&cx, ix, to, true), + .implementation, .subtypes => for (ix.infos.items, 0..) |_, from| { + if (std.mem.indexOfScalar(u32, try related(arena, ix, @intCast(from), 3), sym) != null) + try emitDefinitions(&cx, ix, @intCast(from), kind == .subtypes); }, - .select_refs => { - const b = try atPos(arena, uri.items, pos); - const result = (try call(c, io, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return; - for (items(result)) |h| try emitRange(&cx, c.caps.enc, uri.items, get(h, "range"), ""); + .references, .select_refs => { + var also = try related(arena, ix, sym, 2); + also = try std.mem.concat(arena, u32, &.{ &.{sym}, also }); + for (also) |of| for (ix.of(of)) |o| if (kind == .references or ix.occurrences[o].doc == doc) try emitOcc(&cx, ix, o, ""); }, .hover => { - const b = try atPos(arena, uri.items, pos); - const result = (try call(c, io, arena, "textDocument/hover", b.items, deadline)) orelse return; - const contents = get(result, "contents") orelse return; - const text = str(contents) orelse str(get(contents, "value")) orelse blk: { - for (items(contents)) |m| if (str(m) orelse str(get(m, "value"))) |t| break :blk t; - break :blk ""; + const info = ix.infos.items[sym]; + var said = false; + if (Scip.field(info, 7)) |sig| if (Scip.field(sig.bytes, 5)) |text| { + try out.writeAll(std.mem.trim(u8, text.bytes, " \t\r\n")); + said = true; }; - if (text.len == 0) return; - try out.writeAll(std.mem.trim(u8, text, " \t\r\n")); - try out.writeByte('\n'); - }, - .document_symbols => { - const b = try docParams(arena, uri.items); - const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return; - try walkSymbols(&cx, c.caps.enc, uri.items, result, 0); - }, - .workspace_symbols => { - var b: std.ArrayList(u8) = .empty; - try b.appendSlice(arena, "\"query\":"); - try jstr(&b, arena, req.arg); - const result = (try call(c, io, arena, "workspace/symbol", b.items, deadline)) orelse return; - for (items(result)) |sym| { - const loc = get(sym, "location") orelse continue; - 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), - .workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline), - .rename => { - var b = try atPos(arena, uri.items, pos); - try b.appendSlice(arena, ",\"newName\":"); - try jstr(&b, arena, req.arg); - const result = (try call(c, io, arena, "textDocument/rename", b.items, deadline)) orelse return; - // Other open files that say the old name, should the server - // answer for this file alone (zls renaming at a declaration does, - // whatever it was told): applied here, they would no longer build. - var also: std.ArrayList([]const u8) = .empty; - const old = identAt(req.source, req.offset); - if (old.len > 0) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path) and holdsWord(doc.source, old)) - try also.append(arena, doc.path); - try renameEdits(&cx, c.caps.enc, uri.items, result, req.source, old, also.items); - }, - .format => { - var b = try docParams(arena, uri.items); - try b.appendSlice(arena, ",\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); - const result = (try call(c, io, arena, "textDocument/formatting", b.items, deadline)) orelse return; - try formatEdits(&cx, c.caps.enc, result, req.source); + var r: Scip.Reader = .{ .bytes = info }; + while (r.next() catch null) |f| if (f.num == 3) { + if (said) try out.writeAll("\n\n"); + try out.writeAll(std.mem.trim(u8, f.bytes, " \t\r\n")); + said = true; + }; + if (said) try out.writeByte('\n') else tr.note("no documentation for it in the index", .{}); + return said; }, - .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 }); - const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return; - for (items(result)) |ca| { - const title = str(get(ca, "title")) orelse continue; - try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title)); - cx.rows += 1; + .incoming_calls => for (try indexHops(arena, ix, sym)) |hop| for (hop.sites.items) |site| + try emitPath(&cx, ix.documents[hop.doc].enc, try indexPath(arena, ix, hop.doc), site, hop.call.name), + .outgoing_calls => for (ix.of(sym)) |d| { + // The calls inside its definition, each function once, at its + // own definition. + const def = ix.occurrences[d]; + if (def.roles & Scip.role_definition == 0) continue; + var seen: std.ArrayList(u32) = .empty; + const in = ix.documents[def.doc]; + for (ix.occurrences[in.first..][0..in.len]) |o| { + if (o.roles & Scip.role_definition != 0 or !Scip.holds(def.enclosing, o.range) or !Scip.callable(ix.infos.items[o.symbol])) continue; + if (std.mem.indexOfScalar(u32, seen.items, o.symbol) != null) continue; + try seen.append(arena, o.symbol); + try emitDefinitions(&cx, ix, o.symbol, true); } + break; }, - .completion => { - var b = try atPos(arena, uri.items, pos); - try b.appendSlice(arena, ",\"context\":{\"triggerKind\":1}"); - const result = (try call(c, io, arena, "textDocument/completion", b.items, deadline)) orelse return; - const list = if (get(result, "items")) |it| items(it) else items(result); - const here = lsp.rel(cx.base, cx.cur_path); - var n: usize = 0; - for (list) |item| { - if (n >= 100) break; - const label = str(get(item, "label")) orelse continue; - const detail = str(get(item, "detail")) orelse ""; - var text: std.ArrayList(u8) = .empty; - try text.appendSlice(arena, label); - if (detail.len > 0) { - try text.appendSlice(arena, " "); - try text.appendSlice(arena, flat(arena, detail)); - } - // Every row carries the ASKING position: a completion item has - // no location of its own, so the honest place is where it would - // be inserted. n/N still step the list; Enter goes nowhere new. - try lsp.row(cx.out, here, pos.line, byteCol(req.source, pos.line, pos.ch, c.caps.enc), text.items); - n += 1; - } + .callstack => { + const def = for (ix.of(sym)) |o| { + if (ix.occurrences[o].roles & Scip.role_definition != 0) break ix.occurrences[o]; + } else { + tr.note("no definition of it in the index", .{}); + return false; + }; + // The rows come from the tree alone: the core reads the sources. + cx.no_text = true; + const focus: CallTree.Call = .{ .uri = try indexUri(arena, ix, def.doc), .name = "", .at = def.range }; + try callTree(.{ .index = ix }, si, &cx, enc, focus, try indexHops(arena, ix, sym), tr, req.callstack_depth, req.budget); + return true; }, - .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget), - .incoming_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .incoming, tr), - .outgoing_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr), - .supertypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .supers, tr), - .subtypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .subs, tr), - .status, .explain, .index => unreachable, + else => unreachable, } + tr.note("<- {d} row(s) from the index", .{cx.rows}); + return cx.rows > 0; } -// -------------------------------------------------------- kind sub-handlers +pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) Err!void { + const req = lsp.explaining(asked); + if (req.kind == .status) return status(io, req.path, out); + if (req.kind == .index) return indexCommand(req, out); -/// Goto/references result shapes: bare Location, Location[], LocationLink[]. -fn locations(cx: *Cx, enc: Enc, result: std.json.Value) Err!void { - if (result == .object) { - try emitRange(cx, enc, str(get(result, "uri")) orelse return, get(result, "range"), ""); - return; - } - for (items(result)) |loc| { - if (get(loc, "targetUri")) |tu| { - const r = get(loc, "targetSelectionRange") orelse get(loc, "targetRange"); - try emitRange(cx, enc, str(tu) orelse continue, r, ""); - } else { - try emitRange(cx, enc, str(get(loc, "uri")) orelse continue, get(loc, "range"), ""); - } - } -} + var tr: Trace = .{ .on = req.kind == .explain }; + // Raw: `alloc` fills a safe build's new memory, all 51 MB of it, per query. + const scratch_buf = (arena.rawAlloc(max_rows * 512, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0 .. max_rows * 512]; + var scratch: std.Io.Writer = .fixed(scratch_buf); -/// DocumentSymbol[] nests (`children`), SymbolInformation[] is flat. -fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth: u8) Err!void { - if (depth > 8) return; - for (items(node)) |sym| { - const name = str(get(sym, "name")) orelse continue; - if (get(get(sym, "location"), "range")) |r| { - try emitRange(cx, enc, str(get(get(sym, "location"), "uri")) orelse uri, r, name); - } else { - try emitRange(cx, enc, uri, get(sym, "selectionRange") orelse get(sym, "range"), name); - } - if (get(sym, "children")) |kids| try walkSymbols(cx, enc, uri, kids, depth + 1); + // The index first: what it answers wants no server. + const t0 = nowUs(); + if (fromIndex(io, arena, req, if (req.kind == .explain) req.explains else req.kind, &scratch, &tr) catch |e| blk: { + tr.note("index: {s}", .{@errorName(e)}); + break :blk false; + }) { + const us = nowUs() -| t0; + const rows = std.mem.count(u8, scratch.buffered(), "\n"); + record(specs.len, req, us, rows, ""); + if (req.kind == .explain) return traceOut(&tr, req, out, rows, us); + return out.writeAll(scratch.buffered()); } -} + scratch.end = 0; + const si = specFor(req.path) orelse { + tr.note("STOP: no language server spec matches {s}", .{ + if (req.path.len == 0) "a pane with no file" else req.path, + }); + if (req.kind != .explain) return lsp.none(out, "no language server for {s}", .{if (req.path.len == 0) "a pane with no file" else std.Io.Dir.path.basename(req.path)}); + return traceOut(&tr, req, out, 0, 0); + }; + tr.note("file {s} -> {s} (languageId {s})", .{ std.Io.Dir.path.basename(req.path), specs[si].name, specs[si].lang }); -/// 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 { - 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; - } - var stale = true; - for (c.docs.items) |d| if (std.mem.eql(u8, d.uri, uri)) { - stale = d.stale; - break; + const t1 = nowUs(); + var err_name: []const u8 = ""; + var failure: ?Err = null; + answer(io, arena, si, req, &scratch, &tr) catch |e| { + failure = e; + err_name = @errorName(e); + tr.note("ERROR: {s}", .{err_name}); }; - if (stale) try waitFresh(c, io, uri, nowMs() + diag_ms); - try renderStore(c, arena, cx, uri); + const us = nowUs() -| t1; + const rows = std.mem.count(u8, scratch.buffered(), "\n"); + record(si, req, us, rows, err_name); + + if (req.kind == .explain) return traceOut(&tr, req, out, rows, us); + // No server to start for the language: said, not "did not answer". + if (failure) |err| if (err == error.NoServer) return lsp.none(out, "language server for {s} not found", .{std.Io.Dir.path.extension(req.path)}); + // Installed, but it failed a moment ago: said so, and when it is tried + // again, not that it is not there. + if (failure) |err| if (err == error.Backoff) { + const c = &conns[si]; + c.mu.lockUncancelable(io); + const left_ms = c.retry_after_ms -| nowMs(); + c.mu.unlock(io); + return lsp.none(out, "{s} failed recently; retry in {d}s", .{ specs[si].name, @divFloor(left_ms + 999, 1000) }); + }; + if (failure) |err| return err; + try out.writeAll(scratch.buffered()); } -fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void { - if (c.caps.pull_workspace) { - const result = (try call(c, io, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return; - for (items(get(result, "items"))) |per| { - const uri = str(get(per, "uri")) orelse continue; - for (items(get(per, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena); - } - return; +fn traceOut(tr: *const Trace, req: lsp.Req, out: *std.Io.Writer, rows: usize, us: u64) std.Io.Writer.Error!void { + if (req.kind != .explain) return; + try out.print("lsp explain — the {s} query at byte {d} of {s}\n\n", .{ + @tagName(req.explains), req.offset, if (req.path.len == 0) "(no file)" else std.Io.Dir.path.basename(req.path), + }); + try out.writeAll(tr.buf[0..tr.len]); + if (hideTime()) + try out.print("\n{d} row(s)\n", .{rows}) + else + try out.print("\n{d} row(s) in {d}us\n", .{ rows, us }); +} + +fn answer(io: std.Io, arena: std.mem.Allocator, si: usize, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { + const c = &conns[si]; + try c.mu.lock(io); + defer c.mu.unlock(io); + const g = try ensure(c, io, si, arena, req, tr); + const before = out.end; + try attempt(c, io, si, arena, req, out, tr, g); + // A server still loading its workspace answers at once, with nothing + // (rust-analyzer before its index): that is no answer. Wait until it says + // it is loaded, the message row saying so, and ask again. + const answered = out.buffered()[before..]; + if (c.loading and c.gen == g and (answered.len == 0 or std.mem.startsWith(u8, answered, "@none "))) { + tr.note("{s} is still loading: waiting for it, then asking again", .{specs[si].name}); + post(si, .always, "{s} is still loading the workspace; the answer comes when it is done\u{2026}", .{specs[si].name}); + while (c.loading and c.gen == g and c.alive()) _ = try timedWait(c, io, nowMs() + 500); + out.end = before; + try attempt(c, io, si, arena, req, out, tr, g); } - try renderStore(c, arena, cx, null); } -/// One diagnostic row: `severity: message`, at the diagnostic's own range. -fn emitDiag(cx: *Cx, enc: Enc, uri: []const u8, dg: std.json.Value, arena: std.mem.Allocator) Err!void { - const sev = num(get(dg, "severity")) orelse 1; - const label: []const u8 = switch (sev) { - 1 => "error", - 2 => "warning", - 3 => "info", - else => "hint", +/// One run of a query, and what its failure says of the server. +fn attempt(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace, g: u32) Err!void { + run(c, io, si, arena, req, out, tr) catch |e| { + switch (e) { + // transport-level: this server is gone; forget it, and the next + // query respawns. Gen-checked so a respawn that happened while we + // waited is not the one we kill. + error.Dead, error.Protocol => shutdownIf(c, io, g), + error.Timeout => { + // Slow is only wedged when the server is IDLE. One with + // active `$/progress` work (rust-analyzer running cargo + // check over a thousand crates) is demonstrably alive, is + // narrating its own excuse on the message row, and killing + // it would throw the index away right before it pays off. + if (c.progress > 0) { + c.timeouts = 0; + } else { + c.timeouts +|= 1; + if (c.timeouts >= wedged_strikes and c.gen == g) { + post(si, .always, "{s} not answering — restarting", .{specs[si].name}); + shutdownIf(c, io, g); + } + } + }, + // Canceled: the shell asked something newer, not a wedged server. + error.OutOfMemory, error.NoServer, error.Backoff, error.WriteFailed, error.Canceled => {}, + } + return e; }; - const msg = try std.fmt.allocPrint(arena, "{s}: {s}", .{ label, flat(arena, str(get(dg, "message")) orelse "") }); - try emitRange(cx, enc, uri, get(dg, "range"), msg); + c.timeouts = 0; } -fn renderStore(c: *Conn, arena: std.mem.Allocator, cx: *Cx, only_uri: ?[]const u8) 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); - } -} +/// didOpen the first time a file is seen, didChange when its bytes moved, +/// nothing when they did not — the common case between two presses of gd. +fn syncDoc(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void { + const hash = std.hash.Wyhash.hash(0, src); + var doc: ?*Doc = null; + for (c.docs.items) |*d| if (std.mem.eql(u8, d.uri, uri)) { + doc = d; + break; + }; + if (doc) |d| if (d.hash == hash) return; -/// Block (mutex released) until the reader marks `uri` fresh or the deadline -/// passes. The reader broadcasts on every publishDiagnostics. -fn waitFresh(c: *Conn, io: std.Io, uri: []const u8, deadline: i64) error{Canceled}!void { - const g = c.gen; - while (c.gen == g and c.alive()) { - var fresh = false; - for (c.docs.items) |d| if (std.mem.eql(u8, d.uri, uri)) { - fresh = !d.stale; - break; + var b: std.ArrayList(u8) = .empty; + if (doc) |d| { + d.version += 1; + d.hash = hash; + d.stale = true; + try b.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didChange\",\"params\":{{\"textDocument\":{{\"uri\":", .{}); + try jstr(&b, arena, uri); + try b.print(arena, ",\"version\":{d}}},\"contentChanges\":[{{\"text\":", .{d.version}); + try jstr(&b, arena, src); + try b.appendSlice(arena, "}]}}"); + } else { + if (c.docs.items.len >= 256) return; // a session does not open this many + const owned = try sa.dupe(u8, uri); + c.docs.append(sa, .{ .uri = owned, .version = 1, .hash = hash }) catch { + sa.free(owned); + return error.OutOfMemory; }; - if (fresh) return; - if (!try timedWait(c, io, deadline)) return; + try b.appendSlice(arena, "{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didOpen\",\"params\":{\"textDocument\":{\"uri\":"); + try jstr(&b, arena, uri); + try b.print(arena, ",\"languageId\":\"{s}\",\"version\":1,\"text\":", .{specs[si].lang}); + try jstr(&b, arena, src); + try b.appendSlice(arena, "}}}"); } + // A server busy with what it was sent last reads no more meanwhile + // (clangd parses its compile database at a first open): this waits as + // an answer does, for a cancel or a server that has stopped. + try send(c, io, b.items, nowMs() + wedge_ms); } -/// The identifier at byte `at` of `src`: the word a rename there names. -fn identAt(src: []const u8, at: usize) []const u8 { - const isId = struct { - fn f(ch: u8) bool { - return std.ascii.isAlphanumeric(ch) or ch == '_'; - } - }.f; - var start = @min(at, src.len); - while (start > 0 and isId(src[start - 1])) start -= 1; - var end = @min(at, src.len); - while (end < src.len and isId(src[end])) end += 1; - return src[start..end]; +/// The current-buffer byte column for a (line, ch) position — used only for +/// rows that point at the asking position itself. +fn byteCol(src: []const u8, line: u32, ch: u32, enc: Enc) usize { + var bol: usize = 0; + var n: u32 = 0; + while (n < line) : (n += 1) + bol = (std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse return ch) + 1; + const eol = std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse src.len; + return colBytes(src[bol..eol], ch, enc); } -/// Whether `text` holds `word` as a whole identifier. -fn holdsWord(text: []const u8, word: []const u8) bool { - var from: usize = 0; - while (std.mem.indexOfPos(u8, text, from, word)) |i| : (from = i + 1) { - const before = i == 0 or !(std.ascii.isAlphanumeric(text[i - 1]) or text[i - 1] == '_'); - const end = i + word.len; - const after = end == text.len or !(std.ascii.isAlphanumeric(text[end]) or text[end] == '_'); - if (before and after) return true; - } - return false; +fn hierText(arena: std.mem.Allocator, item: std.json.Value) []const u8 { + const name = str(get(item, "name")) orelse ""; + const detail = str(get(item, "detail")) orelse return name; + return std.fmt.allocPrint(arena, "{s} {s}", .{ name, flat(arena, detail) }) catch name; } -/// A WorkspaceEdit that stays inside the asked-about file becomes `@put` -/// records the core applies as one undo transaction; anything wider (a real -/// multi-file rename, file creates/renames) becomes a PREVIEW — one location -/// row per would-be edit, in the same buffer `gr` fills, because silently -/// applying a fraction of a workspace rename would be worse than either. -fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, src: []const u8, old: []const u8, also: []const []const u8) Err!void { - var edits: std.ArrayList(PutEdit) = .empty; - var foreign = false; - - // (uri, edits) per file, from either shape of a WorkspaceEdit - var files: std.ArrayList(struct { []const u8, ?std.json.Value }) = .empty; - if (get(result, "documentChanges")) |dcs| { - for (items(dcs)) |dc| { - if (get(dc, "kind") != null) { - foreign = true; // create/rename/delete file operations - continue; - } - const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; - try files.append(cx.arena, .{ u, get(dc, "edits") }); - } - } else if (get(result, "changes")) |ch| if (ch == .object) { - var it = ch.object.iterator(); - while (it.next()) |e| try files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* }); - }; +/// The two-step hierarchy kinds: prepare at the cursor, then follow every +/// item the server returned (usually one). Gated on the server capability so +/// an old server costs zero round trips. +fn hierarchy(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void { + const call_side = h == .incoming or h == .outgoing; + if (call_side and !c.caps.call_hier) { + tr.note("STOP: {s} does not advertise callHierarchyProvider", .{specs[si].name}); + return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name}); + } + if (!call_side and !c.caps.type_hier) { + tr.note("STOP: {s} does not advertise typeHierarchyProvider", .{specs[si].name}); + return lsp.none(cx.out, "{s} does not answer type hierarchy (no typeHierarchyProvider)", .{specs[si].name}); + } + const prepare: []const u8 = if (call_side) "textDocument/prepareCallHierarchy" else "textDocument/prepareTypeHierarchy"; + const follow: []const u8 = switch (h) { + .incoming => "callHierarchy/incomingCalls", + .outgoing => "callHierarchy/outgoingCalls", + .supers => "typeHierarchy/supertypes", + .subs => "typeHierarchy/subtypes", + }; + const b = try atPos(arena, uri, pos); + const prepared = (try call(c, io, arena, prepare, b.items, deadline)) orelse return; + for (items(prepared)) |item| { + const result = (try hierarchyResult(c, io, arena, item, follow, deadline)) orelse return; + for (items(result)) |entry| switch (h) { + .incoming => { + // one row per CALL SITE, under the caller's name — that is + // what n/N want to walk + const from = get(entry, "from") orelse continue; + const fu = str(get(from, "uri")) orelse continue; + const name = str(get(from, "name")) orelse ""; + const ranges = items(get(entry, "fromRanges")); + if (ranges.len == 0) { + try emitRange(cx, c.caps.enc, fu, get(from, "selectionRange"), name); + } else for (ranges) |r| try emitRange(cx, c.caps.enc, fu, r, name); + }, + .outgoing => { + const to = get(entry, "to") orelse continue; + try emitRange(cx, c.caps.enc, str(get(to, "uri")) orelse continue, get(to, "selectionRange") orelse get(to, "range"), hierText(cx.arena, to)); + }, + .supers, .subs => try emitRange(cx, c.caps.enc, str(get(entry, "uri")) orelse continue, get(entry, "selectionRange") orelse get(entry, "range"), hierText(cx.arena, entry)), + }; + } +} + +fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io.Writer, tr: *Trace) Err!void { + // `explain` narrates the last query asked (a definition, `gd`, when + // none was) — dispatch below on the effective kind so the trace + // follows the code that query really runs. + const kind: lsp.Kind = if (req.kind == .explain) req.explains else req.kind; + + // Both arg-taking kinds are useless without one, and an empty + // workspace/symbol query means "every symbol in the project". + if ((kind == .rename or kind == .workspace_symbols) and req.arg.len == 0) return; + + var uri: std.ArrayList(u8) = .empty; + try uriOf(&uri, arena, req.path); + try syncDoc(c, io, si, arena, uri.items, req.source); + // Every other open file this server answers for, before a query whose + // answer reaches them: zls renames only in the documents it has. + if (lsp.reachesOtherFiles(kind)) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path)) { + var other: std.ArrayList(u8) = .empty; + try uriOf(&other, arena, doc.path); + try syncDoc(c, io, si, arena, other.items, doc.source); + tr.note("synced {s} first", .{std.Io.Dir.path.basename(doc.path)}); + }; + + const pos = posOf(req.source, req.offset, c.caps.enc); + var cx: Cx = .{ .io = io, .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = req.kind == .callstack }; + for (req.others) |doc| try cx.files.append(arena, .{ .path = doc.path, .text = doc.source }); + tr.note("server {s} (pid {d}), root {s}, {s} columns", .{ + @tagName(c.state), c.pid, c.root, @tagName(c.caps.enc), + }); + const deadline = nowMs() + req_ms; + + switch (kind) { + .definition, .declaration, .type_definition, .implementation, .references => { + const method = switch (kind) { + .definition => "textDocument/definition", + .declaration => "textDocument/declaration", + .type_definition => "textDocument/typeDefinition", + .implementation => "textDocument/implementation", + else => "textDocument/references", + }; + var b = try atPos(arena, uri.items, pos); + if (kind == .references) try b.appendSlice(arena, ",\"context\":{\"includeDeclaration\":true}"); + tr.note("-> {s} @ {d}:{d}", .{ method, pos.line + 1, pos.ch + 1 }); + const result = (try call(c, io, arena, method, b.items, deadline)) orelse { + tr.note("<- empty (no result, an error response, or null)", .{}); + return; + }; + try locations(&cx, c.caps.enc, result); + tr.note("<- {d} row(s)", .{cx.rows}); + }, + .select_refs => { + const b = try atPos(arena, uri.items, pos); + const result = (try call(c, io, arena, "textDocument/documentHighlight", b.items, deadline)) orelse return; + for (items(result)) |h| try emitRange(&cx, c.caps.enc, uri.items, get(h, "range"), ""); + }, + .hover => { + const b = try atPos(arena, uri.items, pos); + const result = (try call(c, io, arena, "textDocument/hover", b.items, deadline)) orelse return; + const contents = get(result, "contents") orelse return; + const text = str(contents) orelse str(get(contents, "value")) orelse blk: { + for (items(contents)) |m| if (str(m) orelse str(get(m, "value"))) |t| break :blk t; + break :blk ""; + }; + if (text.len == 0) return; + try out.writeAll(std.mem.trim(u8, text, " \t\r\n")); + try out.writeByte('\n'); + }, + .document_symbols => { + const b = try docParams(arena, uri.items); + const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return; + try walkSymbols(&cx, c.caps.enc, uri.items, result, 0); + }, + .workspace_symbols => { + var b: std.ArrayList(u8) = .empty; + try b.appendSlice(arena, "\"query\":"); + try jstr(&b, arena, req.arg); + const result = (try call(c, io, arena, "workspace/symbol", b.items, deadline)) orelse return; + for (items(result)) |sym| { + const loc = get(sym, "location") orelse continue; + 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), + .workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline), + .rename => { + var b = try atPos(arena, uri.items, pos); + try b.appendSlice(arena, ",\"newName\":"); + try jstr(&b, arena, req.arg); + const result = (try call(c, io, arena, "textDocument/rename", b.items, deadline)) orelse return; + // Other open files that say the old name, should the server + // answer for this file alone (zls renaming at a declaration does, + // whatever it was told): applied here, they would no longer build. + var also: std.ArrayList([]const u8) = .empty; + const old = identAt(req.source, req.offset); + if (old.len > 0) for (req.others) |doc| if (specFor(doc.path) == si and !std.mem.eql(u8, doc.path, req.path) and holdsWord(doc.source, old)) + try also.append(arena, doc.path); + try renameEdits(&cx, c.caps.enc, uri.items, result, req.source, old, also.items); + }, + .format => { + var b = try docParams(arena, uri.items); + try b.appendSlice(arena, ",\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); + const result = (try call(c, io, arena, "textDocument/formatting", b.items, deadline)) orelse return; + try formatEdits(&cx, c.caps.enc, result, req.source); + }, + .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 }); + const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return; + for (items(result)) |ca| { + const title = str(get(ca, "title")) orelse continue; + try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title)); + cx.rows += 1; + } + }, + .completion => { + var b = try atPos(arena, uri.items, pos); + try b.appendSlice(arena, ",\"context\":{\"triggerKind\":1}"); + const result = (try call(c, io, arena, "textDocument/completion", b.items, deadline)) orelse return; + const list = if (get(result, "items")) |it| items(it) else items(result); + const here = lsp.rel(cx.base, cx.cur_path); + var n: usize = 0; + for (list) |item| { + if (n >= 100) break; + const label = str(get(item, "label")) orelse continue; + const detail = str(get(item, "detail")) orelse ""; + var text: std.ArrayList(u8) = .empty; + try text.appendSlice(arena, label); + if (detail.len > 0) { + try text.appendSlice(arena, " "); + try text.appendSlice(arena, flat(arena, detail)); + } + // Every row carries the ASKING position: a completion item has + // no location of its own, so the honest place is where it would + // be inserted. n/N still step the list; Enter goes nowhere new. + try lsp.row(cx.out, here, pos.line, byteCol(req.source, pos.line, pos.ch, c.caps.enc), text.items); + n += 1; + } + }, + .callstack => try callstack(c, io, si, arena, &cx, uri.items, pos, deadline, tr, req.callstack_depth, req.budget), + .incoming_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .incoming, tr), + .outgoing_calls => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .outgoing, tr), + .supertypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .supers, tr), + .subtypes => try hierarchy(c, io, si, arena, &cx, uri.items, pos, deadline, .subs, tr), + .status, .explain, .index => unreachable, + } +} + +// -------------------------------------------------------- kind sub-handlers + +/// Goto/references result shapes: bare Location, Location[], LocationLink[]. +fn locations(cx: *Cx, enc: Enc, result: std.json.Value) Err!void { + if (result == .object) { + try emitRange(cx, enc, str(get(result, "uri")) orelse return, get(result, "range"), ""); + return; + } + for (items(result)) |loc| { + if (get(loc, "targetUri")) |tu| { + const r = get(loc, "targetSelectionRange") orelse get(loc, "targetRange"); + try emitRange(cx, enc, str(tu) orelse continue, r, ""); + } else { + try emitRange(cx, enc, str(get(loc, "uri")) orelse continue, get(loc, "range"), ""); + } + } +} + +/// DocumentSymbol[] nests (`children`), SymbolInformation[] is flat. +fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth: u8) Err!void { + if (depth > 8) return; + for (items(node)) |sym| { + const name = str(get(sym, "name")) orelse continue; + if (get(get(sym, "location"), "range")) |r| { + try emitRange(cx, enc, str(get(get(sym, "location"), "uri")) orelse uri, r, name); + } else { + try emitRange(cx, enc, uri, get(sym, "selectionRange") orelse get(sym, "range"), name); + } + if (get(sym, "children")) |kids| try walkSymbols(cx, enc, uri, kids, depth + 1); + } +} + +/// 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 { + 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; + } + var stale = true; + for (c.docs.items) |d| if (std.mem.eql(u8, d.uri, uri)) { + stale = d.stale; + break; + }; + if (stale) try waitFresh(c, io, uri, nowMs() + diag_ms); + try renderStore(c, arena, cx, uri); +} + +fn workspaceDiagnostics(c: *Conn, io: std.Io, arena: std.mem.Allocator, cx: *Cx, deadline: i64) Err!void { + if (c.caps.pull_workspace) { + const result = (try call(c, io, arena, "workspace/diagnostic", "\"previousResultIds\":[]", deadline)) orelse return; + for (items(get(result, "items"))) |per| { + const uri = str(get(per, "uri")) orelse continue; + for (items(get(per, "items"))) |dg| try emitDiag(cx, c.caps.enc, uri, dg, arena); + } + return; + } + try renderStore(c, arena, cx, null); +} + +/// One diagnostic row: `severity: message`, at the diagnostic's own range. +fn emitDiag(cx: *Cx, enc: Enc, uri: []const u8, dg: std.json.Value, arena: std.mem.Allocator) Err!void { + const sev = num(get(dg, "severity")) orelse 1; + const label: []const u8 = switch (sev) { + 1 => "error", + 2 => "warning", + 3 => "info", + else => "hint", + }; + const msg = try std.fmt.allocPrint(arena, "{s}: {s}", .{ label, flat(arena, str(get(dg, "message")) orelse "") }); + 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 { + 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); + } +} + +/// Block (mutex released) until the reader marks `uri` fresh or the deadline +/// passes. The reader broadcasts on every publishDiagnostics. +fn waitFresh(c: *Conn, io: std.Io, uri: []const u8, deadline: i64) error{Canceled}!void { + const g = c.gen; + while (c.gen == g and c.alive()) { + var fresh = false; + for (c.docs.items) |d| if (std.mem.eql(u8, d.uri, uri)) { + fresh = !d.stale; + break; + }; + if (fresh) return; + if (!try timedWait(c, io, deadline)) return; + } +} + +/// The identifier at byte `at` of `src`: the word a rename there names. +fn identAt(src: []const u8, at: usize) []const u8 { + const isId = struct { + fn f(ch: u8) bool { + return std.ascii.isAlphanumeric(ch) or ch == '_'; + } + }.f; + var start = @min(at, src.len); + while (start > 0 and isId(src[start - 1])) start -= 1; + var end = @min(at, src.len); + while (end < src.len and isId(src[end])) end += 1; + return src[start..end]; +} + +/// Whether `text` holds `word` as a whole identifier. +fn holdsWord(text: []const u8, word: []const u8) bool { + var from: usize = 0; + while (std.mem.indexOfPos(u8, text, from, word)) |i| : (from = i + 1) { + const before = i == 0 or !(std.ascii.isAlphanumeric(text[i - 1]) or text[i - 1] == '_'); + const end = i + word.len; + const after = end == text.len or !(std.ascii.isAlphanumeric(text[end]) or text[end] == '_'); + if (before and after) return true; + } + return false; +} + +/// (line, character) -> byte offset, or null when the line does not exist or +/// the character runs past its end. End-of-line (character == line length) is +/// a real position — that is where an insert-at-EOL lands. +fn strictOffset(src: []const u8, line: u32, ch: u32, enc: Enc) ?usize { + var bol: usize = 0; + var n: u32 = 0; + while (n < line) : (n += 1) + bol = (std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse return null) + 1; + const eol = std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse src.len; + if (enc == .utf8) { + if (ch > eol - bol) return null; + return bol + ch; + } + var units: u32 = 0; + var i: usize = bol; + while (i < eol and units < ch) { + const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1; + units += if (l == 4 and enc == .utf16) 2 else 1; + i += l; + } + if (units < ch) return null; + return i; +} + +/// Whole-document Range -> half-open byte span for a MUTATING record. +/// STRICT: null unless both endpoints denote positions that exist in the +/// document — for edits, a clamped range is a wrong edit, and wrong edits +/// fail closed (the render paths keep their forgiving conversions; a row a +/// column off is an inconvenience, a splice a column off is corruption). +fn byteSpan(src: []const u8, range: ?std.json.Value, enc: Enc) ?struct { start: usize, end: usize } { + const r = rangeOf(range) orelse return null; + const start = strictOffset(src, r.sl, r.sc, enc) orelse return null; + const end = strictOffset(src, r.el, r.ec, enc) orelse return null; + if (end < start) return null; + return .{ .start = start, .end = end }; +} + +/// A WorkspaceEdit that stays inside the asked-about file becomes `@put` +/// records the core applies as one undo transaction; anything wider (a real +/// multi-file rename, file creates/renames) becomes a PREVIEW — one location +/// row per would-be edit, in the same buffer `gr` fills, because silently +/// applying a fraction of a workspace rename would be worse than either. +fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, src: []const u8, old: []const u8, also: []const []const u8) Err!void { + var edits: std.ArrayList(PutEdit) = .empty; + var foreign = false; + + // (uri, edits) per file, from either shape of a WorkspaceEdit + var files: std.ArrayList(struct { []const u8, ?std.json.Value }) = .empty; + if (get(result, "documentChanges")) |dcs| { + for (items(dcs)) |dc| { + if (get(dc, "kind") != null) { + foreign = true; // create/rename/delete file operations + continue; + } + const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; + try files.append(cx.arena, .{ u, get(dc, "edits") }); + } + } else if (get(result, "changes")) |ch| if (ch == .object) { + var it = ch.object.iterator(); + while (it.next()) |e| try files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* }); + }; for (files.items) |f| { const in_self = std.mem.eql(u8, f[0], self_uri); if (!in_self) foreign = true; @@ -1055,398 +1452,200 @@ fn indexHops(a: std.mem.Allocator, ix: *Scip.Index, symbol: u32) Err![]Hop { }; try hop.sites.append(a, ix.occurrences[o].range); } - return hops.items; -} - -fn callPrefix(out: *std.Io.Writer, tree: *const CallTree, depth: usize) Err!void { - for (tree.siblings[0..depth]) |more| try out.writeAll(if (more) "│ " else " "); -} - -fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator) Err!void { - if (!c.caps.call_hier) return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name}); - const b = try atPos(arena, uri, pos); - const prepared = (try call(c, io, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return; - if (items(prepared).len == 0) return lsp.none(cx.out, "no function at this position", .{}); - // The rows come from the tree alone: the core reads the sources. - cx.no_text = true; - // Each response is parsed into its own part of the region, freed once - // its subtree is walked; only the path being walked is live JSON. - // Raw, as query()'s scratch: `alloc` would fill all ~100 MB of it. - const parse_len = if (budget) |fixed| (fixed.buffer.len - fixed.end_index) / 2 else 0; - var parse_fba: std.heap.FixedBufferAllocator = .init(if (budget != null) - (arena.rawAlloc(parse_len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..parse_len] - else - &.{}); - const parse = if (budget != null) parse_fba.allocator() else arena; - for (items(prepared)) |item| { - const focus: CallTree.Call = .{ .uri = str(get(item, "uri")) orelse continue, .name = "", .at = rangeOf(get(item, "selectionRange") orelse get(item, "range")) orelse continue }; - const incoming = try hierarchyResult(c, io, parse, item, "callHierarchy/incomingCalls", deadline); - const src: Calls = .{ .server = .{ .c = c, .io = io, .parse = if (budget != null) &parse_fba else null } }; - try callTree(src, si, cx, c.caps.enc, focus, try serverHops(parse, incoming), tr, max_depth, budget); - } -} - -/// A Callstack from the first callers of the function asked at: walked to -/// the depth setting, rendered, and why it stopped said. -fn callTree(src: Calls, si: usize, cx: *Cx, enc: Enc, focus: CallTree.Call, first: []const Hop, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator) Err!void { - var tree: CallTree = .{ - .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth), - .si = si, - .started_ms = nowMs(), - .budget = budget, - // The walk stops while the answer's writer has room for the rest. - .byte_cap = if (cx.fixed_out) (cx.out.buffer.len - cx.out.end) -| (1 << 20) else std.math.maxInt(usize), - }; - const count = try collectCallers(src, cx.arena, focus, first, &tree, 0); - if (tree.narrated) post(si, .always, "Callstack: {d} callers", .{tree.collected}); - if (count == 0) try cx.out.writeAll("No callers reported\n"); - try renderCallers(cx, enc, &tree, tree.roots.items, 0); - if (tree.out_of_time) { - try cx.out.writeAll("… the language server ran out of time; continue from a function above with Callstack\n"); - } else if (tree.out_of_memory) { - try cx.out.writeAll("… stopped at the query's memory limit; continue from a function above with Callstack\n"); - } else if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n"); - tr.note("callstack: {d} callers", .{tree.rendered}); -} - -/// No caller count and no overall deadline: the walk narrates itself on the -/// message row after a second, and it stops when a request goes unanswered -/// or the answer is full (what was collected is shown, its last row saying -/// so), or when the shell cancels it for a newer query. -fn collectCallers(src: Calls, arena: std.mem.Allocator, focus: CallTree.Call, hops: []const Hop, tree: *CallTree, depth: usize) Err!usize { - var count: usize = 0; - for (hops) |hop| { - if (tree.budget) |b| if (b.buffer.len - b.end_index < callstack_reserve) { - tree.out_of_memory = true; - }; - if (tree.rows >= tree.row_cap or tree.bytes >= tree.byte_cap) tree.out_of_memory = true; - if (tree.out_of_time or tree.out_of_memory) break; - const cycle = sameCall(focus, hop.call) or for (tree.path[0..depth]) |ancestor| { - if (sameCall(ancestor.call, hop.call)) break true; - } else false; - tree.path[depth] = hop; - count += 1; - tree.collected += 1; - if (nowMs() - tree.started_ms >= 1000) { - tree.narrated = true; - post(tree.si, .chatty, "Callstack: {d} callers\u{2026}", .{tree.collected}); - } - var children: usize = 0; - if (!cycle) { - if (depth + 1 == tree.depth_limit) { - tree.limited = true; - } else if (src == .index) { - children = try collectCallers(src, arena, focus, try indexHops(arena, src.index, hop.symbol), tree, depth + 1); - } else { - const parse = src.server.parse; - const mark = if (parse) |fixed| fixed.end_index else 0; - defer if (parse) |fixed| { - fixed.end_index = mark; - }; - const a = if (parse) |fixed| fixed.allocator() else arena; - const ancestors = hierarchyResult(src.server.c, src.server.io, a, hop.item.?, "callHierarchy/incomingCalls", nowMs() + req_ms) catch |e| switch (e) { - error.Timeout => blk: { - tree.out_of_time = true; - break :blk null; - }, - // A response bigger than the parse region: this caller - // stops here, the walk with it. - error.OutOfMemory => blk: { - tree.out_of_memory = true; - break :blk null; - }, - else => return e, - }; - if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(src, arena, focus, try serverHops(a, ancestors), tree, depth + 1); - } - } - if (children == 0) try tree.addPath(arena, tree.path[0 .. depth + 1], cycle); - } - return count; -} - -/// Test callers sink below the rest; each group keeps the usual order. -// ponytail: substring heuristic, "latest.zig" or "contest" count as tests too -fn testCaller(caller: CallTree.Call) bool { - return std.ascii.findIgnoreCase(caller.name, "test") != null or - std.ascii.findIgnoreCase(std.Io.Dir.path.basenamePosix(caller.uri), "test") != null; -} - -fn callerLess(tree: *const CallTree, a: usize, b: usize) bool { - const left = tree.nodes.items[a].call; - const right = tree.nodes.items[b].call; - if (testCaller(left) != testCaller(right)) return testCaller(right); - const uri_order = std.mem.order(u8, left.uri, right.uri); - if (uri_order != .eq) return uri_order == .lt; - return rangeLess(left.at, right.at); -} - -fn rangeLess(left: Range, right: Range) bool { - return left.sl < right.sl or (left.sl == right.sl and left.sc < right.sc); -} - -fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: usize) Err!void { - std.mem.sort(usize, children, @as(*const CallTree, tree), callerLess); - for (children, 0..) |id, index| { - // emitRange's row ceiling and the writer's room, met here before a - // prefix is half written - if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { - tree.limited = true; - return; - } - const node = &tree.nodes.items[id]; - const more = index + 1 < children.len; - try callPrefix(cx.out, tree, depth); - const has_children = node.children.items.len > 0 or node.ranges.items.len > 0; - try cx.out.writeAll(if (more) "├" else "╰"); - try cx.out.writeAll(if (has_children) "──┬ " else "─── "); - const name = flat(cx.arena, node.call.name); - const label = if (node.cycle) try std.fmt.allocPrint(cx.arena, "{s} ↻ recursion", .{name}) else name; - try emitAt(cx, enc, node.call.uri, node.call.at, label); - tree.rendered += 1; - tree.siblings[depth] = more; - std.mem.sort(CallTree.Site, node.ranges.items, {}, struct { - fn less(_: void, a: CallTree.Site, b: CallTree.Site) bool { - return rangeLess(a.range, b.range); - } - }.less); - for (node.ranges.items, 0..) |site, site_index| { - if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { - tree.limited = true; - return; - } - if (site.focus) try cx.out.writeAll(lsp.callstack_match); - try callPrefix(cx.out, tree, depth + 1); - try cx.out.writeAll(if (node.children.items.len > 0 or site_index + 1 < node.ranges.items.len) "├─ " else "╰─ "); - try emitAt(cx, enc, node.call.uri, site.range, ""); - } - try renderCallers(cx, enc, tree, node.children.items, depth + 1); - } + return hops.items; } -const Hier = enum { incoming, outgoing, supers, subs }; +fn callPrefix(out: *std.Io.Writer, tree: *const CallTree, depth: usize) Err!void { + for (tree.siblings[0..depth]) |more| try out.writeAll(if (more) "│ " else " "); +} -/// The two-step hierarchy kinds: prepare at the cursor, then follow every -/// item the server returned (usually one). Gated on the server capability so -/// an old server costs zero round trips. -fn hierarchy(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, h: Hier, tr: *Trace) Err!void { - const call_side = h == .incoming or h == .outgoing; - if (call_side and !c.caps.call_hier) { - tr.note("STOP: {s} does not advertise callHierarchyProvider", .{specs[si].name}); - return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name}); - } - if (!call_side and !c.caps.type_hier) { - tr.note("STOP: {s} does not advertise typeHierarchyProvider", .{specs[si].name}); - return lsp.none(cx.out, "{s} does not answer type hierarchy (no typeHierarchyProvider)", .{specs[si].name}); - } - const prepare: []const u8 = if (call_side) "textDocument/prepareCallHierarchy" else "textDocument/prepareTypeHierarchy"; - const follow: []const u8 = switch (h) { - .incoming => "callHierarchy/incomingCalls", - .outgoing => "callHierarchy/outgoingCalls", - .supers => "typeHierarchy/supertypes", - .subs => "typeHierarchy/subtypes", - }; +fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx, uri: []const u8, pos: Pos, deadline: i64, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator) Err!void { + if (!c.caps.call_hier) return lsp.none(cx.out, "{s} does not answer call hierarchy (no callHierarchyProvider)", .{specs[si].name}); const b = try atPos(arena, uri, pos); - const prepared = (try call(c, io, arena, prepare, b.items, deadline)) orelse return; + const prepared = (try call(c, io, arena, "textDocument/prepareCallHierarchy", b.items, deadline)) orelse return; + if (items(prepared).len == 0) return lsp.none(cx.out, "no function at this position", .{}); + // The rows come from the tree alone: the core reads the sources. + cx.no_text = true; + // Each response is parsed into its own part of the region, freed once + // its subtree is walked; only the path being walked is live JSON. + // Raw, as query()'s scratch: `alloc` would fill all ~100 MB of it. + const parse_len = if (budget) |fixed| (fixed.buffer.len - fixed.end_index) / 2 else 0; + var parse_fba: std.heap.FixedBufferAllocator = .init(if (budget != null) + (arena.rawAlloc(parse_len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..parse_len] + else + &.{}); + const parse = if (budget != null) parse_fba.allocator() else arena; for (items(prepared)) |item| { - const result = (try hierarchyResult(c, io, arena, item, follow, deadline)) orelse return; - for (items(result)) |entry| switch (h) { - .incoming => { - // one row per CALL SITE, under the caller's name — that is - // what n/N want to walk - const from = get(entry, "from") orelse continue; - const fu = str(get(from, "uri")) orelse continue; - const name = str(get(from, "name")) orelse ""; - const ranges = items(get(entry, "fromRanges")); - if (ranges.len == 0) { - try emitRange(cx, c.caps.enc, fu, get(from, "selectionRange"), name); - } else for (ranges) |r| try emitRange(cx, c.caps.enc, fu, r, name); - }, - .outgoing => { - const to = get(entry, "to") orelse continue; - try emitRange(cx, c.caps.enc, str(get(to, "uri")) orelse continue, get(to, "selectionRange") orelse get(to, "range"), hierText(cx.arena, to)); - }, - .supers, .subs => try emitRange(cx, c.caps.enc, str(get(entry, "uri")) orelse continue, get(entry, "selectionRange") orelse get(entry, "range"), hierText(cx.arena, entry)), - }; + const focus: CallTree.Call = .{ .uri = str(get(item, "uri")) orelse continue, .name = "", .at = rangeOf(get(item, "selectionRange") orelse get(item, "range")) orelse continue }; + const incoming = try hierarchyResult(c, io, parse, item, "callHierarchy/incomingCalls", deadline); + const src: Calls = .{ .server = .{ .c = c, .io = io, .parse = if (budget != null) &parse_fba else null } }; + try callTree(src, si, cx, c.caps.enc, focus, try serverHops(parse, incoming), tr, max_depth, budget); } } -/// Depth of a parsed json value, saturating just past `at`'s caller's cap — -/// the guard that keeps server-controlled nesting away from Stringify's -/// fixed-depth assertion. -fn jsonDepth(v: std.json.Value, at: u32) u32 { - if (at > 96) return at; - return switch (v) { - .object => |o| blk: { - var deepest = at; - var it = o.iterator(); - while (it.next()) |e| deepest = @max(deepest, jsonDepth(e.value_ptr.*, at + 1)); - break :blk deepest; - }, - .array => |a| blk: { - var deepest = at; - for (a.items) |e| deepest = @max(deepest, jsonDepth(e, at + 1)); - break :blk deepest; - }, - else => at, +/// A Callstack from the first callers of the function asked at: walked to +/// the depth setting, rendered, and why it stopped said. +fn callTree(src: Calls, si: usize, cx: *Cx, enc: Enc, focus: CallTree.Call, first: []const Hop, tr: *Trace, max_depth: u8, budget: ?*const std.heap.FixedBufferAllocator) Err!void { + var tree: CallTree = .{ + .depth_limit = std.math.clamp(max_depth, 1, lsp.callstack_max_depth), + .si = si, + .started_ms = nowMs(), + .budget = budget, + // The walk stops while the answer's writer has room for the rest. + .byte_cap = if (cx.fixed_out) (cx.out.buffer.len - cx.out.end) -| (1 << 20) else std.math.maxInt(usize), }; + const count = try collectCallers(src, cx.arena, focus, first, &tree, 0); + if (tree.narrated) post(si, .always, "Callstack: {d} callers", .{tree.collected}); + if (count == 0) try cx.out.writeAll("No callers reported\n"); + try renderCallers(cx, enc, &tree, tree.roots.items, 0); + if (tree.out_of_time) { + try cx.out.writeAll("… the language server ran out of time; continue from a function above with Callstack\n"); + } else if (tree.out_of_memory) { + try cx.out.writeAll("… stopped at the query's memory limit; continue from a function above with Callstack\n"); + } else if (tree.limited) try cx.out.writeAll("… continue from a function above with Callstack\n"); + tr.note("callstack: {d} callers", .{tree.rendered}); } -fn hierText(arena: std.mem.Allocator, item: std.json.Value) []const u8 { - const name = str(get(item, "name")) orelse ""; - const detail = str(get(item, "detail")) orelse return name; - return std.fmt.allocPrint(arena, "{s} {s}", .{ name, flat(arena, detail) }) catch name; -} - -// ---------------------------------------------------------------- the index - -/// The index's answer (Scip.zig), in the rows the server's would make: true -/// when it had one, false to ask the server: no index above the file, a -/// file it does not hold, unsaved changes it has not seen, nothing at the -/// cursor, a symbol it does not know, or a word only a server answers. -fn fromIndex(io: std.Io, arena: std.mem.Allocator, req: lsp.Req, kind: lsp.Kind, out: *std.Io.Writer, tr: *Trace) Err!bool { - switch (kind) { - .definition, .type_definition, .implementation, .references, .select_refs, .hover, .document_symbols, .workspace_symbols, .incoming_calls, .outgoing_calls, .callstack, .supertypes, .subtypes => {}, - else => return false, - } - if (req.dirty) { - tr.note("unsaved changes the index has not seen: the server", .{}); - return false; - } - const dir = std.Io.Dir.path.dirname(req.path) orelse return false; - const ix = (Scip.find(io, dir) catch |e| { - tr.note("index.scip above {s}: {s}", .{ dir, @errorName(e) }); - return false; - }) orelse { - tr.note("no index.scip above {s}", .{dir}); - return false; - }; - const doc = ix.document(req.path) orelse { - tr.note("{s} holds no {s}", .{ ix.path(), std.Io.Dir.path.basename(req.path) }); - return false; - }; - const si = specFor(req.path) orelse 0; - if (ix.stale(io, req.path)) { - tr.note("stale: {s} is newer than the index", .{std.Io.Dir.path.basename(req.path)}); - if (!ix.stale_said) post(si, .always, "{s} is older than {s}: Index makes it again", .{ ix.path(), std.Io.Dir.path.basename(req.path) }); - ix.stale_said = true; - } - const enc = ix.documents[doc].enc; - var cx: Cx = .{ .io = io, .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out, .fixed_out = true, .relative_parents = kind == .callstack }; - for (req.others) |d| try cx.files.append(arena, .{ .path = d.path, .text = d.source }); - if (kind == .document_symbols or kind == .workspace_symbols) { - for (ix.occurrences, 0..) |o, i| { - if (kind == .document_symbols and o.doc != doc) continue; - // A name, never a local or a whole file's module. - if (o.roles & Scip.role_definition == 0 or o.range.sl != o.range.el or std.mem.startsWith(u8, ix.symbols.items[o.symbol], "local ")) continue; - const name = displayName(ix, o.symbol); - if (kind == .workspace_symbols and std.ascii.findIgnoreCase(name, req.arg) == null) continue; - try emitOcc(&cx, ix, @intCast(i), name); +/// No caller count and no overall deadline: the walk narrates itself on the +/// message row after a second, and it stops when a request goes unanswered +/// or the answer is full (what was collected is shown, its last row saying +/// so), or when the shell cancels it for a newer query. +fn collectCallers(src: Calls, arena: std.mem.Allocator, focus: CallTree.Call, hops: []const Hop, tree: *CallTree, depth: usize) Err!usize { + var count: usize = 0; + for (hops) |hop| { + if (tree.budget) |b| if (b.buffer.len - b.end_index < callstack_reserve) { + tree.out_of_memory = true; + }; + if (tree.rows >= tree.row_cap or tree.bytes >= tree.byte_cap) tree.out_of_memory = true; + if (tree.out_of_time or tree.out_of_memory) break; + const cycle = sameCall(focus, hop.call) or for (tree.path[0..depth]) |ancestor| { + if (sameCall(ancestor.call, hop.call)) break true; + } else false; + tree.path[depth] = hop; + count += 1; + tree.collected += 1; + if (nowMs() - tree.started_ms >= 1000) { + tree.narrated = true; + post(tree.si, .chatty, "Callstack: {d} callers\u{2026}", .{tree.collected}); } - tr.note("<- {d} row(s) from the index", .{cx.rows}); - return cx.rows > 0; - } - const pos = posOf(req.source, req.offset, enc); - const at = ix.at(doc, pos.line, pos.ch) orelse { - tr.note("no occurrence at {d}:{d} in {s}", .{ pos.line + 1, pos.ch + 1, ix.path() }); - return false; - }; - const sym = ix.occurrences[at].symbol; - tr.note("{s}: occurrence '{s}' at {d}:{d}", .{ ix.path(), ix.symbols.items[sym], pos.line + 1, pos.ch + 1 }); - switch (kind) { - .definition => try emitDefinitions(&cx, ix, sym, false), - .type_definition => for (try related(arena, ix, sym, 4)) |to| try emitDefinitions(&cx, ix, to, false), - .supertypes => for (try related(arena, ix, sym, 3)) |to| try emitDefinitions(&cx, ix, to, true), - .implementation, .subtypes => for (ix.infos.items, 0..) |_, from| { - if (std.mem.indexOfScalar(u32, try related(arena, ix, @intCast(from), 3), sym) != null) - try emitDefinitions(&cx, ix, @intCast(from), kind == .subtypes); - }, - .references, .select_refs => { - var also = try related(arena, ix, sym, 2); - also = try std.mem.concat(arena, u32, &.{ &.{sym}, also }); - for (also) |of| for (ix.of(of)) |o| if (kind == .references or ix.occurrences[o].doc == doc) try emitOcc(&cx, ix, o, ""); - }, - .hover => { - const info = ix.infos.items[sym]; - var said = false; - if (Scip.field(info, 7)) |sig| if (Scip.field(sig.bytes, 5)) |text| { - try out.writeAll(std.mem.trim(u8, text.bytes, " \t\r\n")); - said = true; - }; - var r: Scip.Reader = .{ .bytes = info }; - while (r.next() catch null) |f| if (f.num == 3) { - if (said) try out.writeAll("\n\n"); - try out.writeAll(std.mem.trim(u8, f.bytes, " \t\r\n")); - said = true; - }; - if (said) try out.writeByte('\n') else tr.note("no documentation for it in the index", .{}); - return said; - }, - .incoming_calls => for (try indexHops(arena, ix, sym)) |hop| for (hop.sites.items) |site| - try emitPath(&cx, ix.documents[hop.doc].enc, try indexPath(arena, ix, hop.doc), site, hop.call.name), - .outgoing_calls => for (ix.of(sym)) |d| { - // The calls inside its definition, each function once, at its - // own definition. - const def = ix.occurrences[d]; - if (def.roles & Scip.role_definition == 0) continue; - var seen: std.ArrayList(u32) = .empty; - const in = ix.documents[def.doc]; - for (ix.occurrences[in.first..][0..in.len]) |o| { - if (o.roles & Scip.role_definition != 0 or !Scip.holds(def.enclosing, o.range) or !Scip.callable(ix.infos.items[o.symbol])) continue; - if (std.mem.indexOfScalar(u32, seen.items, o.symbol) != null) continue; - try seen.append(arena, o.symbol); - try emitDefinitions(&cx, ix, o.symbol, true); - } - break; - }, - .callstack => { - const def = for (ix.of(sym)) |o| { - if (ix.occurrences[o].roles & Scip.role_definition != 0) break ix.occurrences[o]; + var children: usize = 0; + if (!cycle) { + if (depth + 1 == tree.depth_limit) { + tree.limited = true; + } else if (src == .index) { + children = try collectCallers(src, arena, focus, try indexHops(arena, src.index, hop.symbol), tree, depth + 1); } else { - tr.note("no definition of it in the index", .{}); - return false; - }; - // The rows come from the tree alone: the core reads the sources. - cx.no_text = true; - const focus: CallTree.Call = .{ .uri = try indexUri(arena, ix, def.doc), .name = "", .at = def.range }; - try callTree(.{ .index = ix }, si, &cx, enc, focus, try indexHops(arena, ix, sym), tr, req.callstack_depth, req.budget); - return true; - }, - else => unreachable, + const parse = src.server.parse; + const mark = if (parse) |fixed| fixed.end_index else 0; + defer if (parse) |fixed| { + fixed.end_index = mark; + }; + const a = if (parse) |fixed| fixed.allocator() else arena; + const ancestors = hierarchyResult(src.server.c, src.server.io, a, hop.item.?, "callHierarchy/incomingCalls", nowMs() + req_ms) catch |e| switch (e) { + error.Timeout => blk: { + tree.out_of_time = true; + break :blk null; + }, + // A response bigger than the parse region: this caller + // stops here, the walk with it. + error.OutOfMemory => blk: { + tree.out_of_memory = true; + break :blk null; + }, + else => return e, + }; + if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(src, arena, focus, try serverHops(a, ancestors), tree, depth + 1); + } + } + if (children == 0) try tree.addPath(arena, tree.path[0 .. depth + 1], cycle); } - tr.note("<- {d} row(s) from the index", .{cx.rows}); - return cx.rows > 0; + return count; } -/// A symbol's definitions, as rows: named by the symbol, as a type -/// hierarchy's are, or by their source lines. -fn emitDefinitions(cx: *Cx, ix: *Scip.Index, symbol: u32, named: bool) Err!void { - for (ix.of(symbol)) |o| if (ix.occurrences[o].roles & Scip.role_definition != 0) { - const sig = if (Scip.field(ix.infos.items[symbol], 7)) |f| (Scip.field(f.bytes, 5) orelse Scip.Field{ .num = 0 }).bytes else ""; - const note = if (!named) "" else if (sig.len == 0) displayName(ix, symbol) else try std.fmt.allocPrint(cx.arena, "{s} {s}", .{ displayName(ix, symbol), flat(cx.arena, sig) }); - try emitOcc(cx, ix, o, note); - }; +/// Test callers sink below the rest; each group keeps the usual order. +// ponytail: substring heuristic, "latest.zig" or "contest" count as tests too +fn testCaller(caller: CallTree.Call) bool { + return std.ascii.findIgnoreCase(caller.name, "test") != null or + std.ascii.findIgnoreCase(std.Io.Dir.path.basenamePosix(caller.uri), "test") != null; } -fn emitOcc(cx: *Cx, ix: *const Scip.Index, o: u32, note: []const u8) Err!void { - const occ = ix.occurrences[o]; - try emitPath(cx, ix.documents[occ.doc].enc, try indexPath(cx.arena, ix, occ.doc), occ.range, note); +fn callerLess(tree: *const CallTree, a: usize, b: usize) bool { + const left = tree.nodes.items[a].call; + const right = tree.nodes.items[b].call; + if (testCaller(left) != testCaller(right)) return testCaller(right); + const uri_order = std.mem.order(u8, left.uri, right.uri); + if (uri_order != .eq) return uri_order == .lt; + return rangeLess(left.at, right.at); } -/// The symbols `symbol`'s relationships name with flag `flag` set -/// (scip.proto Relationship: 2 is_reference, 3 is_implementation, 4 -/// is_type_definition), the ones the index knows. -fn related(a: std.mem.Allocator, ix: *const Scip.Index, symbol: u32, flag: u64) Err![]u32 { - var out: std.ArrayList(u32) = .empty; - var r: Scip.Reader = .{ .bytes = ix.infos.items[symbol] }; - while (r.next() catch null) |f| if (f.num == 4) { - const to = Scip.field(f.bytes, 1) orelse continue; - if ((Scip.field(f.bytes, flag) orelse continue).int == 0) continue; - if (ix.ids.get(to.bytes)) |id| try out.append(a, id); +fn rangeLess(left: Range, right: Range) bool { + return left.sl < right.sl or (left.sl == right.sl and left.sc < right.sc); +} + +fn renderCallers(cx: *Cx, enc: Enc, tree: *CallTree, children: []usize, depth: usize) Err!void { + std.mem.sort(usize, children, @as(*const CallTree, tree), callerLess); + for (children, 0..) |id, index| { + // emitRange's row ceiling and the writer's room, met here before a + // prefix is half written + if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { + tree.limited = true; + return; + } + const node = &tree.nodes.items[id]; + const more = index + 1 < children.len; + try callPrefix(cx.out, tree, depth); + const has_children = node.children.items.len > 0 or node.ranges.items.len > 0; + try cx.out.writeAll(if (more) "├" else "╰"); + try cx.out.writeAll(if (has_children) "──┬ " else "─── "); + const name = flat(cx.arena, node.call.name); + const label = if (node.cycle) try std.fmt.allocPrint(cx.arena, "{s} ↻ recursion", .{name}) else name; + try emitAt(cx, enc, node.call.uri, node.call.at, label); + tree.rendered += 1; + tree.siblings[depth] = more; + std.mem.sort(CallTree.Site, node.ranges.items, {}, struct { + fn less(_: void, a: CallTree.Site, b: CallTree.Site) bool { + return rangeLess(a.range, b.range); + } + }.less); + for (node.ranges.items, 0..) |site, site_index| { + if (cx.rows >= max_rows or (cx.fixed_out and cx.out.buffer.len - cx.out.end < row_room)) { + tree.limited = true; + return; + } + if (site.focus) try cx.out.writeAll(lsp.callstack_match); + try callPrefix(cx.out, tree, depth + 1); + try cx.out.writeAll(if (node.children.items.len > 0 or site_index + 1 < node.ranges.items.len) "├─ " else "╰─ "); + try emitAt(cx, enc, node.call.uri, site.range, ""); + } + try renderCallers(cx, enc, tree, node.children.items, depth + 1); + } +} + +const Hier = enum { incoming, outgoing, supers, subs }; + +/// Depth of a parsed json value, saturating just past `at`'s caller's cap — +/// the guard that keeps server-controlled nesting away from Stringify's +/// fixed-depth assertion. +fn jsonDepth(v: std.json.Value, at: u32) u32 { + if (at > 96) return at; + return switch (v) { + .object => |o| blk: { + var deepest = at; + var it = o.iterator(); + while (it.next()) |e| deepest = @max(deepest, jsonDepth(e.value_ptr.*, at + 1)); + break :blk deepest; + }, + .array => |a| blk: { + var deepest = at; + for (a.items) |e| deepest = @max(deepest, jsonDepth(e, at + 1)); + break :blk deepest; + }, + else => at, }; - return out.items; } +// ---------------------------------------------------------------- the index + /// A symbol's name to show: its display name, else its string's last /// descriptor (`... lib/add().` is `add`). fn displayName(ix: *const Scip.Index, symbol: u32) []const u8 { @@ -1465,17 +1664,6 @@ fn indexUri(a: std.mem.Allocator, ix: *const Scip.Index, doc: u32) Err![]const u return b.items; } -/// The Index word's command line: the language's indexer, at the -/// project's root, where the index it writes answers for every file under -/// it (`PARDES_SCIP_RS` and the like name another indexer). -fn indexCommand(req: lsp.Req, out: *std.Io.Writer) !void { - const si = specFor(req.path) orelse return lsp.none(out, "no indexer for {s}", .{std.Io.Dir.path.basename(req.path)}); - const cmd = if (libc.getenv(indexer_envs[si])) |v| std.mem.span(v) else specs[si].indexer; - try out.writeAll("@run cd '"); - for (rootOf(std.Io.Dir.path.dirname(req.path) orelse "/", specs[si].markers)) |ch| try if (ch == '\'') out.writeAll("'\\''") else out.writeByte(ch); - try out.print("' && {s}\n", .{cmd}); -} - // ------------------------------------------------------------ row rendering /// Files read while answering ONE query, so a references list over a handful @@ -1523,6 +1711,33 @@ fn emitRange(cx: *Cx, enc: Enc, uri: []const u8, range: ?std.json.Value, note: [ return emitAt(cx, enc, uri, rangeOf(range) orelse return, note); } +/// `file:///a/b%20c` -> `/a/b c`. Rows carry real paths; look.zig opens them +/// and `%20` is not a filename. A decoded CONTROL byte rejects the whole uri: +/// a `%0A` in a path would split one location into two physical rows — a +/// server-forged extra row in the results buffer — and no path worth opening +/// has a newline, tab or NUL in it. +fn pathOf(arena: std.mem.Allocator, uri: []const u8) ?[]const u8 { + var rest = uri; + if (std.mem.startsWith(u8, rest, "file://")) { + rest = rest["file://".len..]; + if (rest.len > 0 and rest[0] != '/') return null; // an authority we cannot open + } else if (std.mem.indexOf(u8, rest, "://") != null) return null; + var out: std.ArrayList(u8) = .empty; + var i: usize = 0; + while (i < rest.len) { + var byte: u8 = rest[i]; + if (rest[i] == '%' and i + 2 < rest.len) { + if (std.fmt.parseInt(u8, rest[i + 1 .. i + 3], 16)) |b| { + byte = b; + i += 3; + } else |_| i += 1; + } else i += 1; + if (byte < 0x20) return null; + out.append(arena, byte) catch return null; + } + return out.items; +} + fn emitAt(cx: *Cx, enc: Enc, uri: []const u8, r: Range, note: []const u8) Err!void { return emitPath(cx, enc, pathOf(cx.arena, uri) orelse return, r, note); } @@ -1577,54 +1792,6 @@ fn colBytes(lntext: []const u8, ch: u32, enc: Enc) usize { return i; } -/// The current-buffer byte column for a (line, ch) position — used only for -/// rows that point at the asking position itself. -fn byteCol(src: []const u8, line: u32, ch: u32, enc: Enc) usize { - var bol: usize = 0; - var n: u32 = 0; - while (n < line) : (n += 1) - bol = (std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse return ch) + 1; - const eol = std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse src.len; - return colBytes(src[bol..eol], ch, enc); -} - -/// Whole-document Range -> half-open byte span for a MUTATING record. -/// STRICT: null unless both endpoints denote positions that exist in the -/// document — for edits, a clamped range is a wrong edit, and wrong edits -/// fail closed (the render paths keep their forgiving conversions; a row a -/// column off is an inconvenience, a splice a column off is corruption). -fn byteSpan(src: []const u8, range: ?std.json.Value, enc: Enc) ?struct { start: usize, end: usize } { - const r = rangeOf(range) orelse return null; - const start = strictOffset(src, r.sl, r.sc, enc) orelse return null; - const end = strictOffset(src, r.el, r.ec, enc) orelse return null; - if (end < start) return null; - return .{ .start = start, .end = end }; -} - -/// (line, character) -> byte offset, or null when the line does not exist or -/// the character runs past its end. End-of-line (character == line length) is -/// a real position — that is where an insert-at-EOL lands. -fn strictOffset(src: []const u8, line: u32, ch: u32, enc: Enc) ?usize { - var bol: usize = 0; - var n: u32 = 0; - while (n < line) : (n += 1) - bol = (std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse return null) + 1; - const eol = std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse src.len; - if (enc == .utf8) { - if (ch > eol - bol) return null; - return bol + ch; - } - var units: u32 = 0; - var i: usize = bol; - while (i < eol and units < ch) { - const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1; - units += if (l == 4 and enc == .utf16) 2 else 1; - i += l; - } - if (units < ch) return null; - return i; -} - /// Prose squashed onto the one line a row is: newlines and tabs to spaces, /// cut to a width a pane can show. fn flat(arena: std.mem.Allocator, s: []const u8) []const u8 { @@ -1640,35 +1807,82 @@ fn flat(arena: std.mem.Allocator, s: []const u8) []const u8 { } if (buf.items.len >= 200) break; } - return std.mem.trim(u8, buf.items, " "); -} - -fn setCloexec(fd: c_int) void { - const FD_CLOEXEC: c_int = 1; - _ = libc.fcntl(fd, libc.F.SETFD, FD_CLOEXEC); + return std.mem.trim(u8, buf.items, " "); +} + +fn setCloexec(fd: c_int) void { + const FD_CLOEXEC: c_int = 1; + _ = libc.fcntl(fd, libc.F.SETFD, FD_CLOEXEC); +} + +// Darwin needs fcntl after socketpair; another concurrent fork can inherit the pair in that window. +fn transportPair(sv: *[2]libc.fd_t) bool { + const sock_type = if (comptime builtin.os.tag.isDarwin()) + libc.SOCK.STREAM + else + libc.SOCK.STREAM | libc.SOCK.CLOEXEC; + if (libc.socketpair(libc.AF.UNIX, sock_type, 0, sv) != 0) return false; + if (comptime builtin.os.tag.isDarwin()) { + setCloexec(sv[0]); + // The child dup2s this onto 0 and 1, and dup2 CLEARS close-on-exec on + // the copy, so the server still gets the socket; this marks only the + // number itself, which the child's 3..1024 sweep closes anyway. + setCloexec(sv[1]); + const one: c_int = 1; + _ = libc.setsockopt(sv[0], libc.SOL.SOCKET, libc.SO.NOSIGPIPE, @ptrCast(&one), @sizeOf(c_int)); + } + return true; +} + +// ----------------------------------------------------------- the connection + +/// Where the project starts: helix's find_root rule. Walk UP from `dir`; the +/// TOP-MOST directory holding a language marker wins (a cargo workspace's +/// root Cargo.toml beats the member crate's), the CLOSEST `.git` is the +/// fallback, the asking directory the last resort. +fn rootOf(dir: []const u8, markers: []const []const u8) []const u8 { + if (dir.len == 0 or dir[0] != '/') return "/"; + var top_marker: ?[]const u8 = null; + var git: ?[]const u8 = null; + var d = dir; + var buf: [4096:0]u8 = undefined; + while (true) { + for (markers) |marker| { + const p = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d, marker }, 0) catch continue; + if (libc.access(p.ptr, libc.F_OK) == 0) { + top_marker = d; + break; + } + } + if (git == null) { + if (std.fmt.bufPrintSentinel(&buf, "{s}/.git", .{d}, 0)) |p| { + if (libc.access(p.ptr, libc.F_OK) == 0) git = d; + } else |_| {} + } + d = std.Io.Dir.path.dirname(d) orelse break; + if (d.len <= 1) break; + } + return top_marker orelse git orelse dir; } -// Darwin needs fcntl after socketpair; another concurrent fork can inherit the pair in that window. -fn transportPair(sv: *[2]libc.fd_t) bool { - const sock_type = if (comptime builtin.os.tag.isDarwin()) - libc.SOCK.STREAM - else - libc.SOCK.STREAM | libc.SOCK.CLOEXEC; - if (libc.socketpair(libc.AF.UNIX, sock_type, 0, sv) != 0) return false; - if (comptime builtin.os.tag.isDarwin()) { - setCloexec(sv[0]); - // The child dup2s this onto 0 and 1, and dup2 CLEARS close-on-exec on - // the copy, so the server still gets the socket; this marks only the - // number itself, which the child's 3..1024 sweep closes anyway. - setCloexec(sv[1]); - const one: c_int = 1; - _ = libc.setsockopt(sv[0], libc.SOL.SOCKET, libc.SO.NOSIGPIPE, @ptrCast(&one), @sizeOf(c_int)); +/// The spec's binary: the env override when set, else `bin`, PATH-searched +/// unless it names a path. +fn binOf(spec: *const Spec, buf: *[4096:0]u8) ?[:0]const u8 { + const name = if (libc.getenv(spec.env)) |v| std.mem.span(v) else spec.bin; + if (name.len == 0) return null; + if (std.mem.indexOfScalar(u8, name, '/') != null) { + const z = std.fmt.bufPrintSentinel(buf, "{s}", .{name}, 0) catch return null; + return if (libc.access(z.ptr, libc.X_OK) == 0) z else null; } - return true; + const path = if (libc.getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin"; + var it = std.mem.tokenizeScalar(u8, path, ':'); + while (it.next()) |dir| { + const cand = std.fmt.bufPrintSentinel(buf, "{s}/{s}", .{ dir, name }, 0) catch continue; + if (libc.access(cand.ptr, libc.X_OK) == 0) return cand; + } + return null; } -// ----------------------------------------------------------- the connection - /// Spawn-or-return, and tell an existing server about a new project root. /// Returns the generation the caller's whole query is bound to. fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, tr: *Trace) Err!u32 { @@ -1892,46 +2106,6 @@ fn provider(v: ?std.json.Value) bool { }; } -/// didOpen the first time a file is seen, didChange when its bytes moved, -/// nothing when they did not — the common case between two presses of gd. -fn syncDoc(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, uri: []const u8, src: []const u8) Err!void { - const hash = std.hash.Wyhash.hash(0, src); - var doc: ?*Doc = null; - for (c.docs.items) |*d| if (std.mem.eql(u8, d.uri, uri)) { - doc = d; - break; - }; - if (doc) |d| if (d.hash == hash) return; - - var b: std.ArrayList(u8) = .empty; - if (doc) |d| { - d.version += 1; - d.hash = hash; - d.stale = true; - try b.print(arena, "{{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didChange\",\"params\":{{\"textDocument\":{{\"uri\":", .{}); - try jstr(&b, arena, uri); - try b.print(arena, ",\"version\":{d}}},\"contentChanges\":[{{\"text\":", .{d.version}); - try jstr(&b, arena, src); - try b.appendSlice(arena, "}]}}"); - } else { - if (c.docs.items.len >= 256) return; // a session does not open this many - const owned = try sa.dupe(u8, uri); - c.docs.append(sa, .{ .uri = owned, .version = 1, .hash = hash }) catch { - sa.free(owned); - return error.OutOfMemory; - }; - try b.appendSlice(arena, "{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didOpen\",\"params\":{\"textDocument\":{\"uri\":"); - try jstr(&b, arena, uri); - try b.print(arena, ",\"languageId\":\"{s}\",\"version\":1,\"text\":", .{specs[si].lang}); - try jstr(&b, arena, src); - try b.appendSlice(arena, "}}}"); - } - // A server busy with what it was sent last reads no more meanwhile - // (clangd parses its compile database at a first open): this waits as - // an answer does, for a cancel or a server that has stopped. - try send(c, io, b.items, nowMs() + wedge_ms); -} - /// Send a request and wait on the mailbox. Returns the parsed `result`, or /// null for an error response / null result — both are legal "no answer". /// Runs with the conn mutex held; the mutex is released inside the wait. @@ -2378,83 +2552,6 @@ fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method // ------------------------------------------------------------- introspection -fn status(io: std.Io, path: []const u8, out: *std.Io.Writer) !void { - // The index the file would ask first, what it holds and how old it is. - const dir = std.Io.Dir.path.dirname(path) orelse "/"; - if (Scip.find(io, dir)) |found| if (found) |ix| { - var symbols: usize = 0; - for (ix.infos.items) |info| symbols += @intFromBool(info.len > 0); - var newer: usize = 0; - var pbuf: [4096]u8 = undefined; - for (ix.documents) |d| newer += @intFromBool(ix.stale(io, std.fmt.bufPrint(&pbuf, "{s}/{s}", .{ ix.root(), d.path }) catch continue)); - try out.print("scip: {s}, {d} KB, {d} documents, {d} symbols", .{ ix.path(), (ix.size + 999) / 1000, ix.documents.len, symbols }); - if (!hideTime()) { - const secs: std.time.epoch.EpochSeconds = .{ .secs = @intCast(@divTrunc(ix.mtime, std.time.ns_per_s)) }; - const day = secs.getEpochDay().calculateYearDay(); - const md = day.calculateMonthDay(); - try out.print(", indexed {d}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} UTC", .{ day.year, md.month.numeric(), md.day_index + 1, secs.getDaySeconds().getHoursIntoDay(), secs.getDaySeconds().getMinutesIntoHour() }); - } - try out.print(", {d} files newer\n", .{newer}); - } else { - try out.print("scip: no index.scip above {s}", .{dir}); - if (specFor(path)) |si| try out.print("; Index runs {s}", .{if (libc.getenv(indexer_envs[si])) |v| std.mem.span(v) else specs[si].indexer}); - try out.writeByte('\n'); - } else |e| try out.print("scip: index.scip above {s}: {s}\n", .{ dir, @errorName(e) }); - try out.print("language servers:\n", .{}); - for (&specs, 0..) |*s, i| { - const c = &conns[i]; - c.mu.lockUncancelable(io); - const state = c.state; - var rootbuf: [256]u8 = undefined; - const rootlen = @min(c.root.len, rootbuf.len); - @memcpy(rootbuf[0..rootlen], c.root[0..rootlen]); - const root = rootbuf[0..rootlen]; - const enc = c.caps.enc; - const ndocs = c.docs.items.len; - const ndiag = c.diags.items.len; - const pull = c.caps.pull; - const chier = c.caps.call_hier; - const thier = c.caps.type_hier; - c.mu.unlock(io); - const env_v = if (libc.getenv(s.env)) |v| std.mem.span(v) else null; - // Never spawned is not off: the first query of its files starts it. - try out.print(" {s:<28} {s}", .{ s.name, if (state == .off) "not started yet (its first query starts it)" else @tagName(state) }); - if (state == .ready or state == .starting) { - try out.print(" root {s} {s}", .{ root, @tagName(enc) }); - if (pull) try out.print(" pull-diags", .{}); - if (chier) try out.print(" call-hier", .{}); - if (thier) try out.print(" type-hier", .{}); - try out.print(" docs {d} diag-files {d}", .{ ndocs, ndiag }); - } - if (env_v) |v| try out.print(" [{s}={s}]", .{ s.env, if (v.len == 0) "(disabled)" else v }); - try out.print("\n ", .{}); - for (s.exts) |e| try out.print("{s} ", .{e}); - try out.print("\n", .{}); - } - - while (!log_mu.tryLock()) std.atomic.spinLoopHint(); - defer log_mu.unlock(); - try out.print("\nclient queries ({d} total, keeping {d}):\n", .{ log_total, log_cap }); - var shown: usize = 0; - for (0..log_cap) |i| { - const e = &log_buf[(log_next + i) % log_cap]; - if (!e.used) continue; - shown += 1; - if (hideTime()) { - try out.print(" {s:<18} {s:<14} {s:<20} {d:>4} row(s){s}{s}\n", .{ - @tagName(e.kind), sourceName(e.spec), e.file[0..e.file_len], e.rows, - if (e.err_len > 0) " ERROR " else "", e.err[0..e.err_len], - }); - } else { - try out.print(" {s:<18} {s:<14} {s:<20} {d:>7}us {d:>4} row(s){s}{s}\n", .{ - @tagName(e.kind), sourceName(e.spec), e.file[0..e.file_len], e.us, e.rows, - if (e.err_len > 0) " ERROR " else "", e.err[0..e.err_len], - }); - } - } - if (shown == 0) try out.print(" (none yet — press gd in a file with a server, then ask again)\n", .{}); -} - // ------------------------------------------------------------------ plumbing /// `"textDocument":{"uri":U}` — the params every request about a file @@ -2572,59 +2669,6 @@ fn nowMs() i64 { return @as(i64, @intCast(ts.sec)) * 1000 + @divTrunc(@as(i64, @intCast(ts.nsec)), 1_000_000); } -fn nowUs() u64 { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.MONOTONIC, &ts); - return @as(u64, @intCast(ts.sec)) *| 1_000_000 +| @as(u64, @intCast(ts.nsec)) / 1000; -} - -/// The spec's binary: the env override when set, else `bin`, PATH-searched -/// unless it names a path. -fn binOf(spec: *const Spec, buf: *[4096:0]u8) ?[:0]const u8 { - const name = if (libc.getenv(spec.env)) |v| std.mem.span(v) else spec.bin; - if (name.len == 0) return null; - if (std.mem.indexOfScalar(u8, name, '/') != null) { - const z = std.fmt.bufPrintSentinel(buf, "{s}", .{name}, 0) catch return null; - return if (libc.access(z.ptr, libc.X_OK) == 0) z else null; - } - const path = if (libc.getenv("PATH")) |p| std.mem.span(p) else "/usr/local/bin:/usr/bin:/bin"; - var it = std.mem.tokenizeScalar(u8, path, ':'); - while (it.next()) |dir| { - const cand = std.fmt.bufPrintSentinel(buf, "{s}/{s}", .{ dir, name }, 0) catch continue; - if (libc.access(cand.ptr, libc.X_OK) == 0) return cand; - } - return null; -} - -/// Where the project starts: helix's find_root rule. Walk UP from `dir`; the -/// TOP-MOST directory holding a language marker wins (a cargo workspace's -/// root Cargo.toml beats the member crate's), the CLOSEST `.git` is the -/// fallback, the asking directory the last resort. -fn rootOf(dir: []const u8, markers: []const []const u8) []const u8 { - if (dir.len == 0 or dir[0] != '/') return "/"; - var top_marker: ?[]const u8 = null; - var git: ?[]const u8 = null; - var d = dir; - var buf: [4096:0]u8 = undefined; - while (true) { - for (markers) |marker| { - const p = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d, marker }, 0) catch continue; - if (libc.access(p.ptr, libc.F_OK) == 0) { - top_marker = d; - break; - } - } - if (git == null) { - if (std.fmt.bufPrintSentinel(&buf, "{s}/.git", .{d}, 0)) |p| { - if (libc.access(p.ptr, libc.F_OK) == 0) git = d; - } else |_| {} - } - d = std.Io.Dir.path.dirname(d) orelse break; - if (d.len <= 1) break; - } - return top_marker orelse git orelse dir; -} - /// A path as a `file://` URI, raw (no JSON quotes). Everything outside the /// unreserved set is percent-encoded; `/` stays a separator. fn uriOf(b: *std.ArrayList(u8), arena: std.mem.Allocator, path: []const u8) Err!void { @@ -2638,33 +2682,6 @@ fn uriOf(b: *std.ArrayList(u8), arena: std.mem.Allocator, path: []const u8) Err! } } -/// `file:///a/b%20c` -> `/a/b c`. Rows carry real paths; look.zig opens them -/// and `%20` is not a filename. A decoded CONTROL byte rejects the whole uri: -/// a `%0A` in a path would split one location into two physical rows — a -/// server-forged extra row in the results buffer — and no path worth opening -/// has a newline, tab or NUL in it. -fn pathOf(arena: std.mem.Allocator, uri: []const u8) ?[]const u8 { - var rest = uri; - if (std.mem.startsWith(u8, rest, "file://")) { - rest = rest["file://".len..]; - if (rest.len > 0 and rest[0] != '/') return null; // an authority we cannot open - } else if (std.mem.indexOf(u8, rest, "://") != null) return null; - var out: std.ArrayList(u8) = .empty; - var i: usize = 0; - while (i < rest.len) { - var byte: u8 = rest[i]; - if (rest[i] == '%' and i + 2 < rest.len) { - if (std.fmt.parseInt(u8, rest[i + 1 .. i + 3], 16)) |b| { - byte = b; - i += 3; - } else |_| i += 1; - } else i += 1; - if (byte < 0x20) return null; - out.append(arena, byte) catch return null; - } - return out.items; -} - /// A JSON string. Invalid UTF-8 becomes `?` one byte at a time rather than /// U+FFFD: a buffer being typed into may be invalid mid-keystroke, and a /// replacement that changed byte lengths would move every offset after it. @@ -2706,23 +2723,6 @@ fn jstr(b: *std.ArrayList(u8), arena: std.mem.Allocator, s: []const u8) Err!void const Pos = struct { line: u32, ch: u32 }; -/// Byte offset -> LSP position. `lsp.lineCol` gives the line and the BYTE -/// column; only the column needs re-counting, and only when the server -/// refused utf-8. -fn posOf(src: []const u8, off: u32, enc: Enc) Pos { - const lc = lsp.lineCol(src, off); - if (enc == .utf8) return .{ .line = @intCast(lc.line), .ch = @intCast(lc.col) }; - const bol = @min(off, src.len) - lc.col; - var units: u32 = 0; - var i: usize = bol; - while (i < bol + lc.col) { - const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1; - units += if (l == 4 and enc == .utf16) 2 else 1; - i += l; - } - return .{ .line = @intCast(lc.line), .ch = units }; -} - // std.json.Value navigation: optional-in, optional-out, so a missing field // and a wrongly-typed one read the same and neither can panic on a server // that sends the unexpected. diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 173a559b..a2db0abc 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -30,12 +30,6 @@ const regexp = @import("regexp.zig"); const addr_lang = @import("ninep/addr.zig"); const modal = @import("modal.zig"); -/// The next place after `p` a search may start: a rune on, and past the -/// text's end once there, which ends a loop over `p <= end`. -fn stepRune(text: []const u8, p: usize) usize { - return if (p >= text.len) p + 1 else modal.nextRune(text, p); -} - pub const Range = addr_lang.Range; /// A change to the text as it was: `job`, when set, is a `<` or `|` whose @@ -564,6 +558,12 @@ const Parser = struct { const e_noname = "no file name given"; +/// The next place after `p` a search may start: a rune on, and past the +/// text's end once there, which ends a loop over `p <= end`. +fn stepRune(text: []const u8, p: usize) usize { + return if (p >= text.len) p + 1 else modal.nextRune(text, p); +} + const Exec = struct { arena: std.mem.Allocator, why: *Why, -- cgit v1.3