Skip to content

Commit 6e4d2a4

Browse files
committed
feat: Use new Zig type builtins replacing @Type
1 parent 7c4578f commit 6e4d2a4

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .lsp_kit,
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.16.0-dev.1204+389368392",
4+
.minimum_zig_version = "0.16.0-dev.1455+6d543bcf9",
55
.dependencies = .{},
66
.paths = .{
77
"build.zig",

src/basic_server.zig

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn validateServerCapabilities(comptime Handler: type, capabilities: types.Se
588588
\\ arena: std.mem.Allocator,
589589
\\ params: {},
590590
\\) {} {{}}
591-
\\
591+
\\
592592
, .{
593593
std.zig.fmtId(method_name),
594594
Handler,
@@ -610,38 +610,44 @@ fn MessageType(comptime Handler: type) type {
610610
&.{ lsp.isRequestMethod, lsp.isNotificationMethod },
611611
&.{ &RequestParams, &NotificationParams },
612612
) |isMethod, Params| {
613-
var methods: []const [:0]const u8 = &.{};
613+
var methods: []const []const u8 = &.{};
614614

615615
for (std.meta.declarations(Handler)) |decl| {
616616
if (isMethod(decl.name)) {
617-
methods = methods ++ [1][:0]const u8{decl.name};
617+
methods = methods ++ [_][]const u8{decl.name};
618618
}
619619
}
620+
methods = methods ++ [_][]const u8{"other"};
621+
622+
const MethodTag = std.math.IntFittingRange(0, methods.len);
623+
var method_values: [methods.len]MethodTag = undefined;
624+
for (0..methods.len) |i| {
625+
method_values[i] = i;
626+
}
620627

621-
var enum_fields: [methods.len + 1]std.builtin.Type.EnumField = undefined;
622-
for (enum_fields[0 .. enum_fields.len - 1], methods, 0..) |*field, method, i| field.* = .{ .name = method, .value = i };
623-
enum_fields[methods.len] = .{ .name = "other", .value = methods.len };
624-
625-
const MethodEnum = @Type(.{ .@"enum" = .{
626-
.tag_type = std.math.IntFittingRange(0, methods.len),
627-
.fields = &enum_fields,
628-
.decls = &.{},
629-
.is_exhaustive = true,
630-
} });
631-
632-
var union_fields: [methods.len + 1]std.builtin.Type.UnionField = undefined;
633-
for (union_fields[0 .. union_fields.len - 1], methods) |*field, method| {
634-
const field_type = lsp.ParamsType(method);
635-
field.* = .{ .name = method, .type = field_type, .alignment = @alignOf(field_type) };
628+
const MethodEnum = @Enum(
629+
MethodTag,
630+
.exhaustive,
631+
methods,
632+
&method_values,
633+
);
634+
635+
var union_types: [methods.len]type = undefined;
636+
var union_attrs: [methods.len]std.builtin.Type.UnionField.Attributes = undefined;
637+
for (0..methods.len - 1) |i| {
638+
union_types[i] = lsp.ParamsType(methods[i]);
639+
union_attrs[i] = .{ .@"align" = @alignOf(union_types[i]) };
636640
}
637-
union_fields[methods.len] = .{ .name = "other", .type = lsp.MethodWithParams, .alignment = @alignOf(lsp.MethodWithParams) };
638-
639-
Params.* = @Type(.{ .@"union" = .{
640-
.layout = .auto,
641-
.tag_type = MethodEnum,
642-
.fields = &union_fields,
643-
.decls = &.{},
644-
} });
641+
union_types[methods.len - 1] = lsp.MethodWithParams;
642+
union_attrs[methods.len - 1] = .{ .@"align" = @alignOf(lsp.MethodWithParams) };
643+
644+
Params.* = @Union(
645+
.auto,
646+
MethodEnum,
647+
methods,
648+
&union_types,
649+
&union_attrs,
650+
);
645651
}
646652

647653
return lsp.Message(RequestParams, NotificationParams, .{});

0 commit comments

Comments
 (0)