Skip to content

Commit 0c13bd7

Browse files
authored
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
1 parent b299a87 commit 0c13bd7

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
lines changed

doc/nvim-tree-lua.txt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Setup may only be run once; subsequent calls will result in a warning.
167167
sort_by = "name",
168168
root_dirs = {},
169169
prefer_startup_root = false,
170-
update_cwd = false,
170+
sync_root_with_cwd = false,
171171
reload_on_bufenter = false,
172172
respect_buf_cwd = false,
173173
view = {
@@ -247,7 +247,6 @@ Setup may only be run once; subsequent calls will result in a warning.
247247
},
248248
update_focused_file = {
249249
enable = false,
250-
update_cwd = false,
251250
update_root = false,
252251
ignore_list = {},
253252
},
@@ -398,7 +397,7 @@ Prefer startup root directory when updating root directory of the tree.
398397
Only relevant when `update_focused_file.update_root` is `true`
399398
Type: `boolean`, Default: `false`
400399

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

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

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

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

434-
*nvim-tree.update_focused_file.update_cwd*
435-
(deprecated, use `update_focused_file.update_root`)
436-
Update the root directory of the tree to the one of the folder containing
437-
the file if the file is not under the current root directory.
438-
Only relevant when `update_focused_file.enable` is `true`
439-
Type: `boolean`, Default: `false`
440-
441-
*nvim-tree.update_focused_file.update_root*
433+
*nvim-tree.update_focused_file.update_root* (previously `update_focused_file.update_cwd`)
442434
Update the root directory of the tree if the file is not under current
443435
root directory. It prefers vim's cwd and `root_dirs`.
444436
Otherwise it falls back to the folder containing the file.
@@ -448,7 +440,7 @@ until it finds the file.
448440
*nvim-tree.update_focused_file.ignore_list*
449441
List of buffer names and filetypes that will not update the root dir
450442
of the tree if the file isn't found under the current root directory.
451-
Only relevant when `update_focused_file.update_cwd` and
443+
Only relevant when `update_focused_file.update_root` and
452444
`update_focused_file.enable` are `true`.
453445
Type: {string}, Default: `{}`
454446

@@ -768,7 +760,7 @@ Configuration for various actions.
768760

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

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

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

lua/nvim-tree.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function M.find_file(with_open, bufnr, bang)
159159

160160
-- if we don't schedule, it will search for NvimTree
161161
vim.schedule(function()
162-
if bang or _config.update_focused_file.update_cwd or _config.update_focused_file.update_root then
162+
if bang or _config.update_focused_file.update_root then
163163
M.change_root(filepath, bufnr)
164164
end
165165
require("nvim-tree.actions.find-file").fn(filepath)
@@ -351,7 +351,7 @@ local function setup_autocommands(opts)
351351
if opts.hijack_cursor then
352352
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
353353
end
354-
if opts.update_cwd then
354+
if opts.sync_root_with_cwd then
355355
create_nvim_tree_autocmd("DirChanged", {
356356
callback = function()
357357
M.change_dir(vim.loop.cwd())
@@ -407,7 +407,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
407407
sort_by = "name",
408408
root_dirs = {},
409409
prefer_startup_root = false,
410-
update_cwd = false,
410+
sync_root_with_cwd = false,
411411
reload_on_bufenter = false,
412412
respect_buf_cwd = false,
413413
view = {
@@ -487,7 +487,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
487487
},
488488
update_focused_file = {
489489
enable = false,
490-
update_cwd = false,
491490
update_root = false,
492491
ignore_list = {},
493492
},

lua/nvim-tree/legacy.lua

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,26 +285,9 @@ local function refactored(opts)
285285
end
286286
end
287287

288-
-- update_to_buf_dir -> hijack_directories
289-
if opts.update_to_buf_dir ~= nil then
290-
utils.table_create_missing(opts, "hijack_directories")
291-
if opts.hijack_directories.enable == nil then
292-
opts.hijack_directories.enable = opts.update_to_buf_dir.enable
293-
end
294-
if opts.hijack_directories.auto_open == nil then
295-
opts.hijack_directories.auto_open = opts.update_to_buf_dir.auto_open
296-
end
297-
opts.update_to_buf_dir = nil
298-
end
299-
300-
-- view.auto_resize -> actions.open_file.resize_window
301-
if opts.view and opts.view.auto_resize ~= nil then
302-
utils.table_create_missing(opts, "actions.open_file")
303-
if opts.actions.open_file.resize_window == nil then
304-
opts.actions.open_file.resize_window = opts.view.auto_resize
305-
end
306-
opts.view.auto_resize = nil
307-
end
288+
-- 2022/06/20
289+
utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root")
290+
utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd")
308291
end
309292

310293
local function removed(opts)

lua/nvim-tree/utils.lua

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,15 @@ end
246246

247247
-- Create empty sub-tables if not present
248248
-- @param tbl to create empty inside of
249-
-- @param sub dot separated string of sub-tables
249+
-- @param path dot separated string of sub-tables
250250
-- @return deepest sub-table
251-
function M.table_create_missing(tbl, sub)
251+
function M.table_create_missing(tbl, path)
252252
if tbl == nil then
253253
return nil
254254
end
255255

256256
local t = tbl
257-
for s in string.gmatch(sub, "([^%.]+)%.*") do
257+
for s in string.gmatch(path, "([^%.]+)%.*") do
258258
if t[s] == nil then
259259
t[s] = {}
260260
end
@@ -264,6 +264,47 @@ function M.table_create_missing(tbl, sub)
264264
return t
265265
end
266266

267+
-- Move a value from src to dst if value is nil on dst
268+
-- @param src to copy from
269+
-- @param src_path dot separated string of sub-tables
270+
-- @param src_pos value pos
271+
-- @param dst to copy to
272+
-- @param dst_path dot separated string of sub-tables, created when missing
273+
-- @param dst_pos value pos
274+
function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos)
275+
local ok, err = pcall(vim.validate, {
276+
src = { src, "table" },
277+
src_path = { src_path, "string" },
278+
src_pos = { src_pos, "string" },
279+
dst = { dst, "table" },
280+
dst_path = { dst_path, "string" },
281+
dst_pos = { dst_pos, "string" },
282+
})
283+
if not ok then
284+
M.warn("move_missing_val: " .. (err or "invalid arguments"))
285+
end
286+
287+
for pos in string.gmatch(src_path, "([^%.]+)%.*") do
288+
if src[pos] and type(src[pos]) == "table" then
289+
src = src[pos]
290+
else
291+
src = nil
292+
break
293+
end
294+
end
295+
local src_val = src and src[src_pos]
296+
if src_val == nil then
297+
return
298+
end
299+
300+
dst = M.table_create_missing(dst, dst_path)
301+
if dst[dst_pos] == nil then
302+
dst[dst_pos] = src_val
303+
end
304+
305+
src[src_pos] = nil
306+
end
307+
267308
function M.format_bytes(bytes)
268309
local units = { "B", "K", "M", "G", "T" }
269310

0 commit comments

Comments
 (0)