chore: update_root, sync_root_with_cwd, refactor with move_missing_val (#1359)

* chore: opts.update_focused_file.update_cwd -> update_root

* chore: opts.update_cwd -> sync_root_with_cwd

* chore: refactor options with utils move_missing_val

* chore: refactor options with utils move_missing_val

* chore: refactor options with utils move_missing_val

* chore: refactor options with utils move_missing_val

* chore: refactor options with utils move_missing_val

* chore: refactor options with utils move_missing_val
This commit is contained in:
Alexander Courtis
2022-06-26 12:18:14 +10:00
committed by GitHub
parent b299a877ad
commit 0c13bd76a8
4 changed files with 57 additions and 42 deletions

View File

@@ -246,15 +246,15 @@ end
-- Create empty sub-tables if not present
-- @param tbl to create empty inside of
-- @param sub dot separated string of sub-tables
-- @param path dot separated string of sub-tables
-- @return deepest sub-table
function M.table_create_missing(tbl, sub)
function M.table_create_missing(tbl, path)
if tbl == nil then
return nil
end
local t = tbl
for s in string.gmatch(sub, "([^%.]+)%.*") do
for s in string.gmatch(path, "([^%.]+)%.*") do
if t[s] == nil then
t[s] = {}
end
@@ -264,6 +264,47 @@ function M.table_create_missing(tbl, sub)
return t
end
-- Move a value from src to dst if value is nil on dst
-- @param src to copy from
-- @param src_path dot separated string of sub-tables
-- @param src_pos value pos
-- @param dst to copy to
-- @param dst_path dot separated string of sub-tables, created when missing
-- @param dst_pos value pos
function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos)
local ok, err = pcall(vim.validate, {
src = { src, "table" },
src_path = { src_path, "string" },
src_pos = { src_pos, "string" },
dst = { dst, "table" },
dst_path = { dst_path, "string" },
dst_pos = { dst_pos, "string" },
})
if not ok then
M.warn("move_missing_val: " .. (err or "invalid arguments"))
end
for pos in string.gmatch(src_path, "([^%.]+)%.*") do
if src[pos] and type(src[pos]) == "table" then
src = src[pos]
else
src = nil
break
end
end
local src_val = src and src[src_pos]
if src_val == nil then
return
end
dst = M.table_create_missing(dst, dst_path)
if dst[dst_pos] == nil then
dst[dst_pos] = src_val
end
src[src_pos] = nil
end
function M.format_bytes(bytes)
local units = { "B", "K", "M", "G", "T" }