From a864ef66736d3cfbdffed8b98a3c46be7985db74 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 12:40:11 -0300 Subject: idioms #13, the language-server client: Cx reads a file through the query's io (Io.Dir, Io.File) rather than libc open and read, notification dispatches on a stringToEnum switch, a status post too long is cut rather than dropped, failure is ?Err not ?anyerror, Rename's other files are tried rather than lost on OOM, Ask's messages are proven to fit (its buffer 160), its @put/@edit parse is a switch, query names Err, and std.fs.path is std.Io.Dir.path Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/lsp/Ask.zig | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) (limited to 'src/lsp/Ask.zig') diff --git a/src/lsp/Ask.zig b/src/lsp/Ask.zig index 8ed0aecc..76c32f2d 100644 --- a/src/lsp/Ask.zig +++ b/src/lsp/Ask.zig @@ -45,22 +45,22 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) // file, place and argument -- from wherever it is run. const last_pane: ?usize = if (kind == .explain) (if (p.lsp_ask.last) |last| p.paneBySerial(last.serial) else null) else null; const id = last_pane orelse asked; - var said: [128]u8 = undefined; + var said: [160]u8 = undefined; if (!lsp.supports.contains(kind)) - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: no language server here answers it", .{lspWord(kind)}) catch "no language server answers it"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: no language server here answers it", .{lspWord(kind)}) catch unreachable); // a word and 45 fit 160 const pane = p.panes[id] orelse return; if (kind != .status) { const f = pane.file orelse - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only a file's pane asks a language server", .{lspWord(kind)}) catch "only a file's pane asks"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only a file's pane asks a language server", .{lspWord(kind)}) catch unreachable); // a word and 45 fit 160 if (f.output != null) - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only a file's pane asks a language server", .{lspWord(kind)}) catch "only a file's pane asks"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: only a file's pane asks a language server", .{lspWord(kind)}) catch unreachable); // a word and 45 fit 160 } - if (arg.len > 128) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: its argument is at most 128 bytes", .{lspWord(kind)}) catch "argument too long"); + if (arg.len > 128) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: its argument is at most 128 bytes", .{lspWord(kind)}) catch unreachable); // a word and 45 fit 160 if (kind == .rename) { const zig_buf = if (pane.file) |f| std.mem.endsWith(u8, f.path, ".zig") else true; if ((zig_buf and (!std.zig.isValidId(arg) or std.zig.isUnderscore(arg))) or (!zig_buf and std.mem.indexOfAny(u8, arg, " \t\r\n\"\\") != null)) - return p.reportFailure(id, std.fmt.bufPrint(&said, "Rename: {s} is no name", .{arg}) catch "Rename: no name"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "Rename: {s} is no name", .{arg}) catch unreachable); // arg is 128 at most } var off = if (pane.file) |f| modal.offsetAt(f.content, .{ .row = @intCast(@max(0, pane.body.cur_row)), @@ -83,7 +83,7 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) } } off = nearest orelse - return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: no {s} in this file", .{ lspWord(kind), arg }) catch "no such symbol in this file"); + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: no {s} in this file", .{ lspWord(kind), arg }) catch unreachable); // a word, 128 and 18 fit 160 arg = ""; } // Lspwhy asks the last query again, narrated: its kind and argument, @@ -93,7 +93,7 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) var at = off; if (kind == .explain) { if (p.lsp_ask.last) |last| { - sent = std.fmt.bufPrint(&explained, "{s} {s}", .{ @tagName(last.kind), last.arg.slice() }) catch ""; + sent = std.fmt.bufPrint(&explained, "{s} {s}", .{ @tagName(last.kind), last.arg.slice() }) catch unreachable; // a kind's name and 128 fit 160 if (last_pane != null) at = last.offset; } } else if (kind != .status) p.lsp_ask.last = .{ .kind = kind, .arg = .from(arg), .serial = pane.serial, .offset = @intCast(off) }; @@ -135,12 +135,10 @@ fn parseLspEdits(p: *Pardes, bytes: []const u8, fallback: ?[]const u8) ?[]LspEdi } var fields = std.mem.tokenizeScalar(u8, line, ' '); const head = fields.next() orelse return null; - const want: @TypeOf(form) = if (std.mem.eql(u8, head, "@put")) - .put - else if (std.mem.eql(u8, head, "@edit")) - .edit - else - return null; + const want: @TypeOf(form) = switch (std.meta.stringToEnum(enum { @"@put", @"@edit" }, head) orelse return null) { + .@"@put" => .put, + .@"@edit" => .edit, + }; if (form == .undecided) form = want; if (form != want) return null; const start = std.fmt.parseInt(usize, fields.next() orelse return null, 10) catch return null; @@ -277,11 +275,11 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { defer if (p.fs.failures != failures) p.noteLateFailure(w.pane); var said: [256]u8 = undefined; const rows = response orelse - return p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: the language server did not answer", .{lspWord(w.kind)}) catch "no answer"); + return p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: the language server did not answer", .{lspWord(w.kind)}) catch unreachable); // short // A backend that cannot answer it says why (`@none `). if (std.mem.startsWith(u8, rows, "@none ")) { const why = std.mem.trimEnd(u8, rows["@none ".len..], "\n"); - return p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: {s}", .{ lspWord(w.kind), why[0..@min(why.len, 200)] }) catch "no answer"); + return p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: {s}", .{ lspWord(w.kind), why[0..@min(why.len, 200)] }) catch unreachable); // 200 fit 256 } // Index's answer is its indexer's command line, run in a command // pane (lsp_client.indexCommand). @@ -295,7 +293,7 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { if (applyLspEdits(p, pane, w.revision, edits)) { var buf: [64]u8 = undefined; const verb: []const u8 = if (w.kind == .rename) "renamed" else "formatted"; - p.setMessage(w.pane, std.fmt.bufPrint(&buf, "{s} {d} range(s)", .{ verb, edits.len }) catch verb); + p.setMessage(w.pane, std.fmt.bufPrint(&buf, "{s} {d} range(s)", .{ verb, edits.len }) catch unreachable); // short } return; } @@ -313,13 +311,13 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { const none: ?[]const u8 = switch (w.kind) { .completion, .format => null, .rename => "nothing here to rename", - .diagnostics, .workspace_diagnostics => return p.setMessage(w.pane, std.fmt.bufPrint(&said, "{s}: none", .{lspWord(w.kind)}) catch "none"), + .diagnostics, .workspace_diagnostics => return p.setMessage(w.pane, std.fmt.bufPrint(&said, "{s}: none", .{lspWord(w.kind)}) catch unreachable), // short .incoming_calls => "callers not found", .outgoing_calls => "callees not found", .hover => "nothing to show: not found", else => "not found", }; - if (none) |text| p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: {s}", .{ lspWord(w.kind), text }) catch "nothing found"); + if (none) |text| p.reportFailure(w.pane, std.fmt.bufPrint(&said, "{s}: {s}", .{ lspWord(w.kind), text }) catch unreachable); // short literals return; } @@ -350,7 +348,7 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { }; if (listing and !p.look_missed) { var where: [300]u8 = undefined; - p.setMessage(v.active, std.fmt.bufPrint(&where, "{s}: the one, {s}", .{ lspWord(w.kind), place[0..@min(place.len, 240)] }) catch lspWord(w.kind)); + p.setMessage(v.active, std.fmt.bufPrint(&where, "{s}: the one, {s}", .{ lspWord(w.kind), place[0..@min(place.len, 240)] }) catch unreachable); // 240 fit 300 } return; } @@ -371,7 +369,7 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { return; }; const dir = if (pane.file) |f| panes.Dir.ofName(f.path) else pane.cwdSlice(); - const content = p.gpa.dupe(u8, rows) catch return; + const content = p.gpa.dupe(u8, rows) catch |err| return p.reportError(w.pane, lspWord(w.kind), err); const serial_before = p.next_serial; p.fs.refilled = null; panes.Output.fillResults(p, w.pane, dir, from, w.arg.slice(), content, null) catch |err| { @@ -401,9 +399,9 @@ pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void { // renameEdits) are no edits. const unrenamed = std.mem.count(u8, rows, ", which the server did not rename\n"); p.setMessage(w.pane, if (unrenamed > 0) - std.fmt.bufPrint(&said_buf, "Rename: previewed, not applied: the server renamed in this file only, and {d} other open file(s) say the name", .{unrenamed}) catch "Rename: previewed" + std.fmt.bufPrint(&said_buf, "Rename: previewed, not applied: the server renamed in this file only, and {d} other open file(s) say the name", .{unrenamed}) catch unreachable // 126 at most else - std.fmt.bufPrint(&said_buf, "Rename: {d} edit(s) across files, previewed, not applied", .{nrows}) catch "Rename: previewed"); + std.fmt.bufPrint(&said_buf, "Rename: {d} edit(s) across files, previewed, not applied", .{nrows}) catch unreachable); // short } if (panes.Output.traits(from).jumps) { const result = p.panes[pane.search_pane orelse return] orelse return; -- cgit v1.3