Skip to content

Commit 206d173

Browse files
committed
extract methods from explorer
1 parent 62b76b3 commit 206d173

File tree

4 files changed

+59
-71
lines changed

4 files changed

+59
-71
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,6 @@ function Explorer:reload(node, git_status)
186186
return node.nodes
187187
end
188188

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

235215
self:reload(node, project)
236-
self:update_parent_statuses(node, project, toplevel)
216+
node:update_parent_statuses(project, toplevel)
237217
end
238218

239219
log.profile_end(profile)
@@ -261,52 +241,6 @@ function Explorer:update_status(nodes_by_path, node_ignored, status)
261241
end
262242
end
263243

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local rename_file = require("nvim-tree.actions.fs.rename-file")
88
local trash = require("nvim-tree.actions.fs.trash")
99
local utils = require("nvim-tree.utils")
1010

11-
local DirectoryNode = require "nvim-tree.node.directory"
11+
local DirectoryNode = require("nvim-tree.node.directory")
1212

1313
---@class Marks
1414
---@field config table hydrated user opts.filters

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)