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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 42 deletions

View File

@ -167,7 +167,7 @@ Setup may only be run once; subsequent calls will result in a warning.
sort_by = "name", sort_by = "name",
root_dirs = {}, root_dirs = {},
prefer_startup_root = false, prefer_startup_root = false,
update_cwd = false, sync_root_with_cwd = false,
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
view = { view = {
@ -247,7 +247,6 @@ Setup may only be run once; subsequent calls will result in a warning.
}, },
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_cwd = false,
update_root = false, update_root = false,
ignore_list = {}, ignore_list = {},
}, },
@ -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` Only relevant when `update_focused_file.update_root` is `true`
Type: `boolean`, Default: `false` 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. Changes the tree root directory on `DirChanged` and refreshes the tree.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
@ -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. Will change cwd of nvim-tree to that of new buffer's when opening nvim-tree.
Type: `boolean`, Default: `false` 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`). hijacks new directory buffers when they are opened (`:e dir`).
*nvim-tree.hijack_directories.enable* *nvim-tree.hijack_directories.enable*
@ -431,14 +430,7 @@ until it finds the file.
Enable this feature. Enable this feature.
Type: `boolean`, Default: `false` Type: `boolean`, Default: `false`
*nvim-tree.update_focused_file.update_cwd* *nvim-tree.update_focused_file.update_root* (previously `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*
Update the root directory of the tree if the file is not under current Update the root directory of the tree if the file is not under current
root directory. It prefers vim's cwd and `root_dirs`. root directory. It prefers vim's cwd and `root_dirs`.
Otherwise it falls back to the folder containing the file. Otherwise it falls back to the folder containing the file.
@ -448,7 +440,7 @@ until it finds the file.
*nvim-tree.update_focused_file.ignore_list* *nvim-tree.update_focused_file.ignore_list*
List of buffer names and filetypes that will not update the root dir 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. 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`. `update_focused_file.enable` are `true`.
Type: {string}, Default: `{}` Type: {string}, Default: `{}`
@ -768,7 +760,7 @@ Configuration for various actions.
*nvim-tree.actions.change_dir.global* *nvim-tree.actions.change_dir.global*
Use `:cd` instead of `:lcd` when changing directories. 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` Type: `boolean`, Default: `false`
*nvim-tree.actions.change_dir.restrict_above_cwd* *nvim-tree.actions.change_dir.restrict_above_cwd*
@ -788,7 +780,7 @@ Configuration for various actions.
It will also disable preventing a buffer overriding the tree. It will also disable preventing a buffer overriding the tree.
Type: `boolean`, Default: `false` 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. Resizes the tree when opening a file.
Type: `boolean`, Default: `true` Type: `boolean`, Default: `true`

View File

@ -159,7 +159,7 @@ function M.find_file(with_open, bufnr, bang)
-- if we don't schedule, it will search for NvimTree -- if we don't schedule, it will search for NvimTree
vim.schedule(function() 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) M.change_root(filepath, bufnr)
end end
require("nvim-tree.actions.find-file").fn(filepath) require("nvim-tree.actions.find-file").fn(filepath)
@ -351,7 +351,7 @@ local function setup_autocommands(opts)
if opts.hijack_cursor then if opts.hijack_cursor then
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node }) create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
end end
if opts.update_cwd then if opts.sync_root_with_cwd then
create_nvim_tree_autocmd("DirChanged", { create_nvim_tree_autocmd("DirChanged", {
callback = function() callback = function()
M.change_dir(vim.loop.cwd()) M.change_dir(vim.loop.cwd())
@ -407,7 +407,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
sort_by = "name", sort_by = "name",
root_dirs = {}, root_dirs = {},
prefer_startup_root = false, prefer_startup_root = false,
update_cwd = false, sync_root_with_cwd = false,
reload_on_bufenter = false, reload_on_bufenter = false,
respect_buf_cwd = false, respect_buf_cwd = false,
view = { view = {
@ -487,7 +487,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
}, },
update_focused_file = { update_focused_file = {
enable = false, enable = false,
update_cwd = false,
update_root = false, update_root = false,
ignore_list = {}, ignore_list = {},
}, },

View File

@ -285,26 +285,9 @@ local function refactored(opts)
end end
end end
-- update_to_buf_dir -> hijack_directories -- 2022/06/20
if opts.update_to_buf_dir ~= nil then utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root")
utils.table_create_missing(opts, "hijack_directories") utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd")
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
end end
local function removed(opts) local function removed(opts)

View File

@ -246,15 +246,15 @@ end
-- Create empty sub-tables if not present -- Create empty sub-tables if not present
-- @param tbl to create empty inside of -- @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 -- @return deepest sub-table
function M.table_create_missing(tbl, sub) function M.table_create_missing(tbl, path)
if tbl == nil then if tbl == nil then
return nil return nil
end end
local t = tbl local t = tbl
for s in string.gmatch(sub, "([^%.]+)%.*") do for s in string.gmatch(path, "([^%.]+)%.*") do
if t[s] == nil then if t[s] == nil then
t[s] = {} t[s] = {}
end end
@ -264,6 +264,47 @@ function M.table_create_missing(tbl, sub)
return t return t
end 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) function M.format_bytes(bytes)
local units = { "B", "K", "M", "G", "T" } local units = { "B", "K", "M", "G", "T" }