diff options
| author | Hirokazu Hata <h.hata.ai.t@gmail.com> | 2020-03-22 13:50:08 +0900 |
|---|---|---|
| committer | Hirokazu Hata <h.hata.ai.t@gmail.com> | 2020-03-22 13:50:08 +0900 |
| commit | 60384dc2a60e8d2e966c0f0e9064590f2c3b28bd (patch) | |
| tree | 15069ecd3dd47e334b666ecf75bb4ff883576bba /lua/nvim_lsp/util.lua | |
| parent | Merge pull request #176 from lithammer/bashls-root-dir (diff) | |
| download | nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar.gz nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar.bz2 nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar.lz nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar.xz nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.tar.zst nvim-lspconfig-60384dc2a60e8d2e966c0f0e9064590f2c3b28bd.zip | |
util: add behavior options to tbl_deep_extend function
tbl_deep_extend always override extended table properties.
This behavior is not expected. This will fix #155.
Diffstat (limited to 'lua/nvim_lsp/util.lua')
| -rw-r--r-- | lua/nvim_lsp/util.lua | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/lua/nvim_lsp/util.lua b/lua/nvim_lsp/util.lua index 33eff0c9..1c68e1fc 100644 --- a/lua/nvim_lsp/util.lua +++ b/lua/nvim_lsp/util.lua @@ -46,20 +46,38 @@ function M.add_hook_after(func, new_fn) end end -function M.tbl_deep_extend(dst, ...) - validate { dst = { dst, 't' } } - for i = 1, select("#", ...) do - local t = select(i, ...) - validate { arg = { t, 't' } } - for k, v in pairs(t) do - if type(v) == 'table' and not vim.tbl_islist(v) then - dst[k] = M.tbl_deep_extend(dst[k] or vim.empty_dict(), v) - else - dst[k] = v +function M.tbl_deep_extend(behavior, ...) + if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then + error('invalid "behavior": '..tostring(behavior)) + end + + if select('#', ...) < 2 then + error('wrong number of arguments (given '..tostring(1 + select('#', ...))..', expected at least 3)') + end + + local ret = {} + if vim._empty_dict_mt ~= nil and getmetatable(select(1, ...)) == vim._empty_dict_mt then + ret = vim.empty_dict() + end + + for i = 1, select('#', ...) do + local tbl = select(i, ...) + vim.validate{["after the second argument"] = {tbl,'t'}} + if tbl then + for k, v in pairs(tbl) do + if type(v) == 'table' and not vim.tbl_islist(v) then + ret[k] = M.tbl_deep_extend(behavior, ret[k] or vim.empty_dict(), v) + elseif behavior ~= 'force' and ret[k] ~= nil then + if behavior == 'error' then + error('key found in more than one map: '..k) + end -- Else behavior is "keep". + else + ret[k] = v + end end end end - return dst + return ret end function M.nvim_multiline_command(command) |
