From 19b38609f8cad5d70f6e4090bdcadafd2f509fd0 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 13 Dec 2025 22:24:52 +0100 Subject: [PATCH] fix: don't use extract_cbor on language_server_options Also, simplify cbor encoding of initializationOptions. --- src/Project.zig | 24 +++++++++--------------- src/project_manager.zig | 2 +- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/Project.zig b/src/Project.zig index 9af626d..3b242a7 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -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 Options = struct { - pub fn cborEncode(options: @This(), writer: *std.Io.Writer) std.io.Writer.Error!void { - const msg = std.mem.replaceOwned(u8, options.alloc, options.option, "\\\"", "\"") catch { - try cbor.writeValue(writer, ""); + const initializationOptions: struct { + pub fn cborEncode(self_: @This(), writer: *std.Io.Writer) std.io.Writer.Error!void { + const toCbor = cbor.fromJsonAlloc(self_.alloc, self_.options) catch { + try cbor.writeValue(writer, cbor.null_); return; }; - defer options.alloc.free(msg); - - const toCbor = cbor.fromJsonAlloc(options.alloc, msg[1..]) catch { - try cbor.writeValue(writer, ""); - return; - }; - defer options.alloc.free(toCbor); + defer self_.alloc.free(toCbor); writer.writeAll(toCbor) catch return error.WriteFailed; } - option: []const u8, + options: []const u8, alloc: std.mem.Allocator, - }; - const initOptions: Options = .{ .option = language_server_options, .alloc = self.allocator }; + } = .{ .options = language_server_options, .alloc = self.allocator }; + try lsp.send_request(self.allocator, "initialize", .{ - .initializationOptions = initOptions, + .initializationOptions = initializationOptions, .processId = if (builtin.os.tag == .linux) std.os.linux.getpid() else null, .rootPath = project_path, .rootUri = project_uri, diff --git a/src/project_manager.zig b/src/project_manager.zig index 2ba42a9..94703d8 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -448,7 +448,7 @@ const Process = struct { 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) })) { 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 ""; 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) })) {