Skip to content

Commit 66b9da3

Browse files
committed
extract methods from explorer
1 parent f998996 commit 66b9da3

File tree

3 files changed

+58
-70
lines changed

3 files changed

+58
-70
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,6 @@ function Explorer:reload(node, git_status)
183183
return node.nodes
184184
end
185185

186-
---TODO #2837 #2871 #2886 move this and similar to node
187-
---Refresh contents and git status for a single node
188-
---@param node Node
189-
---@param callback function
190-
function Explorer:refresh_node(node, callback)
191-
if type(node) ~= "table" then
192-
callback()
193-
end
194-
195-
local parent_node = utils.get_parent_of_group(node)
196-
197-
self:reload_and_get_git_project(node.absolute_path, function(toplevel, project)
198-
self:reload(parent_node, project)
199-
200-
self:update_parent_statuses(parent_node, project, toplevel)
201-
202-
callback()
203-
end)
204-
end
205-
206186
---Refresh contents of all nodes to a path: actual directory and links.
207187
---Groups will be expanded if needed.
208188
---@param path string absolute path
@@ -230,7 +210,7 @@ function Explorer:refresh_parent_nodes_for_path(path)
230210
local project = git.get_project(toplevel) or {}
231211

232212
self:reload(node, project)
233-
self:update_parent_statuses(node, project, toplevel)
213+
node:update_parent_statuses(project, toplevel)
234214
end
235215

236216
log.profile_end(profile)
@@ -258,52 +238,6 @@ function Explorer:update_status(nodes_by_path, node_ignored, status)
258238
end
259239
end
260240

261-
---TODO #2837 #2871 #2886 move this and similar to node
262-
---@private
263-
---@param path string
264-
---@param callback fun(toplevel: string|nil, project: table|nil)
265-
function Explorer:reload_and_get_git_project(path, callback)
266-
local toplevel = git.get_toplevel(path)
267-
268-
git.reload_project(toplevel, path, function()
269-
callback(toplevel, git.get_project(toplevel) or {})
270-
end)
271-
end
272-
273-
---TODO #2837 #2871 #2886 move this and similar to node
274-
---@private
275-
---@param node Node
276-
---@param project table|nil
277-
---@param root string|nil
278-
function Explorer:update_parent_statuses(node, project, root)
279-
while project and node do
280-
-- step up to the containing project
281-
if node.absolute_path == root then
282-
-- stop at the top of the tree
283-
if not node.parent then
284-
break
285-
end
286-
287-
root = git.get_toplevel(node.parent.absolute_path)
288-
289-
-- stop when no more projects
290-
if not root then
291-
break
292-
end
293-
294-
-- update the containing project
295-
project = git.get_project(root)
296-
git.reload_project(root, node.absolute_path, nil)
297-
end
298-
299-
-- update status
300-
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
301-
302-
-- maybe parent
303-
node = node.parent
304-
end
305-
end
306-
307241
---@private
308242
---@param handle uv.uv_fs_t
309243
---@param cwd string

lua/nvim-tree/explorer/watch.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ function M.create_watcher(node)
7676
else
7777
log.line("watcher", "node event executing refresh '%s'", node.absolute_path)
7878
end
79-
node.explorer:refresh_node(node, function()
80-
node.explorer.renderer:draw()
81-
end)
79+
node:refresh()
8280
end)
8381
end
8482

lua/nvim-tree/node/init.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local git = require "nvim-tree.git"
2+
local utils = require "nvim-tree.utils"
23

34
---Abstract Node class.
45
---Uses the abstract factory pattern to instantiate child instances.
@@ -182,4 +183,59 @@ function BaseNode:last_group_node()
182183
return node
183184
end
184185

186+
---@param path string
187+
---@param callback fun(toplevel: string|nil, project: table|nil)
188+
function BaseNode:reload_and_get_git_project(path, callback)
189+
local toplevel = git.get_toplevel(path)
190+
191+
git.reload_project(toplevel, path, function()
192+
callback(toplevel, git.get_project(toplevel) or {})
193+
end)
194+
end
195+
196+
---@param project table|nil
197+
---@param root string|nil
198+
function BaseNode:update_parent_statuses(project, root)
199+
local node = self
200+
while project and node do
201+
-- step up to the containing project
202+
if node.absolute_path == root then
203+
-- stop at the top of the tree
204+
if not node.parent then
205+
break
206+
end
207+
208+
root = git.get_toplevel(node.parent.absolute_path)
209+
210+
-- stop when no more projects
211+
if not root then
212+
break
213+
end
214+
215+
-- update the containing project
216+
project = git.get_project(root)
217+
git.reload_project(root, node.absolute_path, nil)
218+
end
219+
220+
-- update status
221+
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
222+
223+
-- maybe parent
224+
node = node.parent
225+
end
226+
end
227+
228+
---Refresh contents and git status for a single node
229+
function BaseNode:refresh()
230+
local parent_node = utils.get_parent_of_group(self)
231+
232+
self:reload_and_get_git_project(self.absolute_path, function(toplevel, project)
233+
self.explorer:reload(parent_node, project)
234+
235+
parent_node:update_parent_statuses(project, toplevel)
236+
237+
self.explorer.renderer:draw()
238+
end)
239+
end
240+
185241
return BaseNode

0 commit comments

Comments
 (0)