Skip to content

Commit 7829d7c

Browse files
committed
refacto: move set_index_and_redraw -> actions.find-file.fn
1 parent 23c95a6 commit 7829d7c

File tree

5 files changed

+76
-58
lines changed

5 files changed

+76
-58
lines changed

lua/nvim-tree.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function M.find_file(with_open)
143143
end
144144

145145
update_base_dir_with_filepath(filepath, bufnr)
146-
lib.set_index_and_redraw(filepath)
146+
require"nvim-tree.actions.find-file".fn(filepath)
147147
end
148148

149149
function M.resize(size)
@@ -192,7 +192,7 @@ function M.open_on_directory()
192192
view.focus()
193193
view.replace_window()
194194

195-
lib.set_index_and_redraw(bufname)
195+
require"nvim-tree.actions.find-file".fn(bufname)
196196
vim.api.nvim_buf_delete(buf, { force = true })
197197
end
198198

lua/nvim-tree/actions/dir-up.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function M.fn(node)
66
else
77
local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h')
88
require'nvim-tree.actions.change-dir'.fn(newdir)
9-
return require'nvim-tree.lib'.set_index_and_redraw(node.absolute_path)
9+
return require"nvim-tree.actions.find-file".fn(node.absolute_path)
1010
end
1111
end
1212

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
local view = require'nvim-tree.view'
2+
local utils = require'nvim-tree.utils'
3+
local explorer_module = require"nvim-tree.explorer"
4+
local git = require"nvim-tree.git"
5+
6+
local M = {}
7+
8+
local function get_explorer()
9+
return require"nvim-tree.lib".Tree
10+
end
11+
12+
function M.fn(fname)
13+
local i
14+
local hide_root_folder = view.View.hide_root_folder
15+
local Explorer = get_explorer()
16+
if Explorer.cwd == '/' or hide_root_folder then
17+
i = 0
18+
else
19+
i = 1
20+
end
21+
22+
local tree_altered = false
23+
24+
local function iterate_nodes(nodes)
25+
for _, node in ipairs(nodes) do
26+
i = i + 1
27+
if node.absolute_path == fname then
28+
return i
29+
end
30+
31+
local path_matches = utils.str_find(fname, node.absolute_path..utils.path_separator)
32+
if path_matches then
33+
if #node.nodes == 0 then
34+
node.open = true
35+
explorer_module.explore(node.nodes, node.absolute_path, node, {})
36+
git.load_project_status(node.absolute_path, function(status)
37+
if status.dirs or status.files then
38+
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
39+
end
40+
require"nvim-tree.lib".redraw()
41+
end)
42+
end
43+
if node.open == false then
44+
node.open = true
45+
tree_altered = true
46+
end
47+
if iterate_nodes(node.nodes) ~= nil then
48+
return i
49+
end
50+
elseif node.open == true then
51+
iterate_nodes(node.nodes)
52+
end
53+
end
54+
end
55+
56+
local index = iterate_nodes(Explorer.nodes)
57+
if tree_altered then
58+
require"nvim-tree.lib".redraw()
59+
end
60+
if index and view.win_open() then
61+
view.set_cursor({index, 0})
62+
end
63+
end
64+
65+
return M

lua/nvim-tree/actions/reloaders.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ local git = require "nvim-tree.git"
22
local diagnostics = require "nvim-tree.diagnostics"
33
local view = require "nvim-tree.view"
44
local explorer_module = require'nvim-tree.explorer'
5-
local get_explorer = function() return require "nvim-tree.lib".Tree end
65

76
local M = {}
87

8+
local function get_explorer()
9+
return require "nvim-tree.lib".Tree
10+
end
11+
912
local function refresh_nodes(node, projects)
1013
local project_root = git.get_project_root(node.absolute_path or node.cwd)
1114
explorer_module.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {})

lua/nvim-tree/lib.lua

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local luv = vim.loop
44
local renderer = require'nvim-tree.renderer'
55
local diagnostics = require'nvim-tree.diagnostics'
66
local explorer = require'nvim-tree.explorer'
7-
local utils = require'nvim-tree.utils'
87
local view = require'nvim-tree.view'
98
local events = require'nvim-tree.events'
109
local git = require'nvim-tree.git'
@@ -112,58 +111,6 @@ function M.expand_or_collapse(node)
112111
diagnostics.update()
113112
end
114113

115-
function M.set_index_and_redraw(fname)
116-
local i
117-
local hide_root_folder = view.View.hide_root_folder
118-
if M.Tree.cwd == '/' or hide_root_folder then
119-
i = 0
120-
else
121-
i = 1
122-
end
123-
124-
local tree_altered = false
125-
126-
local function iterate_nodes(nodes)
127-
for _, node in ipairs(nodes) do
128-
i = i + 1
129-
if node.absolute_path == fname then
130-
return i
131-
end
132-
133-
local path_matches = utils.str_find(fname, node.absolute_path..utils.path_separator)
134-
if path_matches then
135-
if #node.nodes == 0 then
136-
node.open = true
137-
explorer.explore(node.nodes, node.absolute_path, node, {})
138-
git.load_project_status(node.absolute_path, function(status)
139-
if status.dirs or status.files then
140-
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
141-
end
142-
M.redraw()
143-
end)
144-
end
145-
if node.open == false then
146-
node.open = true
147-
tree_altered = true
148-
end
149-
if iterate_nodes(node.nodes) ~= nil then
150-
return i
151-
end
152-
elseif node.open == true then
153-
iterate_nodes(node.nodes)
154-
end
155-
end
156-
end
157-
158-
local index = iterate_nodes(M.Tree.nodes)
159-
if tree_altered then
160-
M.redraw()
161-
end
162-
if index and view.win_open() then
163-
view.set_cursor({index, 0})
164-
end
165-
end
166-
167114
function M.set_target_win()
168115
local id = api.nvim_get_current_win()
169116
local tree_id = view.get_winnr()
@@ -217,6 +164,9 @@ M.dir_up = require'nvim-tree.actions.dir-up'.fn
217164
M.change_dir = require'nvim-tree.actions.change-dir'.fn
218165
-- @deprecated: use nvim-tree.actions.reloaders.reload_explorer
219166
M.refresh_tree = require'nvim-tree.actions.reloaders'.reload_explorer
220-
167+
-- @deprecated: use nvim-tree.actions.reloaders.reload_git
168+
M.reload_git = require'nvim-tree.actions.reloaders'.reload_git
169+
-- @deprecated: use nvim-tree.actions.find-file.fn
170+
M.set_index_and_redraw = require'nvim-tree.actions.find-file'.fn
221171

222172
return M

0 commit comments

Comments
 (0)