From 6978596066fd46969f31b1ec4743b8d6ee05735e Mon Sep 17 00:00:00 2001 From: Bazaah Date: Fri, 9 Dec 2022 20:03:35 +0000 Subject: [PATCH] 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 --- nvim/lua/psoxizsh/key/bind.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/nvim/lua/psoxizsh/key/bind.lua b/nvim/lua/psoxizsh/key/bind.lua index 5c81fcd..01f63c3 100644 --- a/nvim/lua/psoxizsh/key/bind.lua +++ b/nvim/lua/psoxizsh/key/bind.lua @@ -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