refacto: move code ton explorer and simplify some internal apis
This commit is contained in:
parent
8b27fd4e61
commit
8a6c7bae3a
@ -87,8 +87,9 @@ function M.on_enter(opts)
|
||||
|
||||
local stats = luv.fs_stat(bufname)
|
||||
local is_dir = stats and stats.type == 'directory'
|
||||
local cwd
|
||||
if is_dir then
|
||||
lib.Tree.cwd = vim.fn.expand(bufname)
|
||||
cwd = vim.fn.expand(bufname)
|
||||
end
|
||||
|
||||
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
|
||||
@ -104,7 +105,7 @@ function M.on_enter(opts)
|
||||
M.hijack_current_window()
|
||||
end
|
||||
|
||||
lib.init(should_open, lib.Tree.cwd)
|
||||
lib.init(should_open, cwd)
|
||||
end
|
||||
|
||||
local function is_file_readable(fname)
|
||||
|
||||
@ -13,6 +13,9 @@ function M.fn(fname)
|
||||
local i
|
||||
local hide_root_folder = view.View.hide_root_folder
|
||||
local Explorer = get_explorer()
|
||||
if not Explorer then
|
||||
return
|
||||
end
|
||||
if Explorer.cwd == '/' or hide_root_folder then
|
||||
i = 0
|
||||
else
|
||||
@ -32,7 +35,7 @@ function M.fn(fname)
|
||||
if path_matches then
|
||||
if #node.nodes == 0 then
|
||||
node.open = true
|
||||
explorer_module.explore(node.nodes, node.absolute_path, node, {})
|
||||
explorer_module.explore(node, node.absolute_path, {})
|
||||
git.load_project_status(node.absolute_path, function(status)
|
||||
if status.dirs or status.files then
|
||||
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
|
||||
|
||||
@ -7,7 +7,7 @@ local builders = require'nvim-tree.explorer.node-builders'
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.explore(nodes, cwd, parent_node, status)
|
||||
function M.explore(node, cwd, status)
|
||||
local handle = luv.fs_scandir(cwd)
|
||||
if type(handle) == 'string' then
|
||||
api.nvim_err_writeln(handle)
|
||||
@ -39,42 +39,42 @@ function M.explore(nodes, cwd, parent_node, status)
|
||||
end
|
||||
end
|
||||
|
||||
local parent_node_ignored = parent_node and parent_node.git_status == '!!'
|
||||
local node_ignored = node.git_status == '!!'
|
||||
-- Group empty dirs
|
||||
if parent_node and vim.g.nvim_tree_group_empty == 1 then
|
||||
if vim.g.nvim_tree_group_empty == 1 then
|
||||
if eutils.should_group(cwd, dirs, files, links) then
|
||||
local child_node
|
||||
if dirs[1] then child_node = builders.folder(cwd, dirs[1], status, parent_node_ignored) end
|
||||
if links[1] then child_node = builders.link(cwd, links[1], status, parent_node_ignored) end
|
||||
if dirs[1] then child_node = builders.folder(cwd, dirs[1], status, node_ignored) end
|
||||
if links[1] then child_node = builders.link(cwd, links[1], status, node_ignored) end
|
||||
if luv.fs_access(child_node.absolute_path, 'R') then
|
||||
parent_node.group_next = child_node
|
||||
child_node.git_status = parent_node.git_status
|
||||
M.explore(nodes, child_node.absolute_path, child_node, status)
|
||||
node.group_next = child_node
|
||||
child_node.git_status = node.git_status
|
||||
M.explore(child_node, child_node.absolute_path, status)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, dirname in ipairs(dirs) do
|
||||
local dir = builders.folder(cwd, dirname, status, parent_node_ignored)
|
||||
local dir = builders.folder(cwd, dirname, status, node_ignored)
|
||||
if luv.fs_access(dir.absolute_path, 'R') then
|
||||
table.insert(nodes, dir)
|
||||
table.insert(node.nodes, dir)
|
||||
end
|
||||
end
|
||||
|
||||
for _, linkname in ipairs(links) do
|
||||
local link = builders.link(cwd, linkname, status, parent_node_ignored)
|
||||
local link = builders.link(cwd, linkname, status, node_ignored)
|
||||
if link.link_to ~= nil then
|
||||
table.insert(nodes, link)
|
||||
table.insert(node.nodes, link)
|
||||
end
|
||||
end
|
||||
|
||||
for _, filename in ipairs(files) do
|
||||
local file = builders.file(cwd, filename, status, parent_node_ignored)
|
||||
table.insert(nodes, file)
|
||||
local file = builders.file(cwd, filename, status, node_ignored)
|
||||
table.insert(node.nodes, file)
|
||||
end
|
||||
|
||||
utils.merge_sort(nodes, eutils.node_comparator)
|
||||
utils.merge_sort(node.nodes, eutils.node_comparator)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@ -1,10 +1,47 @@
|
||||
local uv = vim.loop
|
||||
|
||||
local git = require"nvim-tree.git"
|
||||
|
||||
local M = {}
|
||||
|
||||
M.explore = require"nvim-tree.explorer.explore".explore
|
||||
M.reload = require"nvim-tree.explorer.reload".reload
|
||||
|
||||
local Explorer = {}
|
||||
Explorer.__index = Explorer
|
||||
|
||||
function Explorer.new(cwd)
|
||||
cwd = cwd or uv.cwd()
|
||||
return setmetatable({
|
||||
cwd = cwd,
|
||||
nodes = {}
|
||||
}, Explorer)
|
||||
end
|
||||
|
||||
function Explorer:_load(cwd, node)
|
||||
git.load_project_status(cwd, function(git_statuses)
|
||||
M.explore(node, cwd, git_statuses)
|
||||
if type(self.init_cb) == "function" then
|
||||
self.init_cb(self)
|
||||
self.init_cb = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function Explorer:expand(node)
|
||||
self.init_cb = require"nvim-tree.lib".redraw
|
||||
self:_load(node.link_to or node.absolute_path, node)
|
||||
end
|
||||
|
||||
function Explorer:init(f)
|
||||
self.init_cb = f
|
||||
self:_load(self.cwd, self)
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
require"nvim-tree.explorer.utils".setup(opts)
|
||||
end
|
||||
|
||||
M.Explorer = Explorer
|
||||
|
||||
return M
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
local api = vim.api
|
||||
local luv = vim.loop
|
||||
|
||||
local renderer = require'nvim-tree.renderer'
|
||||
local diagnostics = require'nvim-tree.diagnostics'
|
||||
local explorer = require'nvim-tree.explorer'
|
||||
local view = require'nvim-tree.view'
|
||||
local events = require'nvim-tree.events'
|
||||
local git = require'nvim-tree.git'
|
||||
|
||||
local first_init_done = false
|
||||
|
||||
@ -14,32 +12,19 @@ local M = {
|
||||
target_winid = nil,
|
||||
}
|
||||
|
||||
M.Tree = {
|
||||
nodes = {},
|
||||
cwd = nil,
|
||||
}
|
||||
|
||||
local function load_children(cwd, children, parent)
|
||||
git.load_project_status(cwd, function(git_statuses)
|
||||
explorer.explore(children, cwd, parent, git_statuses)
|
||||
M.redraw()
|
||||
end)
|
||||
end
|
||||
|
||||
function M.init(with_open, foldername)
|
||||
M.Tree.nodes = {}
|
||||
M.Tree.cwd = foldername or luv.cwd()
|
||||
M.Tree = explorer.Explorer.new(foldername)
|
||||
M.Tree:init(function()
|
||||
M.redraw()
|
||||
if with_open then
|
||||
M.open()
|
||||
end
|
||||
|
||||
if with_open then
|
||||
M.open()
|
||||
end
|
||||
|
||||
load_children(M.Tree.cwd, M.Tree.nodes)
|
||||
|
||||
if not first_init_done then
|
||||
events._dispatch_ready()
|
||||
first_init_done = true
|
||||
end
|
||||
if not first_init_done then
|
||||
events._dispatch_ready()
|
||||
first_init_done = true
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function M.redraw()
|
||||
@ -100,11 +85,7 @@ function M.expand_or_collapse(node)
|
||||
node.open = not node.open
|
||||
if node.has_children then node.has_children = false end
|
||||
if #node.nodes == 0 then
|
||||
load_children(
|
||||
node.link_to or node.absolute_path,
|
||||
node.nodes,
|
||||
node
|
||||
)
|
||||
M.Tree:expand(node)
|
||||
else
|
||||
M.redraw()
|
||||
end
|
||||
@ -143,12 +124,14 @@ function M.close_node(node)
|
||||
end
|
||||
|
||||
function M.toggle_ignored()
|
||||
explorer.config.filter_ignored = not explorer.config.filter_ignored
|
||||
local config = require"nvim-tree.explorer.utils".config
|
||||
config.filter_ignored = not config.filter_ignored
|
||||
return require'nvim-tree.actions.reloaders'.reload_explorer()
|
||||
end
|
||||
|
||||
function M.toggle_dotfiles()
|
||||
explorer.config.filter_dotfiles = not explorer.config.filter_dotfiles
|
||||
local config = require"nvim-tree.explorer.utils".config
|
||||
config.filter_dotfiles = not config.filter_dotfiles
|
||||
return require'nvim-tree.actions.reloaders'.reload_explorer()
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user