summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-25 05:03:22 -0300
committerGabriel Schneider <[email protected]>2026-11-25 05:10:14 -0300
commit7611e1bb7a1c3eda2f4ac463881f543653abd035 (patch)
treeb0ea5bb10dceaf3db2db7f9b06e7641f3e09ea57 /dev
parent36e5561ed6135e2c66f1e0813b037f34a1249eda (diff)
downloadpardes-7611e1bb7a1c3eda2f4ac463881f543653abd035.tar.gz
pardes-7611e1bb7a1c3eda2f4ac463881f543653abd035.zip
lsp_client, Ask, Pick, body_layer: code actions as a picker (walk UI P3 item 1, SPEC §5.1 "actions", §14.1, POLISH 17): the client sent each action's title alone (lsp_client run `.code_action`), so `SPC l a` could only list them in a +Lsp, nothing to preview or apply; NOW the client writes each action's row and after it its edit of this file as `@put` records (actionEdits, sorted, sharing renameEdits' WorkspaceEdit reading as editFiles), `@elsewhere` for an edit that reaches another file or makes one (never a part of it applied), none for a command's; an action the server sent without its edit but with `data` is asked for it with codeAction/resolve when the server says resolveProvider (Caps.resolve_actions); typed where jumps walk (Ask's `ghost`, recorded at ask time as the hover's) the answer is Pick's new source `actions` in the asking pane's own tag (`␣ l a Actions`, its host the edited pane as the themes picker's), each row its digit in the key ink then the title (the query matches the title), the body moved to the edit's first line while it is under the cursor and showing its edit as ghost rows (Pick.ghosts, drawn after Prompt.ghosts by the body layer: the lines it changes on the Changes layer's deleted ground, what they become after them on the added ground, the rows below pushed down; R3, DESIGN D4 add/del; bounded by the rows shown), Enter or `1`-`9` with the field empty applies it as one undo step through Ask.applyAction (applyLspEdits, of the revision it was asked of: Pick.revision), the tag at rest first; a command's or an elsewhere edit is said, not applied; anyone else's (a 9P script) keeps the +Lsp of titles; the mock (test/lspmock.zig) answers three actions: a title alone, one with its edit (a line put in at the top), one resolved (main.rs's two `add` calls to `sum`); CodeAction's doc and the builtins json say so; TESTS: core/Pick.zig "code actions: a picker in the asking pane's tag ..." (ghost rows and grounds rendered, `3` applies, `u` undoes, a command's said), test/behaviour/language_servers.zig "SPC l a lists the server's code actions in the pane's tag by their digits ..." (through the mock end to end, resolve included; on the base `SPC l a` makes a +Lsp); core-test 1063/1233 (170 skipped), behaviour-test 279/279, shell-test's edit tests 89/89
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'dev')
-rw-r--r--dev/lsp/lsp_client.zig85
1 files changed, 65 insertions, 20 deletions
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig
index fdd4af55..a53ff57f 100644
--- a/dev/lsp/lsp_client.zig
+++ b/dev/lsp/lsp_client.zig
@@ -247,6 +247,8 @@ const Caps = struct {
pull_workspace: bool = false,
call_hier: bool = false,
type_hier: bool = false,
+ /// codeAction/resolve: an action that came without its edit is asked for it
+ resolve_actions: bool = false,
/// workspace/didChangeWorkspaceFolders is worth sending
folders: bool = false,
};
@@ -1038,10 +1040,19 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
var b = try docParams(arena, uri.items);
try b.print(arena, ",\"range\":{{\"start\":{{\"line\":{d},\"character\":{d}}},\"end\":{{\"line\":{d},\"character\":{d}}}}},\"context\":{{\"diagnostics\":[]}}", .{ pos.line, pos.ch, pos.line, pos.ch });
const result = (try call(c, io, arena, "textDocument/codeAction", b.items, deadline)) orelse return;
- for (items(result)) |ca| {
- const title = str(get(ca, "title")) orelse continue;
+ // Each action a row, its edit of this file after it as `@put`
+ // records (the core's picker previews and applies them, Pick.zig);
+ // one the server left to resolve asked for its edit first.
+ for (items(result)) |listed| {
+ const title = str(get(listed, "title")) orelse continue;
try lsp.row(cx.out, lsp.rel(cx.base, cx.cur_path), pos.line, 0, flat(arena, title));
cx.rows += 1;
+ var ca = listed;
+ if (get(ca, "edit") == null and get(ca, "data") != null and c.caps.resolve_actions and ca == .object) {
+ const whole = try std.json.Stringify.valueAlloc(arena, ca, .{});
+ if (try call(c, io, arena, "codeAction/resolve", whole[1 .. whole.len - 1], deadline)) |resolved| ca = resolved;
+ }
+ try actionEdits(&cx, c.caps.enc, uri.items, get(ca, "edit"), req.source);
}
},
.completion => {
@@ -1246,24 +1257,8 @@ fn byteSpan(src: []const u8, range: ?std.json.Value, enc: Enc) ?struct { start:
/// 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;
-
- // (uri, edits) per file, from either shape of a WorkspaceEdit
- var files: std.ArrayList(struct { []const u8, ?std.json.Value }) = .empty;
- if (get(result, "documentChanges")) |dcs| {
- for (items(dcs)) |dc| {
- if (get(dc, "kind") != null) {
- foreign = true; // create/rename/delete file operations
- continue;
- }
- const u = str(get(get(dc, "textDocument"), "uri")) orelse continue;
- 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| try files.append(cx.arena, .{ e.key_ptr.*, e.value_ptr.* });
- };
- for (files.items) |f| {
+ const files, var foreign = try editFiles(cx.arena, result);
+ for (files) |f| {
const in_self = std.mem.eql(u8, f[0], self_uri);
if (!in_self) foreign = true;
for (items(f[1])) |ed| {
@@ -1299,6 +1294,55 @@ fn renameEdits(cx: *Cx, enc: Enc, self_uri: []const u8, result: std.json.Value,
for (edits.items) |ed| try lsp.put(cx.out, ed.start, ed.end, ed.text);
}
+/// A file a WorkspaceEdit edits: its uri and its TextEdit[].
+const EditFile = struct { []const u8, ?std.json.Value };
+
+/// A WorkspaceEdit's files, from either of its shapes, and whether it also
+/// creates, renames or deletes a file.
+fn editFiles(arena: std.mem.Allocator, we: std.json.Value) Err!struct { []const EditFile, bool } {
+ var files: std.ArrayList(EditFile) = .empty;
+ var ops = false;
+ if (get(we, "documentChanges")) |dcs| {
+ for (items(dcs)) |dc| {
+ if (get(dc, "kind") != null) {
+ ops = true;
+ continue;
+ }
+ const u = str(get(get(dc, "textDocument"), "uri")) orelse continue;
+ try files.append(arena, .{ u, get(dc, "edits") });
+ }
+ } else if (get(we, "changes")) |ch| if (ch == .object) {
+ var it = ch.object.iterator();
+ while (it.next()) |e| try files.append(arena, .{ e.key_ptr.*, e.value_ptr.* });
+ };
+ return .{ files.items, ops };
+}
+
+/// A code action's WorkspaceEdit (none for a command's): its edits of this
+/// file as sorted `@put` records; one that reaches another file or makes
+/// one is `@elsewhere`, never a part of it applied; a malformed edit leaves
+/// the action none.
+fn actionEdits(cx: *Cx, enc: Enc, self_uri: []const u8, we: ?std.json.Value, src: []const u8) Err!void {
+ const v = we orelse return;
+ const files, const ops = try editFiles(cx.arena, v);
+ var edits: std.ArrayList(PutEdit) = .empty;
+ var elsewhere = ops;
+ for (files) |f| {
+ if (!std.mem.eql(u8, f[0], self_uri)) {
+ elsewhere = true;
+ continue;
+ }
+ for (items(f[1])) |ed| {
+ const span = byteSpan(src, get(ed, "range"), enc) orelse return;
+ const text = str(get(ed, "newText")) orelse return;
+ try edits.append(cx.arena, .{ .start = span.start, .end = span.end, .text = text });
+ }
+ }
+ if (elsewhere) return cx.out.writeAll("@elsewhere\n");
+ sortEdits(edits.items);
+ for (edits.items) |ed| try lsp.put(cx.out, ed.start, ed.end, ed.text);
+}
+
/// TextEdit[] from formatting is by definition about the current document:
/// straight to sorted `@put` records.
fn formatEdits(cx: *Cx, enc: Enc, result: std.json.Value, src: []const u8) Err!void {
@@ -2111,6 +2155,7 @@ fn handshake(c: *Conn, io: std.Io, arena: std.mem.Allocator, root: []const u8) E
}
c.caps.call_hier = provider(get(caps, "callHierarchyProvider"));
c.caps.type_hier = provider(get(caps, "typeHierarchyProvider"));
+ c.caps.resolve_actions = if (get(get(caps, "codeActionProvider"), "resolveProvider")) |r| r == .bool and r.bool else false;
c.caps.folders = if (get(get(get(caps, "workspace"), "workspaceFolders"), "supported")) |s| s == .bool and s.bool else false;
try send(c, io, "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}", deadline);