diff options
Diffstat (limited to 'src/lsp/lsp.zig')
| -rw-r--r-- | src/lsp/lsp.zig | 113 |
1 files changed, 93 insertions, 20 deletions
diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig index 9dd1da5a..1263b583 100644 --- a/src/lsp/lsp.zig +++ b/src/lsp/lsp.zig @@ -16,6 +16,7 @@ pub const Kind = enum { format, select_refs, completion, + callstack, incoming_calls, outgoing_calls, supertypes, @@ -24,49 +25,110 @@ pub const Kind = enum { explain, }; +pub const callstack_match = "@callstack-match "; +/// The last row of a Callstack page that stopped full: `<path> <callers>`, +/// the entry indices from the top the next page starts at (`3.0.17`), and +/// how many callers this page holds. +pub const callstack_more = "@callstack-more "; +pub const CallstackCorners = enum { rounded, square }; +pub const callstack_default_depth: u8 = 8; +pub const callstack_max_depth: u8 = 64; + pub const Req = struct { + callstack_depth: u8 = callstack_default_depth, + callstack_corners: CallstackCorners = .rounded, kind: Kind, path: []const u8, source: [:0]const u8, offset: u32, arg: []const u8 = "", root: []const u8 = "", + /// The other files open in the editor, for a query whose answer can + /// reach them (a rename, references): the server is told of each first, + /// or it answers from what it has seen and misses the rest. + others: []const Doc = &.{}, + /// For `explain`: the query it narrates, read from `arg` (`<kind> + /// <arg>`) by `explaining`; a definition when none was asked before. + explains: Kind = .definition, + /// The fixed region `arena` allocates from, when it is one: a walk that + /// can grow (Callstack) reads how much is left and stops in time. + budget: ?*const std.heap.FixedBufferAllocator = null, }; -const backends = if (@import("pardes_config").zls_backend) - .{ @import("lsp_zls.zig"), @import("lsp_client.zig") } -else - .{}; +pub const Doc = struct { path: []const u8, source: [:0]const u8 }; + +/// Whether a query's answer can name places in other files, so every open +/// file of the server's language is synced before it is asked. +pub fn reachesOtherFiles(kind: Kind) bool { + return switch (kind) { + .rename, .references, .implementation, .workspace_symbols, .callstack, .incoming_calls, .outgoing_calls, .supertypes, .subtypes => true, + else => false, + }; +} -pub const backend_name = if (backends.len > 1) "zls-inproc+lsp-client" else "zls-inproc"; +/// An `explain` request made the one it narrates: `explains` and `arg` set +/// from its `<kind> <arg>`; any other request as it is. +pub fn explaining(req: Req) Req { + if (req.kind != .explain) return req; + var r = req; + const sp = std.mem.indexOfScalar(u8, req.arg, ' ') orelse req.arg.len; + const named = std.meta.stringToEnum(Kind, req.arg[0..sp]) orelse .definition; + r.explains = if (named == .explain or named == .status) .definition else named; + r.arg = if (sp < req.arg.len) req.arg[sp + 1 ..] else ""; + return r; +} + +test "an explain request narrates the query named in its arg, a definition by default" { + const base: Req = .{ .kind = .explain, .path = "/x.zig", .source = "", .offset = 0 }; + var r = base; + r.arg = "hover "; + try std.testing.expectEqual(Kind.hover, explaining(r).explains); + r.arg = "rename newName"; + try std.testing.expectEqual(Kind.rename, explaining(r).explains); + try std.testing.expectEqualStrings("newName", explaining(r).arg); + try std.testing.expectEqual(Kind.definition, explaining(base).explains); +} -pub const supports: std.EnumSet(Kind) = blk: { - var s: std.EnumSet(Kind) = .initEmpty(); - for (0..backends.len) |i| s.setUnion(backends[i].supports); - break :blk s; +/// Every language, Zig's (zls) included, through the protocol client, which +/// runs its server as a child process. None on the web or the board. +const has_client = switch (@import("pardes_config").platform) { + .web, .esp32p4 => false, + else => true, }; +const client = if (has_client) @import("lsp_client.zig") else void; + +pub const backend_name = "lsp-client"; + +pub const supports: std.EnumSet(Kind) = if (has_client) client.supports else .empty; pub fn speaks(path: []const u8) bool { - inline for (backends) |b| if (b.speaks(path)) return true; - return false; + return has_client and client.speaks(path); } -pub fn query(gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { +pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { if (req.kind == .status) { - inline for (backends) |b| try b.query(gpa, arena, req, out); + try out.print("backend: {s}\n", .{backend_name}); + if (has_client) try client.query(io, gpa, arena, req, out); return; } - inline for (backends) |b| { - if (b.speaks(req.path) and b.supports.contains(req.kind)) - return b.query(gpa, arena, req, out); - } - if (req.kind == .explain and backends.len > 0) - try backends[0].query(gpa, arena, req, out); + if (has_client and (req.kind == .explain or (client.speaks(req.path) and client.supports.contains(req.kind)))) + return client.query(io, gpa, arena, req, out); + // Nothing asked: said why, where an empty answer would read as nothing + // found. + if (speaks(req.path)) + return none(out, "the language server for this file does not answer it", .{}); + try none(out, "no language server for this file", .{}); +} + +/// The one line a backend answers with when it cannot answer at all, and +/// why: the core says it rather than "nothing found". +pub fn none(out: *std.Io.Writer, comptime fmt: []const u8, args: anytype) std.Io.Writer.Error!void { + try out.print("@none " ++ fmt ++ "\n", args); } // Called on server reader threads; the sink must copy text before returning. pub fn setStatusSink(ctx: ?*anyopaque, cb: ?*const fn (ctx: ?*anyopaque, text: []const u8) void) void { - if (@import("pardes_config").zls_backend) backends[1].setStatusSink(ctx, cb); + if (has_client) client.setStatusSink(ctx, cb); } pub fn rel(base: []const u8, path: []const u8) []const u8 { @@ -146,3 +208,14 @@ test "LSP encoders report every insufficient output capacity" { } } } + +test "a question no backend answers for a file says why rather than find nothing" { + if (!has_client) return; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var buf: [256]u8 = undefined; + var out: std.Io.Writer = .fixed(&buf); + // A file no server speaks for is answered, not left empty. + try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out); + try std.testing.expectStringStartsWith(out.buffered(), "@none "); +} |
