summaryrefslogtreecommitdiff
path: root/src/lsp/lsp_client.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/lsp/lsp_client.zig')
-rw-r--r--src/lsp/lsp_client.zig426
1 files changed, 347 insertions, 79 deletions
diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig
index 40c287b0..928d5475 100644
--- a/src/lsp/lsp_client.zig
+++ b/src/lsp/lsp_client.zig
@@ -50,6 +50,7 @@ const std = @import("std");
const builtin = @import("builtin");
const libc = std.c;
const lsp = @import("lsp.zig");
+const Scip = @import("Scip.zig");
extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn usleep(usec: c_uint) c_int;
@@ -81,6 +82,17 @@ const Spec = struct {
/// empty ("") to disable the spec entirely. How the snapshot harness pins
/// a deterministic mock server, and how a user points at a custom build.
env: [:0]const u8,
+ /// The command that writes the language's `index.scip` at the project
+ /// root (Scip.zig), which the Index word runs there; `PARDES_SCIP_*`
+ /// (the env's language) names another.
+ indexer: []const u8,
+};
+
+/// Each spec's indexer override, `PARDES_SCIP_RS` beside `PARDES_LSP_RS`.
+const indexer_envs = blk: {
+ var names: [specs.len][:0]const u8 = undefined;
+ for (specs, &names) |spec, *name| name.* = "PARDES_SCIP_" ++ spec.env["PARDES_LSP_".len..];
+ break :blk names;
};
pub const specs = [_]Spec{
@@ -91,6 +103,7 @@ pub const specs = [_]Spec{
.exts = &.{".zig"},
.markers = &.{ "build.zig", "build.zig.zon" },
.env = "PARDES_LSP_ZIG",
+ .indexer = "scip-zig",
},
.{
.name = "rust-analyzer",
@@ -99,6 +112,7 @@ pub const specs = [_]Spec{
.exts = &.{".rs"},
.markers = &.{ "Cargo.toml", "rust-project.json" },
.env = "PARDES_LSP_RS",
+ .indexer = "rust-analyzer scip .",
},
.{
.name = "clangd",
@@ -107,6 +121,7 @@ pub const specs = [_]Spec{
.exts = &.{ ".c", ".h", ".cc", ".cpp", ".hpp", ".cxx", ".hxx" },
.markers = &.{ "compile_commands.json", "compile_flags.txt", ".clangd" },
.env = "PARDES_LSP_C",
+ .indexer = "scip-clang",
},
.{
.name = "gopls",
@@ -115,6 +130,7 @@ pub const specs = [_]Spec{
.exts = &.{".go"},
.markers = &.{ "go.mod", "go.work" },
.env = "PARDES_LSP_GO",
+ .indexer = "scip-go",
},
.{
.name = "typescript-language-server",
@@ -124,6 +140,7 @@ pub const specs = [_]Spec{
.exts = &.{ ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs" },
.markers = &.{ "tsconfig.json", "jsconfig.json", "package.json" },
.env = "PARDES_LSP_TS",
+ .indexer = "scip-typescript index",
},
.{
.name = "pyright",
@@ -133,6 +150,7 @@ pub const specs = [_]Spec{
.exts = &.{".py"},
.markers = &.{ "pyproject.toml", "setup.py", "requirements.txt" },
.env = "PARDES_LSP_PY",
+ .indexer = "scip-python index .",
},
};
@@ -200,7 +218,7 @@ const sa = std.heap.page_allocator;
/// Which units `character` counts in — see the header note on believing the
/// server.
-const Enc = enum { utf8, utf16 };
+const Enc = Scip.Enc;
const Doc = struct {
uri: []u8,
@@ -391,6 +409,11 @@ 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();
}
@@ -414,9 +437,27 @@ const Trace = struct {
pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void {
const req = lsp.explaining(asked);
- if (req.kind == .status) return status(io, out);
+ 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);
+
+ // 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,
@@ -426,11 +467,7 @@ pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.
};
tr.note("file {s} -> {s} (languageId {s})", .{ std.fs.path.basename(req.path), specs[si].name, specs[si].lang });
- // 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);
-
- const t0 = nowUs();
+ const t1 = nowUs();
var err_name: []const u8 = "";
var failure: ?anyerror = null;
answer(io, arena, si, req, &scratch, &tr) catch |e| {
@@ -438,7 +475,7 @@ pub fn query(io: std.Io, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.
err_name = @errorName(e);
tr.note("ERROR: {s}", .{err_name});
};
- const us = nowUs() -| t0;
+ const us = nowUs() -| t1;
const rows = std.mem.count(u8, scratch.buffered(), "\n");
record(si, req, us, rows, err_name);
@@ -664,7 +701,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
.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 => unreachable,
+ .status, .explain, .index => unreachable,
}
}
@@ -890,8 +927,8 @@ fn hierarchyResult(c: *Conn, io: std.Io, arena: std.mem.Allocator, item: std.jso
}
const CallTree = struct {
- /// What a node keeps of the server's item: its own copies, so the
- /// response it came in is freed once its subtree is walked.
+ /// What a node keeps of its caller: its own copies, so the response it
+ /// came in is freed once its subtree is walked.
const Call = struct { uri: []const u8, name: []const u8, at: Range };
const Site = struct { range: Range, focus: bool };
const Node = struct {
@@ -902,7 +939,7 @@ const CallTree = struct {
};
nodes: std.ArrayList(Node) = .empty,
roots: std.ArrayList(usize) = .empty,
- path: [lsp.callstack_max_depth]std.json.Value = undefined,
+ path: [lsp.callstack_max_depth]Hop = undefined,
depth_limit: usize = lsp.callstack_default_depth,
limited: bool = false,
out_of_time: bool = false,
@@ -931,21 +968,20 @@ const CallTree = struct {
/// Incoming paths arrive nearest-first. Insert them outermost-first,
/// sharing common caller prefixes and retaining all their call sites.
- fn addPath(tree: *CallTree, arena: std.mem.Allocator, path: []const std.json.Value, cycle: bool) Err!void {
+ fn addPath(tree: *CallTree, arena: std.mem.Allocator, path: []const Hop, cycle: bool) Err!void {
var parent: ?usize = null;
var at = path.len;
while (at > 0) {
at -= 1;
- const entry = path[at];
- const item = get(entry, "from").?;
+ const hop = path[at];
const children = if (parent) |id| tree.nodes.items[id].children.items else tree.roots.items;
const found = for (children) |id| {
- if (sameCall(tree.nodes.items[id].call, item)) break id;
+ if (sameCall(tree.nodes.items[id].call, hop.call)) break id;
} else null;
const depth = path.len - 1 - at;
const id = found orelse blk: {
const id = tree.nodes.items.len;
- try tree.nodes.append(arena, .{ .call = try callOf(arena, item) });
+ try tree.nodes.append(arena, .{ .call = .{ .uri = try arena.dupe(u8, hop.call.uri), .name = try arena.dupe(u8, hop.call.name), .at = hop.call.at } });
tree.rows += 1;
tree.bytes += rowBytes(depth, tree.nodes.items[id].call);
if (parent) |pid| {
@@ -955,8 +991,7 @@ const CallTree = struct {
};
const node = &tree.nodes.items[id];
node.cycle = node.cycle or (cycle and at == 0);
- for (items(get(entry, "fromRanges"))) |range| {
- const parsed = rangeOf(range) orelse continue;
+ for (hop.sites.items) |parsed| {
for (node.ranges.items) |*other| {
if (std.meta.eql(parsed, other.range)) {
other.focus = other.focus or at == 0;
@@ -973,37 +1008,58 @@ const CallTree = struct {
}
};
-fn callOf(arena: std.mem.Allocator, item: std.json.Value) Err!CallTree.Call {
- return .{
- .uri = try arena.dupe(u8, str(get(item, "uri")).?),
- .name = try arena.dupe(u8, str(get(item, "name")) orelse "(unnamed)"),
- .at = rangeOf(get(item, "selectionRange") orelse get(item, "range")).?,
- };
+/// A caller one level up a Callstack: the function, the calls it makes
+/// there, and what its own callers are asked by, the server's item or the
+/// index's symbol (and document).
+const Hop = struct { call: CallTree.Call, sites: std.ArrayList(Range) = .empty, item: ?std.json.Value = null, symbol: u32 = 0, doc: u32 = 0 };
+
+/// Where a Callstack's callers come from: the index, else the server.
+const Calls = union(enum) {
+ index: *Scip.Index,
+ server: struct { c: *Conn, io: std.Io, parse: ?*std.heap.FixedBufferAllocator },
+};
+
+fn sameCall(a: CallTree.Call, b: CallTree.Call) bool {
+ return std.mem.eql(u8, a.uri, b.uri) and std.meta.eql(a.at, b.at);
}
-fn sameCall(a: CallTree.Call, b: std.json.Value) bool {
- const bu = str(get(b, "uri")) orelse return false;
- const br = rangeOf(get(b, "selectionRange") orelse get(b, "range")) orelse return false;
- return std.mem.eql(u8, a.uri, bu) and std.meta.eql(a.at, br);
+/// The server's incoming calls as hops, their strings the response's.
+fn serverHops(a: std.mem.Allocator, incoming: ?std.json.Value) Err![]Hop {
+ var hops: std.ArrayList(Hop) = .empty;
+ for (items(incoming)) |entry| {
+ const from = get(entry, "from") orelse continue;
+ const uri = str(get(from, "uri")) orelse continue;
+ const at = rangeOf(get(from, "selectionRange") orelse get(from, "range")) orelse continue;
+ if (!std.mem.startsWith(u8, uri, "file:")) continue;
+ var hop: Hop = .{ .call = .{ .uri = uri, .name = str(get(from, "name")) orelse "(unnamed)", .at = at }, .item = from };
+ for (items(get(entry, "fromRanges"))) |r| if (rangeOf(r)) |site| try hop.sites.append(a, site);
+ try hops.append(a, hop);
+ }
+ return hops.items;
}
-fn sameItem(a: std.json.Value, b: std.json.Value) bool {
- const au = str(get(a, "uri")) orelse return false;
- const ar = rangeOf(get(a, "selectionRange") orelse get(a, "range")) orelse return false;
- return sameCall(.{ .uri = au, .name = "", .at = ar }, b);
+/// The index's callers of `symbol` as hops: the definition enclosing each
+/// of its references, the references it holds its sites.
+fn indexHops(a: std.mem.Allocator, ix: *Scip.Index, symbol: u32) Err![]Hop {
+ var hops: std.ArrayList(Hop) = .empty;
+ for (ix.of(symbol)) |o| {
+ if (ix.occurrences[o].roles & Scip.role_definition != 0) continue;
+ const def = ix.occurrences[ix.enclosingDefinition(o) orelse continue];
+ const hop = for (hops.items) |*h| {
+ if (h.symbol == def.symbol) break h;
+ } else blk: {
+ try hops.append(a, .{ .call = .{ .uri = try indexUri(a, ix, def.doc), .name = displayName(ix, def.symbol), .at = def.range }, .symbol = def.symbol, .doc = def.doc });
+ break :blk &hops.items[hops.items.len - 1];
+ };
+ 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 validCaller(entry: std.json.Value) bool {
- const from = get(entry, "from") orelse return false;
- const uri = str(get(from, "uri")) orelse return false;
- return std.mem.startsWith(u8, uri, "file:") and
- rangeOf(get(from, "selectionRange") orelse get(from, "range")) != null;
-}
-
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);
@@ -1021,46 +1077,52 @@ fn callstack(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, cx: *Cx,
&.{});
const parse = if (budget != null) parse_fba.allocator() else arena;
for (items(prepared)) |item| {
- 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 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 count = try collectCallers(c, io, arena, if (budget != null) &parse_fba else null, item, incoming, &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, c.caps.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});
+ 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(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.heap.FixedBufferAllocator, focus: std.json.Value, incoming: ?std.json.Value, tree: *CallTree, depth: usize) Err!usize {
+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 (items(incoming)) |entry| {
+ 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;
- if (!validCaller(entry)) continue;
- const from = get(entry, "from").?;
- const cycle = sameItem(focus, from) or for (tree.path[0..depth]) |ancestor| {
- if (sameItem(get(ancestor, "from").?, from)) break true;
+ 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] = entry;
+ tree.path[depth] = hop;
count += 1;
tree.collected += 1;
if (nowMs() - tree.started_ms >= 1000) {
@@ -1071,12 +1133,16 @@ fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.h
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 ancestors = hierarchyResult(c, io, if (parse) |fixed| fixed.allocator() else arena, from, "callHierarchy/incomingCalls", nowMs() + req_ms) catch |e| switch (e) {
+ 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;
@@ -1089,7 +1155,7 @@ fn collectCallers(c: *Conn, io: std.Io, arena: std.mem.Allocator, parse: ?*std.h
},
else => return e,
};
- if (!tree.out_of_time and !tree.out_of_memory) children = try collectCallers(c, io, arena, parse, focus, ancestors, tree, depth + 1);
+ 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);
@@ -1230,6 +1296,184 @@ fn hierText(arena: std.mem.Allocator, item: std.json.Value) []const u8 {
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.fs.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.fs.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.fs.path.basename(req.path)});
+ if (!ix.stale_said) post(si, .always, "{s} is older than {s}: Index makes it again", .{ ix.path(), std.fs.path.basename(req.path) });
+ ix.stale_said = true;
+ }
+ const enc = ix.documents[doc].enc;
+ var cx: Cx = .{ .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);
+ },
+ .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];
+ } 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,
+ }
+ tr.note("<- {d} row(s) from the index", .{cx.rows});
+ return cx.rows > 0;
+}
+
+/// 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);
+ };
+}
+
+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);
+}
+
+/// 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;
+}
+
+/// 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 {
+ if (Scip.field(ix.infos.items[symbol], 6)) |f| return f.bytes;
+ const s = std.mem.trimEnd(u8, ix.symbols.items[symbol], "().#/:!");
+ return s[if (std.mem.lastIndexOfAny(u8, s, " /#.")) |cut| cut + 1 else 0..];
+}
+
+fn indexPath(a: std.mem.Allocator, ix: *const Scip.Index, doc: u32) Err![]const u8 {
+ return std.fmt.allocPrint(a, "{s}/{s}", .{ ix.root(), ix.documents[doc].path });
+}
+
+fn indexUri(a: std.mem.Allocator, ix: *const Scip.Index, doc: u32) Err![]const u8 {
+ var b: std.ArrayList(u8) = .empty;
+ try uriOf(&b, a, try indexPath(a, ix, doc));
+ 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.fs.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.fs.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
@@ -1283,8 +1527,11 @@ fn emitRange(cx: *Cx, enc: Enc, uri: []const u8, range: ?std.json.Value, note: [
}
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);
+}
+
+fn emitPath(cx: *Cx, enc: Enc, path: []const u8, r: Range, note: []const u8) Err!void {
if (cx.rows >= max_rows) return;
- const path = pathOf(cx.arena, uri) orelse return;
if (path.len == 0 or path[0] != '/') return; // rows promise absolute-or-rel-from-base
// Without sources (Callstack) the asking file is still known: its rows'
// columns convert from utf-16 as ever.
@@ -1327,7 +1574,7 @@ fn colBytes(lntext: []const u8, ch: u32, enc: Enc) usize {
var i: usize = 0;
while (i < lntext.len and units < ch) {
const l = std.unicode.utf8ByteSequenceLength(lntext[i]) catch 1;
- units += if (l == 4) 2 else 1;
+ units += if (l == 4 and enc == .utf16) 2 else 1;
i += l;
}
return i;
@@ -1374,7 +1621,7 @@ fn strictOffset(src: []const u8, line: u32, ch: u32, enc: Enc) ?usize {
var i: usize = bol;
while (i < eol and units < ch) {
const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1;
- units += if (l == 4) 2 else 1;
+ units += if (l == 4 and enc == .utf16) 2 else 1;
i += l;
}
if (units < ch) return null;
@@ -2135,7 +2382,28 @@ fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method
// ------------------------------------------------------------- introspection
-fn status(io: std.Io, out: *std.Io.Writer) !void {
+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.fs.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];
@@ -2178,12 +2446,12 @@ fn status(io: std.Io, out: *std.Io.Writer) !void {
shown += 1;
if (hideTime()) {
try out.print(" {s:<18} {s:<14} {s:<20} {d:>4} row(s){s}{s}\n", .{
- @tagName(e.kind), specs[e.spec].name, e.file[0..e.file_len], e.rows,
+ @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), specs[e.spec].name, e.file[0..e.file_len], e.us, e.rows,
+ @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],
});
}
@@ -2453,7 +2721,7 @@ fn posOf(src: []const u8, off: u32, enc: Enc) Pos {
var i: usize = bol;
while (i < bol + lc.col) {
const l = std.unicode.utf8ByteSequenceLength(src[i]) catch 1;
- units += if (l == 4) 2 else 1;
+ units += if (l == 4 and enc == .utf16) 2 else 1;
i += l;
}
return .{ .line = @intCast(lc.line), .ch = units };
@@ -2484,7 +2752,7 @@ fn items(v: ?std.json.Value) []std.json.Value {
return if (o == .array) o.array.items else &.{};
}
-const Range = struct { sl: u32, sc: u32, el: u32, ec: u32 };
+const Range = Scip.Range;
fn rangeOf(range: ?std.json.Value) ?Range {
const st = get(range, "start") orelse return null;
@@ -2513,7 +2781,7 @@ test "caller trees reverse incoming paths and merge shared callers and sites" {
defer arena_state.deinit();
const arena = arena_state.allocator();
const fixture = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"from\":{\"name\":\"left\",\"uri\":\"file:///test.c\",\"selectionRange\":{\"start\":{\"line\":3,\"character\":0},\"end\":{\"line\":3,\"character\":4}}},\"fromRanges\":[{\"start\":{\"line\":3,\"character\":0},\"end\":{\"line\":3,\"character\":4}}]},{\"from\":{\"name\":\"right\",\"uri\":\"file:///test.c\",\"selectionRange\":{\"start\":{\"line\":4,\"character\":0},\"end\":{\"line\":4,\"character\":4}}},\"fromRanges\":[{\"start\":{\"line\":4,\"character\":0},\"end\":{\"line\":4,\"character\":4}}]},{\"from\":{\"name\":\"page\",\"uri\":\"file:///test.c\",\"selectionRange\":{\"start\":{\"line\":1,\"character\":0},\"end\":{\"line\":1,\"character\":4}}},\"fromRanges\":[{\"start\":{\"line\":1,\"character\":0},\"end\":{\"line\":1,\"character\":4}}]},{\"from\":{\"name\":\"page\",\"uri\":\"file:///test.c\",\"selectionRange\":{\"start\":{\"line\":1,\"character\":0},\"end\":{\"line\":1,\"character\":4}}},\"fromRanges\":[{\"start\":{\"line\":2,\"character\":0},\"end\":{\"line\":2,\"character\":4}}]},{\"from\":{\"name\":\"main\",\"uri\":\"file:///test.c\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":4}}},\"fromRanges\":[{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":4}}]}]", .{});
- const entries = items(fixture);
+ const entries = try serverHops(arena, fixture);
var tree: CallTree = .{};
try tree.addPath(arena, &.{ entries[0], entries[2], entries[4] }, false);
try tree.addPath(arena, &.{ entries[1], entries[3], entries[4] }, false);
@@ -2552,9 +2820,9 @@ test "test callers sort after the rest, each group by place" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
- const fixture = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"name\":\"test b\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"z\",\"uri\":\"file:///b.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"x\",\"uri\":\"file:///a_test.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}},{\"name\":\"y\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":5,\"character\":0},\"end\":{\"line\":5,\"character\":1}}}]", .{});
+ const fixture = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"from\":{\"name\":\"test b\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}}},{\"from\":{\"name\":\"z\",\"uri\":\"file:///b.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}}},{\"from\":{\"name\":\"x\",\"uri\":\"file:///a_test.zig\",\"selectionRange\":{\"start\":{\"line\":0,\"character\":0},\"end\":{\"line\":0,\"character\":1}}}},{\"from\":{\"name\":\"y\",\"uri\":\"file:///a.zig\",\"selectionRange\":{\"start\":{\"line\":5,\"character\":0},\"end\":{\"line\":5,\"character\":1}}}}]", .{});
var tree: CallTree = .{};
- for (items(fixture)) |item| try tree.nodes.append(arena, .{ .call = try callOf(arena, item) });
+ for (try serverHops(arena, fixture)) |hop| try tree.nodes.append(arena, .{ .call = hop.call });
var order = [_]usize{ 0, 1, 2, 3 };
std.mem.sort(usize, &order, @as(*const CallTree, &tree), callerLess);
try std.testing.expectEqualSlices(usize, &.{ 3, 1, 0, 2 }, &order);
@@ -2810,7 +3078,7 @@ test "a language with no server to start says so, not that it did not answer" {
test "Lspinfo before any query says a server is not started yet, not off" {
var out: std.Io.Writer.Allocating = .init(std.testing.allocator);
defer out.deinit();
- try status(std.testing.io, &out.writer);
+ try status(std.testing.io, "/x/notes.txt", &out.writer);
try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null);
}