aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim_lsp/util.lua
diff options
context:
space:
mode:
authorHirokazu Hata <h.hata.ai.t@gmail.com>2020-03-26 00:12:12 +0900
committerGitHub <noreply@github.com>2020-03-26 00:12:12 +0900
commit68873604e1cbadf784e864c4ff08ff1735ca9be2 (patch)
treecdc6e0d71fe83ca80e33e1b2a7875785a9082075 /lua/nvim_lsp/util.lua
parentMerge pull request #179 from h-michael/invalid-root-dir (diff)
parentutil: add behavior options to tbl_deep_extend function (diff)
downloadnvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar.gz
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar.bz2
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar.lz
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar.xz
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.tar.zst
nvim-lspconfig-68873604e1cbadf784e864c4ff08ff1735ca9be2.zip
Merge pull request #178 from h-michael/deep-extend
util: add behavior options to tbl_deep_extend function
Diffstat (limited to 'lua/nvim_lsp/util.lua')
-rw-r--r--lua/nvim_lsp/util.lua40
1 files changed, 29 insertions, 11 deletions
diff --git a/lua/nvim_lsp/util.lua b/lua/nvim_lsp/util.lua
index ab084ea4..11d3473d 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)