Skip to content

Commit 3e36187

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

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
@@ -692,25 +692,15 @@ fn registerCapability(server: *Server, method: []const u8, registersOptions: ?ty
692692
server.allocator.free(json_message);
693693
}
694694

695+
/// Request configuration options with the `workspace/configuration` request.
695696
fn requestConfiguration(server: *Server) Error!void {
696-
var configuration_items = comptime config: {
697-
var comp_config: [std.meta.fields(Config).len]types.ConfigurationItem = undefined;
698-
for (std.meta.fields(Config), 0..) |field, index| {
699-
comp_config[index] = .{
700-
.section = "zls." ++ field.name,
701-
};
702-
}
703-
704-
break :config comp_config;
697+
const configuration_items: [1]types.ConfigurationItem = .{
698+
.{
699+
.section = "zls",
700+
.scopeUri = if (server.workspaces.items.len == 1) server.workspaces.items[0].uri else null,
701+
},
705702
};
706703

707-
if (server.workspaces.items.len == 1) {
708-
const workspace = server.workspaces.items[0];
709-
for (&configuration_items) |*item| {
710-
item.*.scopeUri = workspace.uri;
711-
}
712-
}
713-
714704
const json_message = try server.sendToClientRequest(
715705
.{ .string = "i_haz_configuration" },
716706
"workspace/configuration",
@@ -721,18 +711,28 @@ fn requestConfiguration(server: *Server) Error!void {
721711
server.allocator.free(json_message);
722712
}
723713

714+
/// Handle the response of the `workspace/configuration` request.
724715
fn handleConfiguration(server: *Server, json: std.json.Value) error{OutOfMemory}!void {
725716
const tracy_zone = tracy.trace(@src());
726717
defer tracy_zone.end();
727718

728-
const fields = std.meta.fields(configuration.Configuration);
729-
const result = switch (json) {
730-
.array => |arr| if (arr.items.len == fields.len) arr.items else {
731-
log.err("workspace/configuration expects an array of size {d} but received {d}", .{ fields.len, arr.items.len });
732-
return;
719+
const result: std.json.Value = switch (json) {
720+
.array => |arr| blk: {
721+
if (arr.items.len != 1) {
722+
log.err("Response to 'workspace/configuration' expects an array of size 1 but received {d}", .{arr.items.len});
723+
return;
724+
}
725+
break :blk switch (arr.items[0]) {
726+
.object => arr.items[0],
727+
.null => return,
728+
else => {
729+
log.err("Response to 'workspace/configuration' expects an array of objects but got an array of {t}.", .{json});
730+
return;
731+
},
732+
};
733733
},
734734
else => {
735-
log.err("workspace/configuration expects an array but received {t}", .{json});
735+
log.err("Response to 'workspace/configuration' expects an array but received {t}", .{json});
736736
return;
737737
},
738738
};
@@ -741,20 +741,15 @@ fn handleConfiguration(server: *Server, json: std.json.Value) error{OutOfMemory}
741741
defer arena_allocator.deinit();
742742
const arena = arena_allocator.allocator();
743743

744-
var new_config: configuration.Configuration = .{};
745-
746-
inline for (fields, result) |field, json_value| {
747-
var runtime_known_field_name: []const u8 = ""; // avoid unnecessary function instantiations of `std.Io.Writer.print`
748-
runtime_known_field_name = field.name;
749-
750-
const maybe_new_value = std.json.parseFromValueLeaky(field.type, arena, json_value, .{}) catch |err| blk: {
751-
log.err("failed to parse configuration option '{s}': {}", .{ runtime_known_field_name, err });
752-
break :blk null;
753-
};
754-
if (maybe_new_value) |new_value| {
755-
@field(new_config, field.name) = new_value;
756-
}
757-
}
744+
var new_config = std.json.parseFromValueLeaky(
745+
configuration.Configuration,
746+
arena,
747+
result,
748+
.{ .ignore_unknown_fields = true },
749+
) catch |err| {
750+
log.err("Failed to parse response from 'workspace/configuration': {}", .{err});
751+
return;
752+
};
758753

759754
const maybe_root_dir: ?[]const u8 = if (server.workspaces.items.len == 1) dir: {
760755
const uri = std.Uri.parse(server.workspaces.items[0].uri) catch |err| {

0 commit comments

Comments
 (0)