fix: merge_tables bug caught

This commit is contained in:
Galen Rowell 2021-09-04 13:35:46 +10:00 committed by siduck76
parent eced5f2ec4
commit a5ae8899d5
1 changed files with 14 additions and 4 deletions

View File

@ -255,6 +255,7 @@ M.merge_table = function(into, from, nodes_to_replace)
if type(nodes_to_replace) == "table" then if type(nodes_to_replace) == "table" then
-- function that will be executed with loadstring -- function that will be executed with loadstring
local replace_fn = function(node)
local base_fn = [[ local base_fn = [[
return function(table1, table2) return function(table1, table2)
local t1, t2 = table1_node or false , table2_node or false local t1, t2 = table1_node or false , table2_node or false
@ -263,11 +264,20 @@ return function(table1, table2)
end end
return table1 return table1
end]] end]]
for _, node in ipairs(nodes_to_replace) do
-- replace the _node in base_fn to actual given node value -- replace the _node in base_fn to actual given node value
local fn = base_fn:gsub("_node", node) local fn = base_fn:gsub("_node", node)
-- if the node if found, it is replaced, otherwise table 1 is returned -- return the function created from the string base_fn
table1 = loadstring(fn)()(table1, table2) return loadstring(fn)()(table1, table2)
end
for _, node in ipairs(nodes_to_replace) do
-- pcall() is a poor workaround for if "['mappings']['plugin']['esc_insertmode']" 'plugin' sub-table does not exist
local ok, result = pcall(replace_fn, node)
if ok then
-- if the node is found then replace
table1 = result
end
end end
end end