Skip to content

Commit 7d65694

Browse files
committed
extract methods from explore_node
1 parent d6974e9 commit 7d65694

File tree

6 files changed

+82
-118
lines changed

6 files changed

+82
-118
lines changed

lua/nvim-tree/actions/moves/item.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
33
local core = require "nvim-tree.core"
44
local lib = require "nvim-tree.lib"
5-
local explorer_node = require "nvim-tree.explorer.node"
65
local diagnostics = require "nvim-tree.diagnostics"
76

87
local M = {}
@@ -16,7 +15,7 @@ local MAX_DEPTH = 100
1615
---@return boolean
1716
local function status_is_valid(node, what, skip_gitignored)
1817
if what == "git" then
19-
local git_status = explorer_node.get_git_status(node)
18+
local git_status = node:get_git_status()
2019
return git_status ~= nil and (not skip_gitignored or git_status[1] ~= "!!")
2120
elseif what == "diag" then
2221
local diag_status = diagnostics.get_diag_status(node)

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local log = require "nvim-tree.log"
33
local notify = require "nvim-tree.notify"
44
local utils = require "nvim-tree.utils"
55
local view = require "nvim-tree.view"
6-
local explorer_node = require "nvim-tree.explorer.node"
76

87
local BaseNode = require "nvim-tree.node"
98
local DirectoryNode = require "nvim-tree.node.directory"
@@ -469,14 +468,13 @@ function Explorer:reload_git()
469468
event_running = true
470469

471470
local projects = git.reload()
472-
explorer_node.reload_node_status(self, projects)
471+
self:reload_node_status(projects)
473472
self.renderer:draw()
474473
event_running = false
475474
end
476475

477476
function Explorer:setup(opts)
478477
config = opts
479-
require("nvim-tree.explorer.node").setup(opts)
480478
require("nvim-tree.explorer.watch").setup(opts)
481479
end
482480

lua/nvim-tree/explorer/node.lua

Lines changed: 0 additions & 105 deletions
This file was deleted.

lua/nvim-tree/git/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ end
290290
---@param status table|nil
291291
---@param absolute_path string
292292
---@return GitStatus|nil
293-
function M.get_dir_git_status(parent_ignored, status, absolute_path)
293+
function M.git_status_dir(parent_ignored, status, absolute_path)
294294
if parent_ignored then
295295
return { file = "!!" }
296296
end
@@ -310,7 +310,7 @@ end
310310
---@param status table
311311
---@param absolute_path string
312312
---@return GitStatus
313-
function M.get_git_status(parent_ignored, status, absolute_path)
313+
function M.git_status_file(parent_ignored, status, absolute_path)
314314
local file_status = parent_ignored and "!!" or (status and status.files and status.files[absolute_path])
315315
return { file = file_status }
316316
end

lua/nvim-tree/node/init.lua

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ local git = require "nvim-tree.git"
1616
---@field diag_status DiagStatus|nil
1717
local BaseNode = {}
1818

19-
---@alias Node DirectoryNode|FileNode|LinkNode
19+
---@alias Node BaseNode|DirectoryNode|FileNode|LinkNode
2020

2121
---@param o BaseNode|nil
2222
---@return BaseNode
@@ -46,9 +46,9 @@ end
4646
function BaseNode:update_git_status(parent_ignored, status)
4747
local get_status
4848
if self.nodes then
49-
get_status = git.get_dir_git_status
49+
get_status = git.git_status_file
5050
else
51-
get_status = git.get_git_status
51+
get_status = git.git_status_file
5252
end
5353

5454
-- status of the node's absolute path
@@ -60,6 +60,79 @@ function BaseNode:update_git_status(parent_ignored, status)
6060
end
6161
end
6262

63+
---@return GitStatus|nil
64+
function BaseNode:get_git_status()
65+
if not self.git_status then
66+
-- status doesn't exist
67+
return nil
68+
end
69+
70+
if not self.nodes then
71+
-- file
72+
return self.git_status.file and { self.git_status.file }
73+
end
74+
75+
-- dir
76+
if not self.explorer.opts.git.show_on_dirs then
77+
return nil
78+
end
79+
80+
local status = {}
81+
if not require("nvim-tree.lib").get_last_group_node(self).open or self.explorer.opts.git.show_on_open_dirs then
82+
-- dir is closed or we should show on open_dirs
83+
if self.git_status.file ~= nil then
84+
table.insert(status, self.git_status.file)
85+
end
86+
if self.git_status.dir ~= nil then
87+
if self.git_status.dir.direct ~= nil then
88+
for _, s in pairs(self.git_status.dir.direct) do
89+
table.insert(status, s)
90+
end
91+
end
92+
if self.git_status.dir.indirect ~= nil then
93+
for _, s in pairs(self.git_status.dir.indirect) do
94+
table.insert(status, s)
95+
end
96+
end
97+
end
98+
else
99+
-- dir is open and we shouldn't show on open_dirs
100+
if self.git_status.file ~= nil then
101+
table.insert(status, self.git_status.file)
102+
end
103+
if self.git_status.dir ~= nil and self.git_status.dir.direct ~= nil then
104+
local deleted = {
105+
[" D"] = true,
106+
["D "] = true,
107+
["RD"] = true,
108+
["DD"] = true,
109+
}
110+
for _, s in pairs(self.git_status.dir.direct) do
111+
if deleted[s] then
112+
table.insert(status, s)
113+
end
114+
end
115+
end
116+
end
117+
if #status == 0 then
118+
return nil
119+
else
120+
return status
121+
end
122+
end
123+
124+
---@param projects table
125+
function BaseNode:reload_node_status(projects)
126+
local toplevel = git.get_toplevel(self.absolute_path)
127+
local status = projects[toplevel] or {}
128+
for _, node in ipairs(self.nodes) do
129+
node:update_git_status(self:is_git_ignored(), status)
130+
if node.nodes and #node.nodes > 0 then
131+
self:reload_node_status(projects)
132+
end
133+
end
134+
end
135+
63136
---@return boolean
64137
function BaseNode:is_git_ignored()
65138
return self.git_status ~= nil and self.git_status.file == "!!"

lua/nvim-tree/renderer/decorator/git.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local notify = require "nvim-tree.notify"
2-
local explorer_node = require "nvim-tree.explorer.node"
32

43
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
54
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
@@ -147,7 +146,7 @@ function DecoratorGit:calculate_icons(node)
147146
return nil
148147
end
149148

150-
local git_status = explorer_node.get_git_status(node)
149+
local git_status = node:get_git_status()
151150
if git_status == nil then
152151
return nil
153152
end
@@ -208,7 +207,7 @@ function DecoratorGit:calculate_highlight(node)
208207
return nil
209208
end
210209

211-
local git_status = explorer_node.get_git_status(node)
210+
local git_status = node:get_git_status()
212211
if not git_status then
213212
return nil
214213
end

0 commit comments

Comments
 (0)