refacto: move code ton explorer and simplify some internal apis

This commit is contained in:
kiyan 2022-02-06 17:58:24 +01:00
parent 8b27fd4e61
commit 8a6c7bae3a
5 changed files with 75 additions and 51 deletions

View File

@ -87,8 +87,9 @@ function M.on_enter(opts)
local stats = luv.fs_stat(bufname) local stats = luv.fs_stat(bufname)
local is_dir = stats and stats.type == 'directory' local is_dir = stats and stats.type == 'directory'
local cwd
if is_dir then if is_dir then
lib.Tree.cwd = vim.fn.expand(bufname) cwd = vim.fn.expand(bufname)
end end
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
@ -104,7 +105,7 @@ function M.on_enter(opts)
M.hijack_current_window() M.hijack_current_window()
end end
lib.init(should_open, lib.Tree.cwd) lib.init(should_open, cwd)
end end
local function is_file_readable(fname) local function is_file_readable(fname)

View File

@ -13,6 +13,9 @@ function M.fn(fname)
local i local i
local hide_root_folder = view.View.hide_root_folder local hide_root_folder = view.View.hide_root_folder
local Explorer = get_explorer() local Explorer = get_explorer()
if not Explorer then
return
end
if Explorer.cwd == '/' or hide_root_folder then if Explorer.cwd == '/' or hide_root_folder then
i = 0 i = 0
else else
@ -32,7 +35,7 @@ function M.fn(fname)
if path_matches then if path_matches then
if #node.nodes == 0 then if #node.nodes == 0 then
node.open = true 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) git.load_project_status(node.absolute_path, function(status)
if status.dirs or status.files then if status.dirs or status.files then
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects) require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)

View File

@ -7,7 +7,7 @@ local builders = require'nvim-tree.explorer.node-builders'
local M = {} local M = {}
function M.explore(nodes, cwd, parent_node, status) function M.explore(node, cwd, status)
local handle = luv.fs_scandir(cwd) local handle = luv.fs_scandir(cwd)
if type(handle) == 'string' then if type(handle) == 'string' then
api.nvim_err_writeln(handle) api.nvim_err_writeln(handle)
@ -39,42 +39,42 @@ function M.explore(nodes, cwd, parent_node, status)
end end
end end
local parent_node_ignored = parent_node and parent_node.git_status == '!!' local node_ignored = node.git_status == '!!'
-- Group empty dirs -- 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 if eutils.should_group(cwd, dirs, files, links) then
local child_node local child_node
if dirs[1] then child_node = builders.folder(cwd, dirs[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, parent_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 if luv.fs_access(child_node.absolute_path, 'R') then
parent_node.group_next = child_node node.group_next = child_node
child_node.git_status = parent_node.git_status child_node.git_status = node.git_status
M.explore(nodes, child_node.absolute_path, child_node, status) M.explore(child_node, child_node.absolute_path, status)
return return
end end
end end
end end
for _, dirname in ipairs(dirs) do 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 if luv.fs_access(dir.absolute_path, 'R') then
table.insert(nodes, dir) table.insert(node.nodes, dir)
end end
end end
for _, linkname in ipairs(links) do 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 if link.link_to ~= nil then
table.insert(nodes, link) table.insert(node.nodes, link)
end end
end end
for _, filename in ipairs(files) do for _, filename in ipairs(files) do
local file = builders.file(cwd, filename, status, parent_node_ignored) local file = builders.file(cwd, filename, status, node_ignored)
table.insert(nodes, file) table.insert(node.nodes, file)
end end
utils.merge_sort(nodes, eutils.node_comparator) utils.merge_sort(node.nodes, eutils.node_comparator)
end end
return M return M

View File

@ -1,10 +1,47 @@
local uv = vim.loop
local git = require"nvim-tree.git"
local M = {} local M = {}
M.explore = require"nvim-tree.explorer.explore".explore M.explore = require"nvim-tree.explorer.explore".explore
M.reload = require"nvim-tree.explorer.reload".reload 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) function M.setup(opts)
require"nvim-tree.explorer.utils".setup(opts) require"nvim-tree.explorer.utils".setup(opts)
end end
M.Explorer = Explorer
return M return M

View File

@ -1,12 +1,10 @@
local api = vim.api local api = vim.api
local luv = vim.loop
local renderer = require'nvim-tree.renderer' local renderer = require'nvim-tree.renderer'
local diagnostics = require'nvim-tree.diagnostics' local diagnostics = require'nvim-tree.diagnostics'
local explorer = require'nvim-tree.explorer' local explorer = require'nvim-tree.explorer'
local view = require'nvim-tree.view' local view = require'nvim-tree.view'
local events = require'nvim-tree.events' local events = require'nvim-tree.events'
local git = require'nvim-tree.git'
local first_init_done = false local first_init_done = false
@ -14,32 +12,19 @@ local M = {
target_winid = nil, 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) function M.init(with_open, foldername)
M.Tree.nodes = {} M.Tree = explorer.Explorer.new(foldername)
M.Tree.cwd = foldername or luv.cwd() M.Tree:init(function()
M.redraw()
if with_open then
M.open()
end
if with_open then if not first_init_done then
M.open() events._dispatch_ready()
end first_init_done = true
end
load_children(M.Tree.cwd, M.Tree.nodes) end)
if not first_init_done then
events._dispatch_ready()
first_init_done = true
end
end end
function M.redraw() function M.redraw()
@ -100,11 +85,7 @@ function M.expand_or_collapse(node)
node.open = not node.open node.open = not node.open
if node.has_children then node.has_children = false end if node.has_children then node.has_children = false end
if #node.nodes == 0 then if #node.nodes == 0 then
load_children( M.Tree:expand(node)
node.link_to or node.absolute_path,
node.nodes,
node
)
else else
M.redraw() M.redraw()
end end
@ -143,12 +124,14 @@ function M.close_node(node)
end end
function M.toggle_ignored() 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() return require'nvim-tree.actions.reloaders'.reload_explorer()
end end
function M.toggle_dotfiles() 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() return require'nvim-tree.actions.reloaders'.reload_explorer()
end end