Skip to content

Commit d6974e9

Browse files
committed
extract methods from explore_node
1 parent fee6176 commit d6974e9

File tree

5 files changed

+60
-61
lines changed

5 files changed

+60
-61
lines changed

lua/nvim-tree/explorer/explore.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local utils = require "nvim-tree.utils"
2-
local explorer_node = require "nvim-tree.explorer.node"
32
local git = require "nvim-tree.git"
43
local log = require "nvim-tree.log"
54

@@ -65,7 +64,7 @@ local function populate_children(handle, cwd, node, git_status, parent)
6564
if child then
6665
table.insert(node.nodes, child)
6766
nodes_by_path[child.absolute_path] = true
68-
explorer_node.update_git_status(child, node_ignored, git_status)
67+
child:update_git_status(node_ignored, git_status)
6968
end
7069
end
7170
else

lua/nvim-tree/explorer/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ end
279279
function Explorer:update_status(nodes_by_path, node_ignored, status)
280280
return function(node)
281281
if nodes_by_path[node.absolute_path] then
282-
explorer_node.update_git_status(node, node_ignored, status)
282+
node:update_git_status(node_ignored, status)
283283
end
284284
return node
285285
end
@@ -324,7 +324,7 @@ function Explorer:update_parent_statuses(node, project, root)
324324
end
325325

326326
-- update status
327-
explorer_node.update_git_status(node, node.parent and node.parent:is_git_ignored() or false, project)
327+
node:update_git_status(node.parent and node.parent:is_git_ignored() or false, project)
328328

329329
-- maybe parent
330330
node = node.parent
@@ -380,7 +380,7 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent)
380380
if child then
381381
table.insert(node.nodes, child)
382382
nodes_by_path[child.absolute_path] = true
383-
explorer_node.update_git_status(child, node_ignored, git_status)
383+
child:update_git_status(node_ignored, git_status)
384384
end
385385
else
386386
for reason, value in pairs(FILTER_REASON) do

lua/nvim-tree/explorer/node.lua

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,6 @@ local git = {} -- circular dependencies
22

33
local M = {}
44

5-
---@class GitStatus
6-
---@field file string|nil
7-
---@field dir table|nil
8-
9-
---@param parent_ignored boolean
10-
---@param status table|nil
11-
---@param absolute_path string
12-
---@return GitStatus|nil
13-
local function get_dir_git_status(parent_ignored, status, absolute_path)
14-
if parent_ignored then
15-
return { file = "!!" }
16-
end
17-
18-
if status then
19-
return {
20-
file = status.files and status.files[absolute_path],
21-
dir = status.dirs and {
22-
direct = status.dirs.direct[absolute_path],
23-
indirect = status.dirs.indirect[absolute_path],
24-
},
25-
}
26-
end
27-
end
28-
29-
---@param parent_ignored boolean
30-
---@param status table
31-
---@param absolute_path string
32-
---@return GitStatus
33-
local function get_git_status(parent_ignored, status, absolute_path)
34-
local file_status = parent_ignored and "!!" or (status and status.files and status.files[absolute_path])
35-
return { file = file_status }
36-
end
37-
38-
---@param node Node
39-
---@param parent_ignored boolean
40-
---@param status table|nil
41-
function M.update_git_status(node, parent_ignored, status)
42-
local get_status
43-
if node.nodes then
44-
get_status = get_dir_git_status
45-
else
46-
get_status = get_git_status
47-
end
48-
49-
-- status of the node's absolute path
50-
node.git_status = get_status(parent_ignored, status, node.absolute_path)
51-
52-
-- status of the link target, if the link itself is not dirty
53-
if node.link_to and not node.git_status then
54-
node.git_status = get_status(parent_ignored, status, node.link_to)
55-
end
56-
end
57-
585
---@param node Node
596
---@return GitStatus|nil
607
function M.get_git_status(node)
@@ -128,7 +75,7 @@ function M.reload_node_status(parent_node, projects)
12875
local toplevel = git.get_toplevel(parent_node.absolute_path)
12976
local status = projects[toplevel] or {}
13077
for _, node in ipairs(parent_node.nodes) do
131-
M.update_git_status(node, M.is_git_ignored(parent_node), status)
78+
node:update_git_status(M.is_git_ignored(parent_node), status)
13279
if node.nodes and #node.nodes > 0 then
13380
M.reload_node_status(node, projects)
13481
end

lua/nvim-tree/git/init.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ local git_utils = require "nvim-tree.git.utils"
44
local Runner = require "nvim-tree.git.runner"
55
local Watcher = require("nvim-tree.watcher").Watcher
66
local Iterator = require "nvim-tree.iterators.node-iterator"
7-
local explorer_node = require "nvim-tree.explorer.node"
7+
8+
---@class GitStatus
9+
---@field file string|nil
10+
---@field dir table|nil
811

912
local M = {
1013
config = {},
@@ -209,7 +212,7 @@ local function reload_tree_at(toplevel)
209212
:hidden()
210213
:applier(function(node)
211214
local parent_ignored = node.parent and node.parent:is_git_ignored() or false
212-
explorer_node.update_git_status(node, parent_ignored, git_status)
215+
node:update_git_status(parent_ignored, git_status)
213216
end)
214217
:recursor(function(node)
215218
return node.nodes and #node.nodes > 0 and node.nodes
@@ -283,6 +286,35 @@ function M.load_project_status(path)
283286
end
284287
end
285288

289+
---@param parent_ignored boolean
290+
---@param status table|nil
291+
---@param absolute_path string
292+
---@return GitStatus|nil
293+
function M.get_dir_git_status(parent_ignored, status, absolute_path)
294+
if parent_ignored then
295+
return { file = "!!" }
296+
end
297+
298+
if status then
299+
return {
300+
file = status.files and status.files[absolute_path],
301+
dir = status.dirs and {
302+
direct = status.dirs.direct[absolute_path],
303+
indirect = status.dirs.indirect[absolute_path],
304+
},
305+
}
306+
end
307+
end
308+
309+
---@param parent_ignored boolean
310+
---@param status table
311+
---@param absolute_path string
312+
---@return GitStatus
313+
function M.get_git_status(parent_ignored, status, absolute_path)
314+
local file_status = parent_ignored and "!!" or (status and status.files and status.files[absolute_path])
315+
return { file = file_status }
316+
end
317+
286318
function M.purge_state()
287319
log.line("git", "purge_state")
288320

lua/nvim-tree/node/init.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local git = require "nvim-tree.git"
2+
13
---@class (exact) BaseNode
24
---@field private __index? table
35
---@field type NODE_TYPE
@@ -39,6 +41,25 @@ function BaseNode:has_one_child_folder()
3941
return #self.nodes == 1 and self.nodes[1].nodes and vim.loop.fs_access(self.nodes[1].absolute_path, "R") or false
4042
end
4143

44+
---@param parent_ignored boolean
45+
---@param status table|nil
46+
function BaseNode:update_git_status(parent_ignored, status)
47+
local get_status
48+
if self.nodes then
49+
get_status = git.get_dir_git_status
50+
else
51+
get_status = git.get_git_status
52+
end
53+
54+
-- status of the node's absolute path
55+
self.git_status = get_status(parent_ignored, status, self.absolute_path)
56+
57+
-- status of the link target, if the link itself is not dirty
58+
if self.link_to and not self.git_status then
59+
self.git_status = get_status(parent_ignored, status, self.link_to)
60+
end
61+
end
62+
4263
---@return boolean
4364
function BaseNode:is_git_ignored()
4465
return self.git_status ~= nil and self.git_status.file == "!!"

0 commit comments

Comments
 (0)