feat: support LSPs that use string IDs
This commit is contained in:
parent
8308be76cd
commit
26694e9b08
3 changed files with 53 additions and 36 deletions
60
src/LSP.zig
60
src/LSP.zig
|
@ -39,13 +39,6 @@ pub fn send_request(self: Self, allocator: std.mem.Allocator, method: []const u8
|
||||||
return self.pid.call(allocator, request_timeout, .{ "REQ", method, cb.items });
|
return self.pid.call(allocator, request_timeout, .{ "REQ", method, cb.items });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_response(self: Self, id: i32, m: anytype) SendError!tp.message {
|
|
||||||
var cb = std.ArrayList(u8).init(self.allocator);
|
|
||||||
defer cb.deinit();
|
|
||||||
try cbor.writeValue(cb.writer(), m);
|
|
||||||
return self.pid.send(.{ "RSP", id, cb.items });
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send_notification(self: Self, method: []const u8, m: anytype) (OutOfMemoryError || SendError)!void {
|
pub fn send_notification(self: Self, method: []const u8, m: anytype) (OutOfMemoryError || SendError)!void {
|
||||||
var cb = std.ArrayList(u8).init(self.allocator);
|
var cb = std.ArrayList(u8).init(self.allocator);
|
||||||
defer cb.deinit();
|
defer cb.deinit();
|
||||||
|
@ -73,7 +66,7 @@ const Process = struct {
|
||||||
sp_tag: [:0]const u8,
|
sp_tag: [:0]const u8,
|
||||||
log_file: ?std.fs.File = null,
|
log_file: ?std.fs.File = null,
|
||||||
next_id: i32 = 0,
|
next_id: i32 = 0,
|
||||||
requests: std.AutoHashMap(i32, tp.pid),
|
requests: std.StringHashMap(tp.pid),
|
||||||
|
|
||||||
const Receiver = tp.Receiver(*Process);
|
const Receiver = tp.Receiver(*Process);
|
||||||
|
|
||||||
|
@ -99,7 +92,7 @@ const Process = struct {
|
||||||
.parent = tp.self_pid().clone(),
|
.parent = tp.self_pid().clone(),
|
||||||
.tag = try allocator.dupeZ(u8, tag),
|
.tag = try allocator.dupeZ(u8, tag),
|
||||||
.project = try allocator.dupeZ(u8, project),
|
.project = try allocator.dupeZ(u8, project),
|
||||||
.requests = std.AutoHashMap(i32, tp.pid).init(allocator),
|
.requests = std.StringHashMap(tp.pid).init(allocator),
|
||||||
.sp_tag = try sp_tag_.toOwnedSliceSentinel(0),
|
.sp_tag = try sp_tag_.toOwnedSliceSentinel(0),
|
||||||
};
|
};
|
||||||
return tp.spawn_link(self.allocator, self, Process.start, self.tag);
|
return tp.spawn_link(self.allocator, self, Process.start, self.tag);
|
||||||
|
@ -107,7 +100,10 @@ const Process = struct {
|
||||||
|
|
||||||
fn deinit(self: *Process) void {
|
fn deinit(self: *Process) void {
|
||||||
var i = self.requests.iterator();
|
var i = self.requests.iterator();
|
||||||
while (i.next()) |req| req.value_ptr.deinit();
|
while (i.next()) |req| {
|
||||||
|
self.allocator.free(req.key_ptr.*);
|
||||||
|
req.value_ptr.deinit();
|
||||||
|
}
|
||||||
self.allocator.free(self.sp_tag);
|
self.allocator.free(self.sp_tag);
|
||||||
self.recv_buf.deinit();
|
self.recv_buf.deinit();
|
||||||
self.allocator.free(self.cmd.buf);
|
self.allocator.free(self.cmd.buf);
|
||||||
|
@ -176,12 +172,12 @@ const Process = struct {
|
||||||
var bytes: []u8 = "";
|
var bytes: []u8 = "";
|
||||||
var err: []u8 = "";
|
var err: []u8 = "";
|
||||||
var code: u32 = 0;
|
var code: u32 = 0;
|
||||||
var id: i32 = 0;
|
var cbor_id: []const u8 = "";
|
||||||
|
|
||||||
if (try cbor.match(m.buf, .{ "REQ", tp.extract(&method), tp.extract(&bytes) })) {
|
if (try cbor.match(m.buf, .{ "REQ", tp.extract(&method), tp.extract(&bytes) })) {
|
||||||
try self.send_request(from, method, bytes);
|
try self.send_request(from, method, bytes);
|
||||||
} else if (try cbor.match(m.buf, .{ "RSP", tp.extract(&id), tp.extract(&bytes) })) {
|
} else if (try cbor.match(m.buf, .{ "RSP", tp.extract_cbor(&cbor_id), tp.extract_cbor(&bytes) })) {
|
||||||
try self.send_response(id, bytes);
|
try self.send_response(cbor_id, bytes);
|
||||||
} else if (try cbor.match(m.buf, .{ "NTFY", tp.extract(&method), tp.extract(&bytes) })) {
|
} else if (try cbor.match(m.buf, .{ "NTFY", tp.extract(&method), tp.extract(&bytes) })) {
|
||||||
try self.send_notification(method, bytes);
|
try self.send_notification(method, bytes);
|
||||||
} else if (try cbor.match(m.buf, .{"close"})) {
|
} else if (try cbor.match(m.buf, .{"close"})) {
|
||||||
|
@ -215,7 +211,7 @@ const Process = struct {
|
||||||
var iter = cb;
|
var iter = cb;
|
||||||
|
|
||||||
const MsgMembers = struct {
|
const MsgMembers = struct {
|
||||||
id: ?i32 = null,
|
cbor_id: ?[]const u8 = null,
|
||||||
method: ?[]const u8 = null,
|
method: ?[]const u8 = null,
|
||||||
params: ?[]const u8 = null,
|
params: ?[]const u8 = null,
|
||||||
result: ?[]const u8 = null,
|
result: ?[]const u8 = null,
|
||||||
|
@ -228,7 +224,9 @@ const Process = struct {
|
||||||
var field_name: []const u8 = undefined;
|
var field_name: []const u8 = undefined;
|
||||||
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
|
if (!(try cbor.matchString(&iter, &field_name))) return error.InvalidMessage;
|
||||||
if (std.mem.eql(u8, field_name, "id")) {
|
if (std.mem.eql(u8, field_name, "id")) {
|
||||||
if (!(try cbor.matchValue(&iter, cbor.extract(&values.id)))) return error.InvalidMessageField;
|
var value: []const u8 = undefined;
|
||||||
|
if (!(try cbor.matchValue(&iter, cbor.extract_cbor(&value)))) return error.InvalidMessageField;
|
||||||
|
values.cbor_id = value;
|
||||||
} else if (std.mem.eql(u8, field_name, "method")) {
|
} else if (std.mem.eql(u8, field_name, "method")) {
|
||||||
if (!(try cbor.matchValue(&iter, cbor.extract(&values.method)))) return error.InvalidMessageField;
|
if (!(try cbor.matchValue(&iter, cbor.extract(&values.method)))) return error.InvalidMessageField;
|
||||||
} else if (std.mem.eql(u8, field_name, "params")) {
|
} else if (std.mem.eql(u8, field_name, "params")) {
|
||||||
|
@ -248,11 +246,11 @@ const Process = struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (values.id) |id| {
|
if (values.cbor_id) |cbor_id| {
|
||||||
return if (values.method) |method| // Request messages have a method
|
return if (values.method) |method| // Request messages have a method
|
||||||
self.receive_lsp_request(id, method, values.params)
|
self.receive_lsp_request(cbor_id, method, values.params)
|
||||||
else // Everything else is a Response message
|
else // Everything else is a Response message
|
||||||
self.receive_lsp_response(id, values.result, values.@"error");
|
self.receive_lsp_response(cbor_id, values.result, values.@"error");
|
||||||
} else { // Notification message has no ID
|
} else { // Notification message has no ID
|
||||||
return if (values.method) |method|
|
return if (values.method) |method|
|
||||||
self.receive_lsp_notification(method, values.params)
|
self.receive_lsp_notification(method, values.params)
|
||||||
|
@ -310,10 +308,14 @@ const Process = struct {
|
||||||
|
|
||||||
sp.send(output.items) catch return error.SendFailed;
|
sp.send(output.items) catch return error.SendFailed;
|
||||||
self.write_log("### SEND request:\n{s}\n###\n", .{output.items});
|
self.write_log("### SEND request:\n{s}\n###\n", .{output.items});
|
||||||
try self.requests.put(id, from.clone());
|
|
||||||
|
var cbor_id = std.ArrayList(u8).init(self.allocator);
|
||||||
|
defer cbor_id.deinit();
|
||||||
|
try cbor.writeValue(cbor_id.writer(), id);
|
||||||
|
try self.requests.put(try cbor_id.toOwnedSlice(), from.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_response(self: *Process, id: i32, result_cb: []const u8) (error{Closed} || SendError || cbor.Error || cbor.JsonEncodeError)!void {
|
fn send_response(self: *Process, cbor_id: []const u8, result_cb: []const u8) (error{Closed} || SendError || cbor.Error || cbor.JsonEncodeError)!void {
|
||||||
const sp = if (self.sp) |*sp| sp else return error.Closed;
|
const sp = if (self.sp) |*sp| sp else return error.Closed;
|
||||||
|
|
||||||
var msg = std.ArrayList(u8).init(self.allocator);
|
var msg = std.ArrayList(u8).init(self.allocator);
|
||||||
|
@ -323,7 +325,7 @@ const Process = struct {
|
||||||
try cbor.writeValue(msg_writer, "jsonrpc");
|
try cbor.writeValue(msg_writer, "jsonrpc");
|
||||||
try cbor.writeValue(msg_writer, "2.0");
|
try cbor.writeValue(msg_writer, "2.0");
|
||||||
try cbor.writeValue(msg_writer, "id");
|
try cbor.writeValue(msg_writer, "id");
|
||||||
try cbor.writeValue(msg_writer, id);
|
try msg_writer.writeAll(cbor_id);
|
||||||
try cbor.writeValue(msg_writer, "result");
|
try cbor.writeValue(msg_writer, "result");
|
||||||
_ = try msg_writer.write(result_cb);
|
_ = try msg_writer.write(result_cb);
|
||||||
|
|
||||||
|
@ -395,10 +397,12 @@ const Process = struct {
|
||||||
if (rest.len > 0) return self.frame_message_recv();
|
if (rest.len > 0) return self.frame_message_recv();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_lsp_request(self: *Process, id: i32, method: []const u8, params: ?[]const u8) Error!void {
|
fn receive_lsp_request(self: *Process, cbor_id: []const u8, method: []const u8, params: ?[]const u8) Error!void {
|
||||||
|
const json_id = try cbor.toJsonPrettyAlloc(self.allocator, cbor_id);
|
||||||
|
defer self.allocator.free(json_id);
|
||||||
const json = if (params) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
const json = if (params) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
||||||
defer if (json) |p| self.allocator.free(p);
|
defer if (json) |p| self.allocator.free(p);
|
||||||
self.write_log("### RECV req: {d}\nmethod: {s}\n{s}\n###\n", .{ id, method, json orelse "no params" });
|
self.write_log("### RECV req: {s}\nmethod: {s}\n{s}\n###\n", .{ json_id, method, json orelse "no params" });
|
||||||
var msg = std.ArrayList(u8).init(self.allocator);
|
var msg = std.ArrayList(u8).init(self.allocator);
|
||||||
defer msg.deinit();
|
defer msg.deinit();
|
||||||
const writer = msg.writer();
|
const writer = msg.writer();
|
||||||
|
@ -408,18 +412,20 @@ const Process = struct {
|
||||||
try cbor.writeValue(writer, self.tag);
|
try cbor.writeValue(writer, self.tag);
|
||||||
try cbor.writeValue(writer, "request");
|
try cbor.writeValue(writer, "request");
|
||||||
try cbor.writeValue(writer, method);
|
try cbor.writeValue(writer, method);
|
||||||
try cbor.writeValue(writer, id);
|
try writer.writeAll(cbor_id);
|
||||||
if (params) |p| _ = try writer.write(p) else try cbor.writeValue(writer, null);
|
if (params) |p| _ = try writer.write(p) else try cbor.writeValue(writer, null);
|
||||||
self.parent.send_raw(.{ .buf = msg.items }) catch return error.SendFailed;
|
self.parent.send_raw(.{ .buf = msg.items }) catch return error.SendFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_lsp_response(self: *Process, id: i32, result: ?[]const u8, err: ?[]const u8) Error!void {
|
fn receive_lsp_response(self: *Process, cbor_id: []const u8, result: ?[]const u8, err: ?[]const u8) Error!void {
|
||||||
|
const json_id = try cbor.toJsonPrettyAlloc(self.allocator, cbor_id);
|
||||||
|
defer self.allocator.free(json_id);
|
||||||
const json = if (result) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
const json = if (result) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
||||||
defer if (json) |p| self.allocator.free(p);
|
defer if (json) |p| self.allocator.free(p);
|
||||||
const json_err = if (err) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
const json_err = if (err) |p| try cbor.toJsonPrettyAlloc(self.allocator, p) else null;
|
||||||
defer if (json_err) |p| self.allocator.free(p);
|
defer if (json_err) |p| self.allocator.free(p);
|
||||||
self.write_log("### RECV rsp: {d} {s}\n{s}\n###\n", .{ id, if (json_err) |_| "error" else "response", json_err orelse json orelse "no result" });
|
self.write_log("### RECV rsp: {s} {s}\n{s}\n###\n", .{ json_id, if (json_err) |_| "error" else "response", json_err orelse json orelse "no result" });
|
||||||
const from = self.requests.get(id) orelse return;
|
const from = self.requests.get(cbor_id) orelse return;
|
||||||
var msg = std.ArrayList(u8).init(self.allocator);
|
var msg = std.ArrayList(u8).init(self.allocator);
|
||||||
defer msg.deinit();
|
defer msg.deinit();
|
||||||
const writer = msg.writer();
|
const writer = msg.writer();
|
||||||
|
|
|
@ -900,16 +900,25 @@ pub fn show_message(_: *Self, _: tp.pid_ref, params_cb: []const u8) !void {
|
||||||
logger.print("{s}", .{msg});
|
logger.print("{s}", .{msg});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_capability(self: *Self, from: tp.pid_ref, id: i32, params_cb: []const u8) ClientError!void {
|
pub fn register_capability(self: *Self, from: tp.pid_ref, cbor_id: []const u8, params_cb: []const u8) ClientError!void {
|
||||||
_ = params_cb;
|
_ = params_cb;
|
||||||
return self.send_lsp_response(from, id, null);
|
return self.send_lsp_response(from, cbor_id, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_lsp_response(self: *Self, from: tp.pid_ref, id: i32, result: anytype) ClientError!void {
|
pub fn workDoneProgress_create(self: *Self, from: tp.pid_ref, cbor_id: []const u8, params_cb: []const u8) ClientError!void {
|
||||||
|
_ = params_cb;
|
||||||
|
return self.send_lsp_response(from, cbor_id, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_lsp_response(self: *Self, from: tp.pid_ref, cbor_id: []const u8, result: anytype) ClientError!void {
|
||||||
var cb = std.ArrayList(u8).init(self.allocator);
|
var cb = std.ArrayList(u8).init(self.allocator);
|
||||||
defer cb.deinit();
|
defer cb.deinit();
|
||||||
|
const writer = cb.writer();
|
||||||
|
try cbor.writeArrayHeader(writer, 3);
|
||||||
|
try cbor.writeValue(writer, "RSP");
|
||||||
|
try writer.writeAll(cbor_id);
|
||||||
try cbor.writeValue(cb.writer(), result);
|
try cbor.writeValue(cb.writer(), result);
|
||||||
from.send(.{ "RSP", id, cb.items }) catch return error.ClientFailed;
|
from.send_raw(.{ .buf = cb.items }) catch return error.ClientFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_lsp_init_request(self: *Self, lsp: LSP, project_path: []const u8, project_basename: []const u8, project_uri: []const u8) CallError!tp.message {
|
fn send_lsp_init_request(self: *Self, lsp: LSP, project_path: []const u8, project_basename: []const u8, project_uri: []const u8) CallError!tp.message {
|
||||||
|
|
|
@ -268,7 +268,7 @@ const Process = struct {
|
||||||
var file_type: []const u8 = undefined;
|
var file_type: []const u8 = undefined;
|
||||||
var language_server: []const u8 = undefined;
|
var language_server: []const u8 = undefined;
|
||||||
var method: []const u8 = undefined;
|
var method: []const u8 = undefined;
|
||||||
var id: i32 = 0;
|
var cbor_id: []const u8 = undefined;
|
||||||
var params_cb: []const u8 = undefined;
|
var params_cb: []const u8 = undefined;
|
||||||
var high: i64 = 0;
|
var high: i64 = 0;
|
||||||
var low: i64 = 0;
|
var low: i64 = 0;
|
||||||
|
@ -299,8 +299,8 @@ const Process = struct {
|
||||||
self.update_mru(project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
self.update_mru(project_directory, path, row, col) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
|
||||||
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&project_directory), tp.extract(&language_server), "notify", tp.extract(&method), tp.extract_cbor(¶ms_cb) })) {
|
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&project_directory), tp.extract(&language_server), "notify", tp.extract(&method), tp.extract_cbor(¶ms_cb) })) {
|
||||||
self.dispatch_notify(project_directory, language_server, method, params_cb) catch |e| return self.logger.err("lsp-handling", e);
|
self.dispatch_notify(project_directory, language_server, method, params_cb) catch |e| return self.logger.err("lsp-handling", e);
|
||||||
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&project_directory), tp.extract(&language_server), "request", tp.extract(&method), tp.extract(&id), tp.extract_cbor(¶ms_cb) })) {
|
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&project_directory), tp.extract(&language_server), "request", tp.extract(&method), tp.extract_cbor(&cbor_id), tp.extract_cbor(¶ms_cb) })) {
|
||||||
self.dispatch_request(from, project_directory, language_server, method, id, params_cb) catch |e| return self.logger.err("lsp-handling", e);
|
self.dispatch_request(from, project_directory, language_server, method, cbor_id, params_cb) catch |e| return self.logger.err("lsp-handling", e);
|
||||||
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&path), "done" })) {
|
} else if (try cbor.match(m.buf, .{ "child", tp.extract(&path), "done" })) {
|
||||||
self.logger.print_err("lsp-handling", "child '{s}' terminated", .{path});
|
self.logger.print_err("lsp-handling", "child '{s}' terminated", .{path});
|
||||||
} else if (try cbor.match(m.buf, .{ "open", tp.extract(&project_directory) })) {
|
} else if (try cbor.match(m.buf, .{ "open", tp.extract(&project_directory) })) {
|
||||||
|
@ -524,11 +524,13 @@ const Process = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_request(self: *Process, from: tp.pid_ref, project_directory: []const u8, language_server: []const u8, method: []const u8, id: i32, params_cb: []const u8) (ProjectError || Project.ClientError || cbor.Error || cbor.JsonEncodeError || UnsupportedError)!void {
|
fn dispatch_request(self: *Process, from: tp.pid_ref, project_directory: []const u8, language_server: []const u8, method: []const u8, cbor_id: []const u8, params_cb: []const u8) (ProjectError || Project.ClientError || cbor.Error || cbor.JsonEncodeError || UnsupportedError)!void {
|
||||||
_ = language_server;
|
_ = language_server;
|
||||||
const project = if (self.projects.get(project_directory)) |p| p else return error.NoProject;
|
const project = if (self.projects.get(project_directory)) |p| p else return error.NoProject;
|
||||||
return if (std.mem.eql(u8, method, "client/registerCapability"))
|
return if (std.mem.eql(u8, method, "client/registerCapability"))
|
||||||
project.register_capability(from, id, params_cb)
|
project.register_capability(from, cbor_id, params_cb)
|
||||||
|
else if (std.mem.eql(u8, method, "window/workDoneProgress/create"))
|
||||||
|
project.workDoneProgress_create(from, cbor_id, params_cb)
|
||||||
else blk: {
|
else blk: {
|
||||||
const params = try cbor.toJsonAlloc(self.allocator, params_cb);
|
const params = try cbor.toJsonAlloc(self.allocator, params_cb);
|
||||||
defer self.allocator.free(params);
|
defer self.allocator.free(params);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue