Skip to content

chore: update_root, sync_root_with_cwd, refactor with move_missing_val #1359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 26, 2022
Merged
22 changes: 7 additions & 15 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Setup may only be run once; subsequent calls will result in a warning.
sort_by = "name",
root_dirs = {},
prefer_startup_root = false,
update_cwd = false,
sync_root_with_cwd = false,
reload_on_bufenter = false,
respect_buf_cwd = false,
view = {
Expand Down Expand Up @@ -247,7 +247,6 @@ Setup may only be run once; subsequent calls will result in a warning.
},
update_focused_file = {
enable = false,
update_cwd = false,
update_root = false,
ignore_list = {},
},
Expand Down Expand Up @@ -398,7 +397,7 @@ Prefer startup root directory when updating root directory of the tree.
Only relevant when `update_focused_file.update_root` is `true`
Type: `boolean`, Default: `false`

*nvim-tree.update_cwd*
*nvim-tree.sync_root_with_cwd* (previously `update_cwd`)
Changes the tree root directory on `DirChanged` and refreshes the tree.
Type: `boolean`, Default: `false`

Expand All @@ -410,7 +409,7 @@ Automatically reloads the tree on `BufEnter` nvim-tree.
Will change cwd of nvim-tree to that of new buffer's when opening nvim-tree.
Type: `boolean`, Default: `false`

*nvim-tree.hijack_directories*
*nvim-tree.hijack_directories* (previously `update_to_buf_dir`)
hijacks new directory buffers when they are opened (`:e dir`).

*nvim-tree.hijack_directories.enable*
Expand All @@ -431,14 +430,7 @@ until it finds the file.
Enable this feature.
Type: `boolean`, Default: `false`

*nvim-tree.update_focused_file.update_cwd*
(deprecated, use `update_focused_file.update_root`)
Update the root directory of the tree to the one of the folder containing
the file if the file is not under the current root directory.
Only relevant when `update_focused_file.enable` is `true`
Type: `boolean`, Default: `false`

*nvim-tree.update_focused_file.update_root*
*nvim-tree.update_focused_file.update_root* (previously `update_focused_file.update_cwd`)
Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file.
Expand All @@ -448,7 +440,7 @@ until it finds the file.
*nvim-tree.update_focused_file.ignore_list*
List of buffer names and filetypes that will not update the root dir
of the tree if the file isn't found under the current root directory.
Only relevant when `update_focused_file.update_cwd` and
Only relevant when `update_focused_file.update_root` and
`update_focused_file.enable` are `true`.
Type: {string}, Default: `{}`

Expand Down Expand Up @@ -768,7 +760,7 @@ Configuration for various actions.

*nvim-tree.actions.change_dir.global*
Use `:cd` instead of `:lcd` when changing directories.
Consider that this might cause issues with the `update_cwd` options.
Consider that this might cause issues with the |nvim-tree.sync_root_with_cwd| option.
Type: `boolean`, Default: `false`

*nvim-tree.actions.change_dir.restrict_above_cwd*
Expand All @@ -788,7 +780,7 @@ Configuration for various actions.
It will also disable preventing a buffer overriding the tree.
Type: `boolean`, Default: `false`

*nvim-tree.actions.open_file.resize_window*
*nvim-tree.actions.open_file.resize_window* (previously `view.auto_resize`)
Resizes the tree when opening a file.
Type: `boolean`, Default: `true`

Expand Down
7 changes: 3 additions & 4 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function M.find_file(with_open, bufnr, bang)

-- if we don't schedule, it will search for NvimTree
vim.schedule(function()
if bang or _config.update_focused_file.update_cwd or _config.update_focused_file.update_root then
if bang or _config.update_focused_file.update_root then
M.change_root(filepath, bufnr)
end
require("nvim-tree.actions.find-file").fn(filepath)
Expand Down Expand Up @@ -351,7 +351,7 @@ local function setup_autocommands(opts)
if opts.hijack_cursor then
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
end
if opts.update_cwd then
if opts.sync_root_with_cwd then
create_nvim_tree_autocmd("DirChanged", {
callback = function()
M.change_dir(vim.loop.cwd())
Expand Down Expand Up @@ -407,7 +407,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
sort_by = "name",
root_dirs = {},
prefer_startup_root = false,
update_cwd = false,
sync_root_with_cwd = false,
reload_on_bufenter = false,
respect_buf_cwd = false,
view = {
Expand Down Expand Up @@ -487,7 +487,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
update_focused_file = {
enable = false,
update_cwd = false,
update_root = false,
ignore_list = {},
},
Expand Down
23 changes: 3 additions & 20 deletions lua/nvim-tree/legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,9 @@ local function refactored(opts)
end
end

-- update_to_buf_dir -> hijack_directories
if opts.update_to_buf_dir ~= nil then
utils.table_create_missing(opts, "hijack_directories")
if opts.hijack_directories.enable == nil then
opts.hijack_directories.enable = opts.update_to_buf_dir.enable
end
if opts.hijack_directories.auto_open == nil then
opts.hijack_directories.auto_open = opts.update_to_buf_dir.auto_open
end
opts.update_to_buf_dir = nil
end

-- view.auto_resize -> actions.open_file.resize_window
if opts.view and opts.view.auto_resize ~= nil then
utils.table_create_missing(opts, "actions.open_file")
if opts.actions.open_file.resize_window == nil then
opts.actions.open_file.resize_window = opts.view.auto_resize
end
opts.view.auto_resize = nil
end
-- 2022/06/20
utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root")
utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd")
end

local function removed(opts)
Expand Down
47 changes: 44 additions & 3 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" }

Expand Down