diff options
author | Yasuhiro Matsumoto <mattn.jp@gmail.com> | 2020-02-08 21:57:05 +0900 |
---|---|---|
committer | Yasuhiro Matsumoto <mattn.jp@gmail.com> | 2020-02-08 21:57:05 +0900 |
commit | c3e4b62d1ed0cfcb138357a2006de3d4e6ac371a (patch) | |
tree | 98a6127c5ec0f0e5e9e615dd71555aca99beeeaa /autoload | |
parent | 5a9a1bdc182e8c5ade75bc91ec112d105abb37c7 (diff) | |
download | vim-lsp-settings-c3e4b62d1ed0cfcb138357a2006de3d4e6ac371a.tar.gz vim-lsp-settings-c3e4b62d1ed0cfcb138357a2006de3d4e6ac371a.tar.bz2 vim-lsp-settings-c3e4b62d1ed0cfcb138357a2006de3d4e6ac371a.zip |
Add utility function to merge dictionary which has keys with dot separated.
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/lsp_settings/utils.vim | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/autoload/lsp_settings/utils.vim b/autoload/lsp_settings/utils.vim index 96feb0a..a39851f 100644 --- a/autoload/lsp_settings/utils.vim +++ b/autoload/lsp_settings/utils.vim @@ -30,3 +30,78 @@ function! lsp_settings#utils#first_one(lines) abort endif return l:path endfunction + +function! s:merge(lhs, rhs) abort + let [l:lhs, l:rhs] = [a:lhs, a:rhs] + if type(l:lhs) ==# 3 + if type(l:rhs) ==# 3 + let l:lhs += l:rhs + if len(l:lhs) + call remove(l:lhs, 0, len(l:lhs)-1) + endif + for l:rhi in l:rhs + call add(l:lhs, l:rhs[l:rhi]) + endfor + elseif type(l:rhs) ==# 4 + let l:lhs += map(keys(l:rhs), '{v:val : l:rhs[v:val]}') + endif + elseif type(l:lhs) ==# 4 + if type(l:rhs) ==# 3 + for l:V in l:rhs + if type(l:V) != 4 + continue + endif + for l:k in keys(l:V) + let l:lhs[l:k] = l:V[l:k] + endfor + endfor + elseif type(l:rhs) ==# 4 + for l:key in keys(l:rhs) + if type(l:rhs[l:key]) ==# 3 + if !has_key(l:lhs, l:key) + let l:lhs[l:key] = [] + endif + if type(l:lhs[l:key]) == 3 + let l:lhs[l:key] += l:rhs[l:key] + elseif type(l:lhs[l:key]) == 4 + for l:k in keys(l:rhs[l:key]) + let l:lhs[l:key][l:k] = l:rhs[l:key][l:k] + endfor + endif + elseif type(l:rhs[l:key]) ==# 4 + if has_key(l:lhs, l:key) + call s:merge(l:lhs[l:key], l:rhs[l:key]) + else + let l:lhs[l:key] = l:rhs[l:key] + endif + else + let l:lhs[l:key] = l:rhs[l:key] + endif + endfor + endif + endif +endfunction + +function! lsp_settings#utils#merge(d) abort + let l:ret = {} + let l:keys = keys(a:d) + for l:k in sort(keys(a:d)) + let l:new = {} + let l:cur = l:new + let l:arr = split(l:k, '\.') + for l:i in range(len(l:arr)) + let l:kk = l:arr[l:i] + if type(l:cur) == v:t_dict && !has_key(l:cur, l:kk) + if l:i == len(l:arr) - 1 + let l:cur[l:kk] = a:d[l:k] + break + else + let l:cur[l:kk] = {} + endif + endif + let l:cur = l:cur[l:kk] + endfor + call s:merge(l:ret, l:new) + endfor + return l:ret +endfunction |