diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:35:18 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-04 23:39:25 -0300 |
| commit | 63e6ceefc7ab33e31c9d4f83387183e44089e443 (patch) | |
| tree | 62a63e4924643680849e4bca46030d536dfa26bb /src/lsp | |
| parent | 383256a741117596931c4e3817491041fde5229d (diff) | |
| download | pardes-63e6ceefc7ab33e31c9d4f83387183e44089e443.tar.gz pardes-63e6ceefc7ab33e31c9d4f83387183e44089e443.zip | |
lsp_client: try in place of 25 catch return error.OutOfMemory into an Err that already holds it, and of app(), which did the same; offsetAt, which only its test called, goes (the test checks posOf against strictOffset); query's gpa and status's req, never read, go, host_io's call and lspbench's two dropping the argument; the notes of the gone ZLS backend go, and renameEdits gets back its doc from above identAt
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/lsp')
| -rw-r--r-- | src/lsp/lsp.zig | 8 | ||||
| -rw-r--r-- | src/lsp/lsp_client.zig | 156 |
2 files changed, 73 insertions, 91 deletions
diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig index 1263b583..88d9cda2 100644 --- a/src/lsp/lsp.zig +++ b/src/lsp/lsp.zig @@ -105,14 +105,14 @@ pub fn speaks(path: []const u8) bool { return has_client and client.speaks(path); } -pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { +pub fn query(io: std.Io, arena: std.mem.Allocator, req: Req, out: *std.Io.Writer) !void { if (req.kind == .status) { try out.print("backend: {s}\n", .{backend_name}); - if (has_client) try client.query(io, gpa, arena, req, out); + if (has_client) try client.query(io, arena, req, out); return; } 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); + return client.query(io, arena, req, out); // Nothing asked: said why, where an empty answer would read as nothing // found. if (speaks(req.path)) @@ -216,6 +216,6 @@ test "a question no backend answers for a file says why rather than find nothing 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 query(std.testing.io, arena.allocator(), .{ .kind = .incoming_calls, .path = "/x/notes.txt", .source = "", .offset = 0 }, &out); try std.testing.expectStringStartsWith(out.buffered(), "@none "); } diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index cc92e00e..e685aa7d 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -396,8 +396,8 @@ fn hideTime() bool { return std.mem.span(v).len != 0; } -/// `SPC l w` narration, same shape as the ZLS backend's: threaded through the -/// real path, so it cannot disagree with what `gd` actually did. +/// `SPC l w` narration, threaded through the real path, so it cannot +/// disagree with what `gd` actually did. const Trace = struct { on: bool = false, buf: [16 * 1024]u8 = undefined, @@ -413,10 +413,9 @@ const Trace = struct { // ------------------------------------------------------------------- query -pub fn query(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, asked: lsp.Req, out: *std.Io.Writer) !void { - _ = gpa; +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, req, out); + if (req.kind == .status) return status(io, out); var tr: Trace = .{ .on = req.kind == .explain }; const si = specFor(req.path) orelse { @@ -564,7 +563,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, else => "textDocument/references", }; var b = try atPos(arena, uri.items, pos); - if (kind == .references) try app(&b, arena, ",\"context\":{\"includeDeclaration\":true}"); + 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)", .{}); @@ -597,7 +596,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, }, .workspace_symbols => { var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"query\":"); + 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| { @@ -609,7 +608,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, .workspace_diagnostics => try workspaceDiagnostics(c, io, arena, &cx, deadline), .rename => { var b = try atPos(arena, uri.items, pos); - try app(&b, arena, ",\"newName\":"); + 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 @@ -623,7 +622,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, }, .format => { var b = try docParams(arena, uri.items); - try app(&b, arena, ",\"options\":{\"tabSize\":4,\"insertSpaces\":true}"); + 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); }, @@ -639,7 +638,7 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, }, .completion => { var b = try atPos(arena, uri.items, pos); - try app(&b, arena, ",\"context\":{\"triggerKind\":1}"); + 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); @@ -649,15 +648,14 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req, const label = str(get(item, "label")) orelse continue; const detail = str(get(item, "detail")) orelse ""; var text: std.ArrayList(u8) = .empty; - try app(&text, arena, label); + try text.appendSlice(arena, label); if (detail.len > 0) { - try app(&text, arena, " "); - try app(&text, arena, flat(arena, detail)); + 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 (unlike the ZLS backend, which points - // at declarations), so the honest place is where it would be - // inserted. n/N still step the list; Enter goes nowhere new. + // 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; } @@ -770,11 +768,6 @@ fn waitFresh(c: *Conn, io: std.Io, uri: []const u8, deadline: i64) error{Cancele } } -/// 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. /// 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 { @@ -801,6 +794,11 @@ fn holdsWord(text: []const u8, word: []const u8) bool { return false; } +/// 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; @@ -814,11 +812,11 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, continue; } const u = str(get(get(dc, "textDocument"), "uri")) orelse continue; - files.append(cx.arena, .{ u, get(dc, "edits") }) catch return error.OutOfMemory; + 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| files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* }) catch return error.OutOfMemory; + 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); @@ -829,7 +827,7 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value, // applying the valid remainder would be a partial edit set const span = byteSpan(src, get(ed, "range"), enc) orelse return; const text = str(get(ed, "newText")) orelse return; - edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }) catch return error.OutOfMemory; + try edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }); } else try emitRange(cx, enc, f[0], get(ed, "range"), flat(cx.arena, str(get(ed, "newText")) orelse "")); } } @@ -865,7 +863,7 @@ fn formatEdits(cx: *Cx, enc: Enc, result: std.json.Value, src: []const u8) Err!v // of a format is not a format const span = byteSpan(src, get(ed, "range"), enc) orelse return; const text = str(get(ed, "newText")) orelse return; - edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }) catch return error.OutOfMemory; + try edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text }); } sortEdits(edits.items); for (edits.items) |ed| try lsp.put(cx.out, ed.start, ed.end, ed.text); @@ -887,8 +885,8 @@ fn sortEdits(edits: []PutEdit) void { fn hierarchyResult(c: *Conn, io: std.Io, arena: std.mem.Allocator, item: std.json.Value, method: []const u8, deadline: i64) Err!?std.json.Value { if (jsonDepth(item, 0) > 96) return null; var body: std.ArrayList(u8) = .empty; - try app(&body, arena, "\"item\":"); - try app(&body, arena, std.json.Stringify.valueAlloc(arena, item, .{}) catch return error.OutOfMemory); + try body.appendSlice(arena, "\"item\":"); + try body.appendSlice(arena, try std.json.Stringify.valueAlloc(arena, item, .{})); return call(c, io, arena, method, body.items, deadline); } @@ -1418,17 +1416,6 @@ fn strictOffset(src: []const u8, line: u32, ch: u32, enc: Enc) ?usize { return i; } -/// The RENDER-path sibling of strictOffset: clamps instead of failing, -/// because a row is presentation, not mutation. -fn offsetAt(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 src.len) + 1; - const eol = std.mem.indexOfScalarPos(u8, src, bol, '\n') orelse src.len; - return bol + @min(colBytes(src[bol..eol], ch, enc), eol - bol); -} - /// 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 { @@ -1491,11 +1478,11 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re }; if (!known and c.caps.folders) { var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "{\"jsonrpc\":\"2.0\",\"method\":\"workspace/didChangeWorkspaceFolders\",\"params\":{\"event\":{\"added\":[{\"name\":\"root\",\"uri\":\""); + try b.appendSlice(arena, "{\"jsonrpc\":\"2.0\",\"method\":\"workspace/didChangeWorkspaceFolders\",\"params\":{\"event\":{\"added\":[{\"name\":\"root\",\"uri\":\""); try uriOf(&b, arena, root); - try app(&b, arena, "\"}],\"removed\":[]}}}"); + try b.appendSlice(arena, "\"}],\"removed\":[]}}}"); try send(c, io, b.items, nowMs() + wedge_ms); - const owned = sa.dupe(u8, root) catch return error.OutOfMemory; + const owned = try sa.dupe(u8, root); c.extra_roots.append(sa, owned) catch sa.free(owned); } } @@ -1525,7 +1512,7 @@ fn ensure(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Re // Owned BEFORE anything spawns: the reader thread is the socket's only // closer once it exists, so nothing fallible may sit between fork and // reader-spawn — an abort there would strand the fd. - const owned_root = sa.dupe(u8, root) catch return error.OutOfMemory; + const owned_root = try sa.dupe(u8, root); var sv: [2]libc.fd_t = undefined; if (!transportPair(&sv)) { @@ -1643,10 +1630,10 @@ fn handshake(c: *Conn, io: std.Io, arena: std.mem.Allocator, root: []const u8) E var b: std.ArrayList(u8) = .empty; try b.print(arena, "\"processId\":{d},\"clientInfo\":{{\"name\":\"pardes\"}},\"rootUri\":\"", .{@as(u32, @bitCast(libc.getpid()))}); try uriOf(&b, arena, root); - try app(&b, arena, "\",\"workspaceFolders\":[{\"name\":\"root\",\"uri\":\""); + try b.appendSlice(arena, "\",\"workspaceFolders\":[{\"name\":\"root\",\"uri\":\""); try uriOf(&b, arena, root); - try app(&b, arena, "\""); - try app(&b, arena, + try b.appendSlice(arena, "\""); + try b.appendSlice(arena, \\}],"capabilities":{"general":{"positionEncodings":["utf-8","utf-16"]}, \\"window":{"workDoneProgress":true}, \\"workspace":{"configuration":true,"workspaceFolders":true,"symbol":{}, @@ -1716,19 +1703,19 @@ fn syncDoc(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, uri: []con try jstr(&b, arena, uri); try b.print(arena, ",\"version\":{d}}},\"contentChanges\":[{{\"text\":", .{d.version}); try jstr(&b, arena, src); - try app(&b, arena, "}]}}"); + try b.appendSlice(arena, "}]}}"); } else { if (c.docs.items.len >= 256) return; // a session does not open this many - const owned = sa.dupe(u8, uri) catch return error.OutOfMemory; + 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 app(&b, arena, "{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didOpen\",\"params\":{\"textDocument\":{\"uri\":"); + 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 app(&b, arena, "}}}"); + 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 @@ -1746,8 +1733,8 @@ fn call(c: *Conn, io: std.Io, arena: std.mem.Allocator, method: []const u8, para var b: std.ArrayList(u8) = .empty; try b.print(arena, "{{\"jsonrpc\":\"2.0\",\"id\":{d},\"method\":\"{s}\",\"params\":{{", .{ id, method }); - try app(&b, arena, params); - try app(&b, arena, "}}"); + try b.appendSlice(arena, params); + try b.appendSlice(arena, "}}"); if (c.resp) |r| sa.free(r); c.resp = null; @@ -2038,23 +2025,23 @@ fn dispatch(c: *Conn, io: std.Io, si: usize, g: u32, sock: c_int, raw: []const u /// the server blocks forever on its own question. fn serverRequest(c: *Conn, io: std.Io, arena: std.mem.Allocator, sock: c_int, method: []const u8, id: std.json.Value, params: ?std.json.Value) void { var b: std.ArrayList(u8) = .empty; - app(&b, arena, "{\"jsonrpc\":\"2.0\",\"id\":") catch return; + b.appendSlice(arena, "{\"jsonrpc\":\"2.0\",\"id\":") catch return; switch (id) { .integer => |n| b.print(arena, "{d}", .{n}) catch return, .string => |t| jstr(&b, arena, t) catch return, - else => app(&b, arena, "null") catch return, + else => b.appendSlice(arena, "null") catch return, } if (std.mem.eql(u8, method, "workspace/configuration")) { // one null per asked item — a bare null here is a protocol violation // some servers punish with a parse loop const n = items(get(params, "items")).len; - app(&b, arena, ",\"result\":[") catch return; - for (0..n) |i| app(&b, arena, if (i == 0) "null" else ",null") catch return; - app(&b, arena, "]}") catch return; + b.appendSlice(arena, ",\"result\":[") catch return; + for (0..n) |i| b.appendSlice(arena, if (i == 0) "null" else ",null") catch return; + b.appendSlice(arena, "]}") catch return; } else if (std.mem.eql(u8, method, "workspace/applyEdit")) { - app(&b, arena, ",\"result\":{\"applied\":false}}") catch return; + b.appendSlice(arena, ",\"result\":{\"applied\":false}}") catch return; } else { - app(&b, arena, ",\"result\":null}") catch return; + b.appendSlice(arena, ",\"result\":null}") catch return; } answerServer(c, io, sock, b.items); } @@ -2183,8 +2170,7 @@ fn notification(c: *Conn, io: std.Io, si: usize, g: u32, raw: []const u8, method // ------------------------------------------------------------- introspection -fn status(io: std.Io, req: lsp.Req, out: *std.Io.Writer) !void { - _ = req; +fn status(io: std.Io, out: *std.Io.Writer) !void { try out.print("\nprotocol servers (lsp-client):\n", .{}); for (&specs, 0..) |*s, i| { const c = &conns[i]; @@ -2242,17 +2228,13 @@ fn status(io: std.Io, req: lsp.Req, out: *std.Io.Writer) !void { // ------------------------------------------------------------------ plumbing -fn app(b: *std.ArrayList(u8), arena: std.mem.Allocator, s: []const u8) Err!void { - b.appendSlice(arena, s) catch return error.OutOfMemory; -} - /// `"textDocument":{"uri":U}` — the params every request about a file /// starts with. fn docParams(arena: std.mem.Allocator, uri: []const u8) Err!std.ArrayList(u8) { var b: std.ArrayList(u8) = .empty; - try app(&b, arena, "\"textDocument\":{\"uri\":"); + try b.appendSlice(arena, "\"textDocument\":{\"uri\":"); try jstr(&b, arena, uri); - try app(&b, arena, "}"); + try b.appendSlice(arena, "}"); return b; } @@ -2260,7 +2242,7 @@ fn docParams(arena: std.mem.Allocator, uri: []const u8) Err!std.ArrayList(u8) { /// position request. fn atPos(arena: std.mem.Allocator, uri: []const u8, pos: Pos) Err!std.ArrayList(u8) { var b = try docParams(arena, uri); - b.print(arena, ",\"position\":{{\"line\":{d},\"character\":{d}}}", .{ pos.line, pos.ch }) catch return error.OutOfMemory; + try b.print(arena, ",\"position\":{{\"line\":{d},\"character\":{d}}}", .{ pos.line, pos.ch }); return b; } @@ -2417,12 +2399,12 @@ fn rootOf(dir: []const u8, markers: []const []const u8) []const u8 { /// 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 { - b.appendSlice(arena, "file://") catch return error.OutOfMemory; + try b.appendSlice(arena, "file://"); for (path) |ch| { if (std.ascii.isAlphanumeric(ch) or ch == '/' or ch == '-' or ch == '_' or ch == '.' or ch == '~') { - b.append(arena, ch) catch return error.OutOfMemory; + try b.append(arena, ch); } else { - b.print(arena, "%{X:0>2}", .{ch}) catch return error.OutOfMemory; + try b.print(arena, "%{X:0>2}", .{ch}); } } } @@ -2458,39 +2440,39 @@ fn pathOf(arena: std.mem.Allocator, uri: []const u8) ?[]const u8 { /// 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. fn jstr(b: *std.ArrayList(u8), arena: std.mem.Allocator, s: []const u8) Err!void { - b.append(arena, '"') catch return error.OutOfMemory; + try b.append(arena, '"'); var i: usize = 0; while (i < s.len) { const ch = s[i]; if (ch < 0x80) { switch (ch) { - '"' => b.appendSlice(arena, "\\\"") catch return error.OutOfMemory, - '\\' => b.appendSlice(arena, "\\\\") catch return error.OutOfMemory, - '\n' => b.appendSlice(arena, "\\n") catch return error.OutOfMemory, - '\r' => b.appendSlice(arena, "\\r") catch return error.OutOfMemory, - '\t' => b.appendSlice(arena, "\\t") catch return error.OutOfMemory, + '"' => try b.appendSlice(arena, "\\\""), + '\\' => try b.appendSlice(arena, "\\\\"), + '\n' => try b.appendSlice(arena, "\\n"), + '\r' => try b.appendSlice(arena, "\\r"), + '\t' => try b.appendSlice(arena, "\\t"), else => if (ch < 0x20) - b.print(arena, "\\u{x:0>4}", .{ch}) catch return error.OutOfMemory + try b.print(arena, "\\u{x:0>4}", .{ch}) else - b.append(arena, ch) catch return error.OutOfMemory, + try b.append(arena, ch), } i += 1; continue; } const l = std.unicode.utf8ByteSequenceLength(ch) catch { - b.append(arena, '?') catch return error.OutOfMemory; + try b.append(arena, '?'); i += 1; continue; }; if (i + l > s.len or !std.unicode.utf8ValidateSlice(s[i .. i + l])) { - b.append(arena, '?') catch return error.OutOfMemory; + try b.append(arena, '?'); i += 1; continue; } - b.appendSlice(arena, s[i .. i + l]) catch return error.OutOfMemory; + try b.appendSlice(arena, s[i .. i + l]); i += l; } - b.append(arena, '"') catch return error.OutOfMemory; + try b.append(arena, '"'); } const Pos = struct { line: u32, ch: u32 }; @@ -2750,13 +2732,13 @@ test "uri round-trips spaces and utf-8" { try std.testing.expect(pathOf(arena, "https://x/y") == null); } -test "posOf and offsetAt invert each other under both encodings" { +test "posOf and strictOffset invert each other under both encodings" { const src = "aé𝕏b\ncd\n"; // é: 2 bytes/1 unit, 𝕏: 4 bytes/2 units inline for (.{ Enc.utf8, Enc.utf16 }) |enc| { const off: u32 = 7; // the 'b' const p = posOf(src, off, enc); try std.testing.expectEqual(@as(u32, 0), p.line); - try std.testing.expectEqual(off, @as(u32, @intCast(offsetAt(src, p.line, p.ch, enc)))); + try std.testing.expectEqual(@as(?usize, off), strictOffset(src, p.line, p.ch, enc)); } const p2 = posOf("aé𝕏b\ncd\n", 10, .utf16); // the 'd' try std.testing.expectEqual(@as(u32, 1), p2.line); @@ -2865,14 +2847,14 @@ test "a language with no server to start says so, not that it did not answer" { defer arena.deinit(); var buf: [256]u8 = undefined; var out: std.Io.Writer = .fixed(&buf); - try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out); + try query(std.testing.io, arena.allocator(), .{ .kind = .hover, .path = "/x/a.py", .source = "", .offset = 0 }, &out); try std.testing.expectEqualStrings("@none language server for .py not found\n", out.buffered()); } 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, .{ .kind = .status, .path = "", .source = "", .offset = 0 }, &out.writer); + try status(std.testing.io, &out.writer); try std.testing.expect(std.mem.indexOf(u8, out.written(), "not started yet (its first query starts it)") != null); } @@ -2896,7 +2878,7 @@ test "a server that failed a moment ago is said to have failed, and when it is t defer arena.deinit(); var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); - try query(std.testing.io, std.testing.allocator, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer); + try query(std.testing.io, arena.allocator(), .{ .kind = .hover, .path = "/x/a.zig", .source = "", .offset = 0 }, &out.writer); try std.testing.expect(std.mem.indexOf(u8, out.written(), "zls failed recently; retry in 5s") != null); } |
