From 85b60dedb6bdb192abd11b2d58ca7c0e8c944b18 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 1 Oct 2026 06:22:44 -0300 Subject: A Rename, references and the like tell the server of every open file of its language first, so a rename reaches files it had not seen and the preview lists them zls renames only in the documents in its store, and syncDoc told it of a file only when a query was asked from it, so renaming in progress.zig left main.zig unchanged and broken, with no preview. For a query whose answer can reach other files (rename, references, implementation, workspace symbols, calls and type hierarchy), the host snapshots every other open file and the client syncs those its server answers for first. The mock server answers a rename in each document it was told of; lsp-rename-files opens main.rs by a look, renames from lib.rs, and its preview lists both. Co-Authored-By: Claude Opus 5.5 --- src/host_io.zig | 35 +++++++++++++++++++++++++++++++++++ src/lsp/lsp.zig | 15 +++++++++++++++ src/lsp/lsp_client.zig | 8 ++++++++ 3 files changed, 58 insertions(+) (limited to 'src') diff --git a/src/host_io.zig b/src/host_io.zig index f944e1cb..9e9a37ed 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -143,12 +143,19 @@ pub const Lsp = struct { source: [:0]u8, arg: []u8, root: []u8, + /// The other open files, for a query that can reach them. + others: []pardes.lsp.Doc = &.{}, pub fn free(job: *Job, gpa: std.mem.Allocator) void { gpa.free(job.path); gpa.free(job.source); gpa.free(job.arg); gpa.free(job.root); + for (job.others) |doc| { + gpa.free(doc.path); + gpa.free(doc.source); + } + gpa.free(job.others); gpa.destroy(job); } }; @@ -169,6 +176,7 @@ pub const Lsp = struct { errdefer gpa.free(arg); const declared_root = if (file) |f| std.fs.path.dirname(f.path) orelse "/" else pane.cwdSlice(); const root = try gpa.dupe(u8, filesystem.localPath(declared_root) orelse declared_root); + errdefer gpa.free(root); job.* = .{ .id = req.id, .kind = req.kind, @@ -178,6 +186,32 @@ pub const Lsp = struct { .arg = arg, .root = root, }; + // ponytail: a copy of every open file per such query; a server + // already told of one is sent nothing more (syncDoc). + if (pardes.lsp.reachesOtherFiles(req.kind)) { + var others: std.ArrayList(pardes.lsp.Doc) = .empty; + errdefer { + for (others.items) |doc| { + gpa.free(doc.path); + gpa.free(doc.source); + } + others.deinit(gpa); + } + for (core.panes, 0..) |slot, i| { + const other = slot orelse continue; + if (i == req.pane) continue; + const f = other.file orelse continue; + if (f.output != null) continue; + const local = filesystem.localPath(f.path) orelse continue; + const other_path = try gpa.dupe(u8, local); + const other_source = gpa.dupeZ(u8, f.content) catch |err| { + gpa.free(other_path); + return err; + }; + try others.append(gpa, .{ .path = other_path, .source = other_source }); + } + job.others = try others.toOwnedSlice(gpa); + } return job; } @@ -197,6 +231,7 @@ pub const Lsp = struct { .offset = job.offset, .arg = job.arg, .root = job.root, + .others = job.others, }, &out.writer) catch { deliver(ctx, job.id, null); return; diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig index 33e95570..8dc4da4f 100644 --- a/src/lsp/lsp.zig +++ b/src/lsp/lsp.zig @@ -31,11 +31,26 @@ pub const Req = struct { 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` (` /// `) by `explaining`; a definition when none was asked before. explains: Kind = .definition, }; +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, .incoming_calls, .outgoing_calls, .supertypes, .subtypes => true, + else => false, + }; +} + /// An `explain` request made the one it narrates: `explains` and `arg` set /// from its ` `; any other request as it is. pub fn explaining(req: Req) Req { diff --git a/src/lsp/lsp_client.zig b/src/lsp/lsp_client.zig index 0c5e3b7e..b57b348e 100644 --- a/src/lsp/lsp_client.zig +++ b/src/lsp/lsp_client.zig @@ -501,6 +501,14 @@ fn run(c: *Conn, si: usize, arena: std.mem.Allocator, req: lsp.Req, out: *std.Io var uri: std.ArrayList(u8) = .empty; try uriOf(&uri, arena, req.path); try syncDoc(c, 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. + 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, si, arena, other.items, doc.source); + tr.note("synced {s} first", .{std.fs.path.basename(doc.path)}); + }; const pos = posOf(req.source, req.offset, c.caps.enc); var cx: Cx = .{ .arena = arena, .base = req.root, .cur_path = req.path, .cur_src = req.source, .out = out }; -- cgit v1.3