diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index ec59c2e6d68..7b2550aeca4 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -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 = { @@ -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 = {}, }, @@ -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` @@ -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* @@ -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. @@ -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: `{}` @@ -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* @@ -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` diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 2120ebd3c67..708f10c5f44 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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) @@ -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()) @@ -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 = { @@ -487,7 +487,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS }, update_focused_file = { enable = false, - update_cwd = false, update_root = false, ignore_list = {}, }, diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 2b59bdf53f4..f19b09acf1e 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -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) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index aee28dc34f9..59fa757a38d 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -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" }