Skip to content

Commit e3a2eed

Browse files
committed
extract node factory, remove unused code
1 parent b318681 commit e3a2eed

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
local git = require("nvim-tree.git")
2-
local log = require("nvim-tree.log")
3-
local notify = require("nvim-tree.notify")
4-
local utils = require("nvim-tree.utils")
5-
local view = require("nvim-tree.view")
6-
7-
local BaseNode = require("nvim-tree.node")
8-
local DirectoryNode = require("nvim-tree.node.directory")
9-
local FileNode = require("nvim-tree.node.file")
10-
local LinkNode = require("nvim-tree.node.link")
11-
local Watcher = require("nvim-tree.watcher")
1+
local git = require "nvim-tree.git"
2+
local log = require "nvim-tree.log"
3+
local notify = require "nvim-tree.notify"
4+
local utils = require "nvim-tree.utils"
5+
local view = require "nvim-tree.view"
6+
local node_factory = require "nvim-tree.node.factory"
7+
8+
local BaseNode = require "nvim-tree.node"
9+
local DirectoryNode = require "nvim-tree.node.directory"
10+
local Watcher = require "nvim-tree.watcher"
1211

1312
local Iterator = require("nvim-tree.iterators.node-iterator")
1413
local NodeIterator = require("nvim-tree.iterators.node-iterator")
@@ -150,17 +149,7 @@ function Explorer:reload(node, git_status)
150149
end
151150

152151
if not nodes_by_path[abs] then
153-
local new_child = nil
154-
if t == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
155-
new_child = DirectoryNode:new(self, node, abs, name, stat)
156-
elseif t == "file" then
157-
new_child = FileNode:new(self, node, abs, name, stat)
158-
elseif t == "link" then
159-
local link = LinkNode:new(self, node, abs, name, stat)
160-
if link.link_to ~= nil then
161-
new_child = link
162-
end
163-
end
152+
local new_child = node_factory.create_node(self, node, abs, stat, name)
164153
if new_child then
165154
table.insert(node.nodes, new_child)
166155
nodes_by_path[abs] = new_child
@@ -365,6 +354,7 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent)
365354
local stat = vim.loop.fs_lstat(abs)
366355
local filter_reason = parent.filters:should_filter_as_reason(abs, stat, filter_status)
367356
if filter_reason == FILTER_REASON.none and not nodes_by_path[abs] then
357+
<<<<<<< HEAD
368358
-- Type must come from fs_stat and not fs_scandir_next to maintain sshfs compatibility
369359
local t = stat and stat.type or nil
370360
local child = nil
@@ -378,6 +368,9 @@ function Explorer:populate_children(handle, cwd, node, git_status, parent)
378368
child = link
379369
end
380370
end
371+
=======
372+
local child = node_factory.create_node(self, node, abs, stat, name)
373+
>>>>>>> c02c98b (extract node factory, remove unused code)
381374
if child then
382375
table.insert(node.nodes, child)
383376
nodes_by_path[child.absolute_path] = true

lua/nvim-tree/node/factory.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local DirectoryNode = require("nvim-tree.node.directory")
2+
local LinkNode = require("nvim-tree.node.link")
3+
local FileNode = require("nvim-tree.node.file")
4+
local Watcher = require("nvim-tree.watcher")
5+
6+
local M = {}
7+
8+
---@param explorer Explorer
9+
-----@param parent DirectoryNode -- TODO #2871 #2886
10+
---@param abs string
11+
---@param stat uv.fs_stat.result|nil
12+
---@param name string
13+
---@return Node|nil
14+
function M.create_node(explorer, parent, abs, stat, name)
15+
if not stat then
16+
return nil
17+
end
18+
19+
if stat.type == "directory" and vim.loop.fs_access(abs, "R") and Watcher.is_fs_event_capable(abs) then
20+
return DirectoryNode:new(explorer, parent, abs, name, stat)
21+
elseif stat.type == "file" then
22+
return FileNode:new(explorer, parent, abs, name, stat)
23+
elseif stat.type == "link" then
24+
local link = LinkNode:new(explorer, parent, abs, name, stat)
25+
if link.link_to ~= nil then
26+
return link
27+
end
28+
end
29+
30+
return nil
31+
end
32+
33+
return M

0 commit comments

Comments
 (0)