fix: don't use extract_cbor on language_server_options

Also, simplify cbor encoding of initializationOptions.
This commit is contained in:
CJ van den Berg 2025-12-13 22:24:52 +01:00
parent 8890ec7497
commit 19b38609f8
2 changed files with 10 additions and 16 deletions

View file

@ -2025,28 +2025,22 @@ fn send_lsp_init_request(self: *Self, lsp: *const LSP, project_path: []const u8,
}; };
const version = if (root.version.len > 0 and root.version[0] == 'v') root.version[1..] else root.version; const version = if (root.version.len > 0 and root.version[0] == 'v') root.version[1..] else root.version;
const Options = struct { const initializationOptions: struct {
pub fn cborEncode(options: @This(), writer: *std.Io.Writer) std.io.Writer.Error!void { pub fn cborEncode(self_: @This(), writer: *std.Io.Writer) std.io.Writer.Error!void {
const msg = std.mem.replaceOwned(u8, options.alloc, options.option, "\\\"", "\"") catch { const toCbor = cbor.fromJsonAlloc(self_.alloc, self_.options) catch {
try cbor.writeValue(writer, ""); try cbor.writeValue(writer, cbor.null_);
return; return;
}; };
defer options.alloc.free(msg); defer self_.alloc.free(toCbor);
const toCbor = cbor.fromJsonAlloc(options.alloc, msg[1..]) catch {
try cbor.writeValue(writer, "");
return;
};
defer options.alloc.free(toCbor);
writer.writeAll(toCbor) catch return error.WriteFailed; writer.writeAll(toCbor) catch return error.WriteFailed;
} }
option: []const u8, options: []const u8,
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
}; } = .{ .options = language_server_options, .alloc = self.allocator };
const initOptions: Options = .{ .option = language_server_options, .alloc = self.allocator };
try lsp.send_request(self.allocator, "initialize", .{ try lsp.send_request(self.allocator, "initialize", .{
.initializationOptions = initOptions, .initializationOptions = initializationOptions,
.processId = if (builtin.os.tag == .linux) std.os.linux.getpid() else null, .processId = if (builtin.os.tag == .linux) std.os.linux.getpid() else null,
.rootPath = project_path, .rootPath = project_path,
.rootUri = project_uri, .rootUri = project_uri,

View file

@ -448,7 +448,7 @@ const Process = struct {
self.add_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; self.add_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
} else if (try cbor.match(m.buf, .{ "delete_task", tp.extract(&project_directory), tp.extract(&task) })) { } else if (try cbor.match(m.buf, .{ "delete_task", tp.extract(&project_directory), tp.extract(&task) })) {
self.delete_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; self.delete_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
} else if (try cbor.match(m.buf, .{ "did_open", tp.extract(&project_directory), tp.extract(&path), tp.extract(&file_type), tp.extract_cbor(&language_server), tp.extract_cbor(&language_server_options), tp.extract(&version), tp.extract(&text_ptr), tp.extract(&text_len) })) { } else if (try cbor.match(m.buf, .{ "did_open", tp.extract(&project_directory), tp.extract(&path), tp.extract(&file_type), tp.extract_cbor(&language_server), tp.extract(&language_server_options), tp.extract(&version), tp.extract(&text_ptr), tp.extract(&text_len) })) {
const text = if (text_len > 0) @as([*]const u8, @ptrFromInt(text_ptr))[0..text_len] else ""; const text = if (text_len > 0) @as([*]const u8, @ptrFromInt(text_ptr))[0..text_len] else "";
self.did_open(project_directory, path, file_type, language_server, language_server_options, version, text) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; self.did_open(project_directory, path, file_type, language_server, language_server_options, version, text) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed;
} else if (try cbor.match(m.buf, .{ "did_change", tp.extract(&project_directory), tp.extract(&path), tp.extract(&version), tp.extract(&text_dst_ptr), tp.extract(&text_dst_len), tp.extract(&text_src_ptr), tp.extract(&text_src_len), tp.extract(&eol_mode) })) { } else if (try cbor.match(m.buf, .{ "did_change", tp.extract(&project_directory), tp.extract(&path), tp.extract(&version), tp.extract(&text_dst_ptr), tp.extract(&text_dst_len), tp.extract(&text_src_ptr), tp.extract(&text_src_len), tp.extract(&eol_mode) })) {