summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
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);