Skip to content

Commit f92cc3a

Browse files
authored
chore(#3196): remove unused utils and refactor (#3199)
* chore(#3196): remove unused utils.read_file * chore(#3196): move utils.move_missing_val to legacy * chore(#3196): move utils.table_create_missing to legacy
1 parent fefa335 commit f92cc3a

File tree

2 files changed

+60
-77
lines changed

2 files changed

+60
-77
lines changed

lua/nvim-tree/legacy.lua

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,74 @@
1-
local utils = require("nvim-tree.utils")
21
local notify = require("nvim-tree.notify")
32

43
local M = {}
54

5+
--- Create empty sub-tables if not present
6+
---@param tbl table to create empty inside of
7+
---@param path string dot separated string of sub-tables
8+
---@return table deepest sub-table
9+
local function create(tbl, path)
10+
local t = tbl
11+
for s in string.gmatch(path, "([^%.]+)%.*") do
12+
if t[s] == nil then
13+
t[s] = {}
14+
end
15+
t = t[s]
16+
end
17+
18+
return t
19+
end
20+
21+
--- Move a value from src to dst if value is nil on dst.
22+
--- Remove value from src
23+
---@param src table to copy from
24+
---@param src_path string dot separated string of sub-tables
25+
---@param src_pos string value pos
26+
---@param dst table to copy to
27+
---@param dst_path string dot separated string of sub-tables, created when missing
28+
---@param dst_pos string value pos
29+
---@param remove boolean
30+
local function move(src, src_path, src_pos, dst, dst_path, dst_pos, remove)
31+
for pos in string.gmatch(src_path, "([^%.]+)%.*") do
32+
if src[pos] and type(src[pos]) == "table" then
33+
src = src[pos]
34+
else
35+
return
36+
end
37+
end
38+
local src_val = src[src_pos]
39+
if src_val == nil then
40+
return
41+
end
42+
43+
dst = create(dst, dst_path)
44+
if dst[dst_pos] == nil then
45+
dst[dst_pos] = src_val
46+
end
47+
48+
if remove then
49+
src[src_pos] = nil
50+
end
51+
end
52+
653
-- silently move, please add to help nvim-tree-legacy-opts
754
local function refactored(opts)
855
-- 2022/06/20
9-
utils.move_missing_val(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root", true)
10-
utils.move_missing_val(opts, "", "update_cwd", opts, "", "sync_root_with_cwd", true)
56+
move(opts, "update_focused_file", "update_cwd", opts, "update_focused_file", "update_root", true)
57+
move(opts, "", "update_cwd", opts, "", "sync_root_with_cwd", true)
1158

1259
-- 2022/11/07
13-
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "open", false)
14-
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "close", true)
15-
utils.move_missing_val(opts, "", "ignore_buf_on_tab_change", opts, "tab.sync", "ignore", true)
60+
move(opts, "", "open_on_tab", opts, "tab.sync", "open", false)
61+
move(opts, "", "open_on_tab", opts, "tab.sync", "close", true)
62+
move(opts, "", "ignore_buf_on_tab_change", opts, "tab.sync", "ignore", true)
1663

1764
-- 2022/11/22
18-
utils.move_missing_val(opts, "renderer", "root_folder_modifier", opts, "renderer", "root_folder_label", true)
65+
move(opts, "renderer", "root_folder_modifier", opts, "renderer", "root_folder_label", true)
1966

2067
-- 2023/01/01
21-
utils.move_missing_val(opts, "update_focused_file", "debounce_delay", opts, "view", "debounce_delay", true)
68+
move(opts, "update_focused_file", "debounce_delay", opts, "view", "debounce_delay", true)
2269

2370
-- 2023/01/08
24-
utils.move_missing_val(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true)
71+
move(opts, "trash", "require_confirm", opts, "ui.confirm", "trash", true)
2572

2673
-- 2023/01/15
2774
if type(opts.view) == "table" and opts.view.adaptive_size ~= nil then
@@ -35,13 +82,13 @@ local function refactored(opts)
3582
end
3683

3784
-- 2023/07/15
38-
utils.move_missing_val(opts, "", "sort_by", opts, "sort", "sorter", true)
85+
move(opts, "", "sort_by", opts, "sort", "sorter", true)
3986

4087
-- 2023/07/16
41-
utils.move_missing_val(opts, "git", "ignore", opts, "filters", "git_ignored", true)
88+
move(opts, "git", "ignore", opts, "filters", "git_ignored", true)
4289

4390
-- 2023/08/26
44-
utils.move_missing_val(opts, "renderer.icons", "webdev_colors", opts, "renderer.icons.web_devicons.file", "color", true)
91+
move(opts, "renderer.icons", "webdev_colors", opts, "renderer.icons.web_devicons.file", "color", true)
4592

4693
-- 2023/10/08
4794
if type(opts.renderer) == "table" and type(opts.renderer.highlight_diagnostics) == "boolean" then
@@ -59,7 +106,7 @@ local function refactored(opts)
59106
opts.update_focused_file.update_root = { enable = opts.update_focused_file.update_root }
60107
end
61108
end
62-
utils.move_missing_val(opts, "update_focused_file", "ignore_list", opts, "update_focused_file.update_root", "ignore_list", true)
109+
move(opts, "update_focused_file", "ignore_list", opts, "update_focused_file.update_root", "ignore_list", true)
63110

64111
-- 2025/04/30
65112
if opts.renderer and opts.renderer.icons and type(opts.renderer.icons.padding) == "string" then

lua/nvim-tree/utils.lua

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ function M.str_find(haystack, needle)
1717
return vim.fn.stridx(haystack, needle) ~= -1
1818
end
1919

20-
---@param path string
21-
---@return string|uv.uv_fs_t
22-
function M.read_file(path)
23-
local fd = vim.loop.fs_open(path, "r", 438)
24-
if not fd then
25-
return ""
26-
end
27-
local stat = vim.loop.fs_fstat(fd)
28-
if not stat then
29-
return ""
30-
end
31-
local data = vim.loop.fs_read(fd, stat.size, 0)
32-
vim.loop.fs_close(fd)
33-
return data or ""
34-
end
35-
3620
local path_separator = package.config:sub(1, 1)
3721
---@param paths string[]
3822
---@return string
@@ -377,54 +361,6 @@ function M.escape_special_chars(path)
377361
return M.is_windows and escape_special_char_for_windows(path) or path
378362
end
379363

380-
--- Create empty sub-tables if not present
381-
---@param tbl table to create empty inside of
382-
---@param path string dot separated string of sub-tables
383-
---@return table deepest sub-table
384-
function M.table_create_missing(tbl, path)
385-
local t = tbl
386-
for s in string.gmatch(path, "([^%.]+)%.*") do
387-
if t[s] == nil then
388-
t[s] = {}
389-
end
390-
t = t[s]
391-
end
392-
393-
return t
394-
end
395-
396-
--- Move a value from src to dst if value is nil on dst.
397-
--- Remove value from src
398-
---@param src table to copy from
399-
---@param src_path string dot separated string of sub-tables
400-
---@param src_pos string value pos
401-
---@param dst table to copy to
402-
---@param dst_path string dot separated string of sub-tables, created when missing
403-
---@param dst_pos string value pos
404-
---@param remove boolean
405-
function M.move_missing_val(src, src_path, src_pos, dst, dst_path, dst_pos, remove)
406-
for pos in string.gmatch(src_path, "([^%.]+)%.*") do
407-
if src[pos] and type(src[pos]) == "table" then
408-
src = src[pos]
409-
else
410-
return
411-
end
412-
end
413-
local src_val = src[src_pos]
414-
if src_val == nil then
415-
return
416-
end
417-
418-
dst = M.table_create_missing(dst, dst_path)
419-
if dst[dst_pos] == nil then
420-
dst[dst_pos] = src_val
421-
end
422-
423-
if remove then
424-
src[src_pos] = nil
425-
end
426-
end
427-
428364
local function round(value)
429365
-- Amount of digits to round to after floating point.
430366
local digits = 2

0 commit comments

Comments
 (0)