Skip to content

Commit 2765a6b

Browse files
committed
request config options using a single ConfigurationItem
Depends on ziglang/vscode-zig#442 to work properly in VS Code.
1 parent a5e8045 commit 2765a6b

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/Server.zig

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -677,25 +677,15 @@ fn registerCapability(server: *Server, method: []const u8, registersOptions: ?ty
677677
server.allocator.free(json_message);
678678
}
679679

680+
/// Request configuration options with the `workspace/configuration` request.
680681
fn requestConfiguration(server: *Server) Error!void {
681-
var configuration_items = comptime config: {
682-
var comp_config: [std.meta.fields(Config).len]types.ConfigurationItem = undefined;
683-
for (std.meta.fields(Config), 0..) |field, index| {
684-
comp_config[index] = .{
685-
.section = "zls." ++ field.name,
686-
};
687-
}
688-
689-
break :config comp_config;
682+
const configuration_items: [1]types.ConfigurationItem = .{
683+
.{
684+
.section = "zls",
685+
.scopeUri = if (server.workspaces.items.len == 1) server.workspaces.items[0].uri else null,
686+
},
690687
};
691688

692-
if (server.workspaces.items.len == 1) {
693-
const workspace = server.workspaces.items[0];
694-
for (&configuration_items) |*item| {
695-
item.*.scopeUri = workspace.uri;
696-
}
697-
}
698-
699689
const json_message = try server.sendToClientRequest(
700690
.{ .string = "i_haz_configuration" },
701691
"workspace/configuration",
@@ -706,18 +696,28 @@ fn requestConfiguration(server: *Server) Error!void {
706696
server.allocator.free(json_message);
707697
}
708698

699+
/// Handle the response of the `workspace/configuration` request.
709700
fn handleConfiguration(server: *Server, json: std.json.Value) error{OutOfMemory}!void {
710701
const tracy_zone = tracy.trace(@src());
711702
defer tracy_zone.end();
712703

713-
const fields = std.meta.fields(configuration.Configuration);
714-
const result = switch (json) {
715-
.array => |arr| if (arr.items.len == fields.len) arr.items else {
716-
log.err("workspace/configuration expects an array of size {d} but received {d}", .{ fields.len, arr.items.len });
717-
return;
704+
const result: std.json.Value = switch (json) {
705+
.array => |arr| blk: {
706+
if (arr.items.len != 1) {
707+
log.err("Response to 'workspace/configuration' expects an array of size 1 but received {d}", .{arr.items.len});
708+
return;
709+
}
710+
break :blk switch (arr.items[0]) {
711+
.object => arr.items[0],
712+
.null => return,
713+
else => {
714+
log.err("Response to 'workspace/configuration' expects an array of objects but got an array of {t}.", .{json});
715+
return;
716+
},
717+
};
718718
},
719719
else => {
720-
log.err("workspace/configuration expects an array but received {t}", .{json});
720+
log.err("Response to 'workspace/configuration' expects an array but received {t}", .{json});
721721
return;
722722
},
723723
};
@@ -726,20 +726,15 @@ fn handleConfiguration(server: *Server, json: std.json.Value) error{OutOfMemory}
726726
defer arena_allocator.deinit();
727727
const arena = arena_allocator.allocator();
728728

729-
var new_config: configuration.Configuration = .{};
730-
731-
inline for (fields, result) |field, json_value| {
732-
var runtime_known_field_name: []const u8 = ""; // avoid unnecessary function instantiations of `std.Io.Writer.print`
733-
runtime_known_field_name = field.name;
734-
735-
const maybe_new_value = std.json.parseFromValueLeaky(field.type, arena, json_value, .{}) catch |err| blk: {
736-
log.err("failed to parse configuration option '{s}': {}", .{ runtime_known_field_name, err });
737-
break :blk null;
738-
};
739-
if (maybe_new_value) |new_value| {
740-
@field(new_config, field.name) = new_value;
741-
}
742-
}
729+
var new_config = std.json.parseFromValueLeaky(
730+
configuration.Configuration,
731+
arena,
732+
result,
733+
.{ .ignore_unknown_fields = true },
734+
) catch |err| {
735+
log.err("Failed to parse response from 'workspace/configuration': {}", .{err});
736+
return;
737+
};
743738

744739
const maybe_root_dir: ?[]const u8 = dir: {
745740
if (server.workspaces.items.len != 1) break :dir null;

0 commit comments

Comments
 (0)