nvim/key: add __newindex meta to bind.Group

This allows callers of the struct to perform the more normal '='
assignment and have sane behavior.

Previously, this would not do what one expects when used with bind.Bind
or other bind.Group(s): it merely adds them to the Group object
directly, not adding it to the ._children table or downward propagating
the Group's settings.

This is fixed here, by adding a __newindex metamethod to the bind.Group
object, which checks to see if the addition is a known Bind or Group
type, and does the right if it is, falling back to the previous behavior
if it isn't
This commit is contained in:
Paul Stemmet 2022-12-09 20:03:35 +00:00
parent 9be9933052
commit 6978596066
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 13 additions and 0 deletions

View File

@ -63,6 +63,19 @@ setmetatable(Group, {
return self.__children[name]
end,
__newindex = function(self, name, new)
local t = type(new) == 'table' and new.__struct or nil
if t and t == Group.__struct or t == Bind.__struct then
-- We rely on public behavior here (__call meta method)
-- should perhaps refactor the logic out of that and directly
-- call it here
self { [name] = new }
else
rawset(self, name, new)
end
end,
---Group.__call meta method
---@param self BindGroup
---@param new table<string, Bind|BindGroup|BindOptions>