From 37eb14149063bffd44b142114c1140cc2bd99870 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 18 Jun 2022 15:53:16 +1000 Subject: [PATCH 1/8] chore: opts.update_focused_file.update_cwd -> update_root --- doc/nvim-tree-lua.txt | 16 ++++------------ lua/nvim-tree.lua | 5 ++--- lua/nvim-tree/legacy.lua | 8 ++++++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 821bce3354d..29cc5af1637 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -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 = {}, }, @@ -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: `{}` @@ -784,7 +776,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 dd7544a2070..87db43e4334 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -156,7 +156,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) @@ -348,7 +348,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.update_root then create_nvim_tree_autocmd("DirChanged", { callback = function() M.change_dir(vim.loop.cwd()) @@ -484,7 +484,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..365d6bd3db4 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -305,6 +305,14 @@ local function refactored(opts) end opts.view.auto_resize = nil end + + -- view.update_focused_file.update_cwd -> update_root + if opts.view and opts.update_focused_file and opts.update_focused_file.update_cwd ~= nil then + if not opts.update_focused_file.update_root == nil then + opts.update_focused_file.update_root = opts.update_focused_file.update_cwd + end + opts.update_focused_file.update_cwd = nil + end end local function removed(opts) From 41ddcb454f87a63c0ca49b80e04012503f82db6b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 18 Jun 2022 16:05:41 +1000 Subject: [PATCH 2/8] chore: opts.update_cwd -> sync_root_with_cwd --- doc/nvim-tree-lua.txt | 6 +++--- lua/nvim-tree.lua | 4 ++-- lua/nvim-tree/legacy.lua | 10 +++++++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 29cc5af1637..d71bc330ae1 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 = { @@ -397,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` @@ -759,7 +759,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* diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 87db43e4334..3b3b78ec876 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -348,7 +348,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_root then + if opts.sync_root_with_cwd then create_nvim_tree_autocmd("DirChanged", { callback = function() M.change_dir(vim.loop.cwd()) @@ -404,7 +404,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 = { diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 365d6bd3db4..02806f43e19 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -308,11 +308,19 @@ local function refactored(opts) -- view.update_focused_file.update_cwd -> update_root if opts.view and opts.update_focused_file and opts.update_focused_file.update_cwd ~= nil then - if not opts.update_focused_file.update_root == nil then + if opts.update_focused_file.update_root == nil then opts.update_focused_file.update_root = opts.update_focused_file.update_cwd end opts.update_focused_file.update_cwd = nil end + + -- view.update_focused_file.update_cwd -> update_root + if opts.update_cwd ~= nil then + if opts.sync_root_with_cwd == nil then + opts.sync_root_with_cwd = opts.update_cwd + end + opts.update_cwd = nil + end end local function removed(opts) From 530cdd0f13de614e5d7327f517a36b22d851d64c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 19 Jun 2022 13:51:40 +1000 Subject: [PATCH 3/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree.lua | 5 ++++- lua/nvim-tree/legacy.lua | 38 +++++------------------------------- lua/nvim-tree/utils.lua | 42 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 3b3b78ec876..6af080ad1ba 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -586,7 +586,10 @@ local function validate_options(conf) for k, v in pairs(user) do local invalid local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {} - if def[k] == nil then + if type(v) == "table" and not next(v) then + -- clear empty tables + user[k] = nil + elseif def[k] == nil then -- option does not exist invalid = string.format("unknown option: %s%s", prefix, k) elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 02806f43e19..02a98e736b7 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -285,42 +285,14 @@ 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 + utils.move_missing_val(opts, "update_to_buf_dir", "enable", opts, "hijack_directories", "enable") + utils.move_missing_val(opts, "update_to_buf_dir", "auto_open", opts, "hijack_directories", "auto_open") - -- 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 + utils.move_missing_val(opts, "view", "auto_resize", opts, "actions.open_file", "resize_window") - -- view.update_focused_file.update_cwd -> update_root - if opts.view and opts.update_focused_file and opts.update_focused_file.update_cwd ~= nil then - if opts.update_focused_file.update_root == nil then - opts.update_focused_file.update_root = opts.update_focused_file.update_cwd - end - opts.update_focused_file.update_cwd = nil - end + utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root") - -- view.update_focused_file.update_cwd -> update_root - if opts.update_cwd ~= nil then - if opts.sync_root_with_cwd == nil then - opts.sync_root_with_cwd = opts.update_cwd - end - opts.update_cwd = nil - end + 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..1606b702fdd 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,42 @@ 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 optional dot separated string of sub-tables +-- @param src_pos value pos +-- @param dst to copy to +-- @param dst_path optional 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) + if not src or not src_pos or src_pos == "" or not dst or not dst_pos or dst_pos == "" then + return + end + + -- find + 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 + + -- copy + dst = M.table_create_missing(dst, dst_path) + if dst[dst_pos] == nil then + dst[dst_pos] = src_val + end + + -- remove + src[src_pos] = nil +end + function M.format_bytes(bytes) local units = { "B", "K", "M", "G", "T" } From 20b9d4c6de1b8a9724d5cc2b1dc866ec055319e0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 20 Jun 2022 11:01:43 +1000 Subject: [PATCH 4/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree.lua | 5 +---- lua/nvim-tree/legacy.lua | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a1ea0ad44f0..72d75f9759c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -589,10 +589,7 @@ local function validate_options(conf) for k, v in pairs(user) do local invalid local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {} - if type(v) == "table" and not next(v) then - -- clear empty tables - user[k] = nil - elseif def[k] == nil then + if def[k] == nil then -- option does not exist invalid = string.format("unknown option: %s%s", prefix, k) elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index 02a98e736b7..eadbd3c25ec 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -287,6 +287,7 @@ local function refactored(opts) utils.move_missing_val(opts, "update_to_buf_dir", "enable", opts, "hijack_directories", "enable") utils.move_missing_val(opts, "update_to_buf_dir", "auto_open", opts, "hijack_directories", "auto_open") + opts.update_to_buf_dir = nil utils.move_missing_val(opts, "view", "auto_resize", opts, "actions.open_file", "resize_window") From d3f4c4c767cc568127259e1e64c5f8611de2371a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 20 Jun 2022 11:03:04 +1000 Subject: [PATCH 5/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree/utils.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 1606b702fdd..522eb51232a 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -276,7 +276,6 @@ function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos) return end - -- find for pos in string.gmatch(src_path, "([^%.]+)%.*") do if src[pos] and type(src[pos]) == "table" then src = src[pos] @@ -290,13 +289,11 @@ function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos) return end - -- copy dst = M.table_create_missing(dst, dst_path) if dst[dst_pos] == nil then dst[dst_pos] = src_val end - -- remove src[src_pos] = nil end From 86ed2c61c9337cbb12f3891e2ed4737529e85043 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 20 Jun 2022 11:36:45 +1000 Subject: [PATCH 6/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree/utils.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 522eb51232a..df18311b046 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -266,13 +266,22 @@ end -- Move a value from src to dst if value is nil on dst -- @param src to copy from --- @param src_path optional dot separated string of sub-tables +-- @param src_path dot separated string of sub-tables -- @param src_pos value pos -- @param dst to copy to --- @param dst_path optional dot separated string of sub-tables, created when missing +-- @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) - if not src or not src_pos or src_pos == "" or not dst or not dst_pos or dst_pos == "" then + if + not 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" }, + }) + then return end From 65535427b4432da487784e574e5d5485a394d0e3 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Mon, 20 Jun 2022 16:39:46 +1000 Subject: [PATCH 7/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree/legacy.lua | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lua/nvim-tree/legacy.lua b/lua/nvim-tree/legacy.lua index eadbd3c25ec..f19b09acf1e 100644 --- a/lua/nvim-tree/legacy.lua +++ b/lua/nvim-tree/legacy.lua @@ -285,14 +285,8 @@ local function refactored(opts) end end - utils.move_missing_val(opts, "update_to_buf_dir", "enable", opts, "hijack_directories", "enable") - utils.move_missing_val(opts, "update_to_buf_dir", "auto_open", opts, "hijack_directories", "auto_open") - opts.update_to_buf_dir = nil - - utils.move_missing_val(opts, "view", "auto_resize", opts, "actions.open_file", "resize_window") - + -- 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 From f73f8e9f83d6d6afaa21f7545551b235571a4233 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 25 Jun 2022 10:22:41 +1000 Subject: [PATCH 8/8] chore: refactor options with utils move_missing_val --- lua/nvim-tree/utils.lua | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index df18311b046..59fa757a38d 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -272,17 +272,16 @@ end -- @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) - if - not 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" }, - }) - then - return + 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