Allow struct/union/enum binding types to have default values (#7291)

This allows for `keybind = super+d=new_split` to now work (defaults to
"auto"). This will also let us convert void types to union/enum/struct
types in the future without breaking existing bindings.
pull/7061/head
Mitchell Hashimoto 2025-05-07 09:50:44 -07:00 committed by GitHub
commit c36314ca98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 1 deletions

View File

@ -568,6 +568,8 @@ pub const Action = union(enum) {
left, left,
up, up,
auto, // splits along the larger direction auto, // splits along the larger direction
pub const default: SplitDirection = .auto;
}; };
pub const SplitFocusDirection = enum { pub const SplitFocusDirection = enum {
@ -729,7 +731,28 @@ pub const Action = union(enum) {
Action.CursorKey => return Error.InvalidAction, Action.CursorKey => return Error.InvalidAction,
else => { else => {
const idx = colonIdx orelse return Error.InvalidFormat; // Get the parameter after the colon. The parameter
// can be optional for action types that can have a
// "default" decl.
const idx = colonIdx orelse {
switch (@typeInfo(field.type)) {
.@"struct",
.@"union",
.@"enum",
=> if (@hasDecl(field.type, "default")) {
return @unionInit(
Action,
field.name,
@field(field.type, "default"),
);
},
else => {},
}
return Error.InvalidFormat;
};
const param = input[idx + 1 ..]; const param = input[idx + 1 ..];
return @unionInit( return @unionInit(
Action, Action,
@ -2015,6 +2038,17 @@ test "parse: action with enum" {
} }
} }
test "parse: action with enum with default" {
const testing = std.testing;
// parameter
{
const binding = try parseSingle("a=new_split");
try testing.expect(binding.action == .new_split);
try testing.expectEqual(Action.SplitDirection.auto, binding.action.new_split);
}
}
test "parse: action with int" { test "parse: action with int" {
const testing = std.testing; const testing = std.testing;