diff options
author | Folke Lemaitre <folke.lemaitre@gmail.com> | 2021-07-19 17:52:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-19 08:52:44 -0700 |
commit | 526fc609b83742430ff952f95ab3b56ef9f4951b (patch) | |
tree | ad6fd4f6ca9243585b56914edeaab494ca481ea9 /runtime/lua/vim/shared.lua | |
parent | 3b2a85ff76c464ff317459672ce7d46417dd06a0 (diff) | |
download | rneovim-526fc609b83742430ff952f95ab3b56ef9f4951b.tar.gz rneovim-526fc609b83742430ff952f95ab3b56ef9f4951b.tar.bz2 rneovim-526fc609b83742430ff952f95ab3b56ef9f4951b.zip |
fix(shared): do not treat empty tables as list in deep extend (#15094)
An empty table was previously always treated as a list, which means that
while merging tables, whenever an empty table was encountered it would
always truncate any table on the left.
`vim.tbl_deep_extend("force", { b = { a = 1 } }, { b = {} })`
Before: `{ b = {} }`
After: `{ b = { a = 1 } }`
Diffstat (limited to 'runtime/lua/vim/shared.lua')
-rw-r--r-- | runtime/lua/vim/shared.lua | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 0a663628a5..f7f37e089f 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -200,6 +200,10 @@ function vim.tbl_isempty(t) return next(t) == nil end +local function can_merge(v) + return type(v) == "table" and (vim.tbl_isempty(v) or not vim.tbl_islist(v)) +end + local function tbl_extend(behavior, deep_extend, ...) if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then error('invalid "behavior": '..tostring(behavior)) @@ -219,8 +223,8 @@ local function tbl_extend(behavior, deep_extend, ...) vim.validate{["after the second argument"] = {tbl,'t'}} if tbl then for k, v in pairs(tbl) do - if type(v) == 'table' and deep_extend and not vim.tbl_islist(v) then - ret[k] = tbl_extend(behavior, true, ret[k] or vim.empty_dict(), v) + if deep_extend and can_merge(v) and can_merge(ret[k]) then + ret[k] = tbl_extend(behavior, true, ret[k], v) elseif behavior ~= 'force' and ret[k] ~= nil then if behavior == 'error' then error('key found in more than one map: '..k) |