diff options
| author | Hirokazu Hata <h.hata.ai.t@gmail.com> | 2020-03-26 00:12:12 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-26 00:12:12 +0900 |
| commit | 68873604e1cbadf784e864c4ff08ff1735ca9be2 (patch) | |
| tree | cdc6e0d71fe83ca80e33e1b2a7875785a9082075 /lua | |
| parent | Merge pull request #179 from h-michael/invalid-root-dir (diff) | |
| parent | util: add behavior options to tbl_deep_extend function (diff) | |
| download | nvim-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')
| -rw-r--r-- | lua/nvim_lsp/configs.lua | 12 | ||||
| -rw-r--r-- | lua/nvim_lsp/elmls.lua | 2 | ||||
| -rw-r--r-- | lua/nvim_lsp/util.lua | 40 |
3 files changed, 33 insertions, 21 deletions
diff --git a/lua/nvim_lsp/configs.lua b/lua/nvim_lsp/configs.lua index e1145147..a70efdb5 100644 --- a/lua/nvim_lsp/configs.lua +++ b/lua/nvim_lsp/configs.lua @@ -99,16 +99,10 @@ function configs.__newindex(t, config_name, config_def) end local make_config = function(_root_dir) - local new_config = vim.tbl_extend("keep", vim.empty_dict(), config) - -- Deepcopy anything that is >1 level nested. - new_config.settings = vim.deepcopy(new_config.settings) - util.tbl_deep_extend(new_config.settings, default_config.settings) - - new_config.init_options = vim.deepcopy(new_config.init_options) - util.tbl_deep_extend(new_config.init_options, default_config.init_options) - + local new_config = util.tbl_deep_extend("keep", vim.empty_dict(), config) + new_config = util.tbl_deep_extend('keep', new_config, default_config) new_config.capabilities = new_config.capabilities or lsp.protocol.make_client_capabilities() - util.tbl_deep_extend(new_config.capabilities, { + new_config.capabilities = util.tbl_deep_extend('keep', new_config.capabilities, { workspace = { configuration = true; } diff --git a/lua/nvim_lsp/elmls.lua b/lua/nvim_lsp/elmls.lua index 827614f5..4f95cebf 100644 --- a/lua/nvim_lsp/elmls.lua +++ b/lua/nvim_lsp/elmls.lua @@ -43,7 +43,7 @@ configs[server_name] = { else new_config.cmd = {install_info.binaries[bin_name]} end - util.tbl_deep_extend(new_config.init_options, { + new_config.init_options = util.tbl_deep_extend('force', new_config.init_options, { elmPath = install_info.binaries["elm"]; elmFormatPath = install_info.binaries["elm-format"]; elmTestPath = install_info.binaries["elm-test"]; 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) |
