diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-01-26 22:22:20 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-05-16 09:04:17 -0600 |
| commit | e3551e1ab54b6220047b005947d99380b2b7dfde (patch) | |
| tree | 9cbb00de31f748a8110101eb9d2c617c0fb6c85f /util.lua | |
| parent | cef8b428cff4cfb3fa07f30eab644cba1540b4b2 (diff) | |
| download | vis-lspc-e3551e1ab54b6220047b005947d99380b2b7dfde.tar.gz vis-lspc-e3551e1ab54b6220047b005947d99380b2b7dfde.zip | |
add table helper utility functions
Diffstat (limited to 'util.lua')
| -rw-r--r-- | util.lua | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -279,4 +279,60 @@ function util.vis_pos_to_sel(win, pos) return sel end +--- Utility table function +util.table = {} + +--- Recursively copy a table and all its members. +-- @param tbl the table to copy +-- @return a deep copy of the table +function util.table.deep_copy(tbl) + local cpy = {} + for k, v in pairs(tbl) do + if type(v) ~= 'table' then + cpy[k] = v + else + cpy[k] = util.table.deep_copy(v) + end + end + + for i, v in ipairs(tbl) do + if type(v) ~= 'table' then + cpy[i] = v + else + cpy[i] = util.table.deep_copy(v) + end + end + return cpy +end + +--- Recursively merge two tables. +-- This method modifies the weak table, if this is not intended make sure you +-- use a deep copy of the weak table. +-- The members of the strong table override members of the weak one. +-- @param weak table which's members are potentially overridden +-- @param strong table which's members override ones from the weak table +-- @return the merged table +function util.table.merge(weak, strong) + -- merge named members + for k, v in pairs(strong) do + -- potentially override the weak member + if type(v) ~= 'table' then + weak[k] = v + else + weak[k] = util.table.merge(weak[k] or {}, v) + end + end + + -- merge numbered members + for i, v in ipairs(strong) do + -- potentially override the weak member + if type(v) ~= 'table' then + weak[i] = v + else + weak[i] = util.table.merge(weak[i] or {}, v) + end + end + return weak +end + return util |
