@@ -1,5 +1,5 @@
|
|||||||
local api = vim.api
|
local api = vim.api
|
||||||
local luv = vim.loop
|
local uv = vim.loop
|
||||||
|
|
||||||
local utils = require'nvim-tree.utils'
|
local utils = require'nvim-tree.utils'
|
||||||
local eutils = require'nvim-tree.explorer.utils'
|
local eutils = require'nvim-tree.explorer.utils'
|
||||||
@@ -8,70 +8,43 @@ local builders = require'nvim-tree.explorer.node-builders'
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.explore(node, cwd, status)
|
function M.explore(node, cwd, status)
|
||||||
local handle = luv.fs_scandir(cwd)
|
local handle = uv.fs_scandir(cwd)
|
||||||
if type(handle) == 'string' then
|
if type(handle) == 'string' then
|
||||||
api.nvim_err_writeln(handle)
|
api.nvim_err_writeln(handle)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local dirs = {}
|
local node_ignored = node.git_status == '!!'
|
||||||
local links = {}
|
|
||||||
local files = {}
|
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local name, t = luv.fs_scandir_next(handle)
|
local name, t = uv.fs_scandir_next(handle)
|
||||||
if not name then break end
|
if not name then break end
|
||||||
|
|
||||||
local abs = utils.path_join({cwd, name})
|
local abs = utils.path_join({cwd, name})
|
||||||
|
t = t or (uv.fs_stat(abs) or {}).type
|
||||||
if not eutils.should_ignore(abs) and not eutils.should_ignore_git(abs, status.files) then
|
if not eutils.should_ignore(abs) and not eutils.should_ignore_git(abs, status.files) then
|
||||||
if not t then
|
if t == 'directory' and uv.fs_access(abs, 'R') then
|
||||||
local stat = luv.fs_stat(abs)
|
table.insert(node.nodes, builders.folder(abs, name, status, node_ignored))
|
||||||
t = stat and stat.type
|
|
||||||
end
|
|
||||||
|
|
||||||
if t == 'directory' then
|
|
||||||
table.insert(dirs, name)
|
|
||||||
elseif t == 'file' then
|
elseif t == 'file' then
|
||||||
table.insert(files, name)
|
table.insert(node.nodes, builders.file(abs, name, status, node_ignored))
|
||||||
elseif t == 'link' then
|
elseif t == 'link' then
|
||||||
table.insert(links, name)
|
local link = builders.link(abs, name, status, node_ignored)
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local node_ignored = node.git_status == '!!'
|
|
||||||
-- Group empty dirs
|
|
||||||
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, 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
|
|
||||||
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, node_ignored)
|
|
||||||
if luv.fs_access(dir.absolute_path, 'R') then
|
|
||||||
table.insert(node.nodes, dir)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, linkname in ipairs(links) do
|
|
||||||
local link = builders.link(cwd, linkname, status, node_ignored)
|
|
||||||
if link.link_to ~= nil then
|
if link.link_to ~= nil then
|
||||||
table.insert(node.nodes, link)
|
table.insert(node.nodes, link)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
for _, filename in ipairs(files) do
|
if vim.g.nvim_tree_group_empty == 1 then
|
||||||
local file = builders.file(cwd, filename, status, node_ignored)
|
local child_node = node.nodes[1]
|
||||||
table.insert(node.nodes, file)
|
if #(node.nodes) == 1 and child_node.nodes and uv.fs_access(child_node.absolute_path, 'R') then
|
||||||
|
node.group_next = child_node
|
||||||
|
child_node.git_status = node.git_status
|
||||||
|
node.nodes = {}
|
||||||
|
M.explore(child_node, child_node.absolute_path, status)
|
||||||
|
return
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.merge_sort(node.nodes, eutils.node_comparator)
|
utils.merge_sort(node.nodes, eutils.node_comparator)
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ local function get_dir_git_status(parent_ignored, status, absolute_path)
|
|||||||
return dir_status or file_status
|
return dir_status or file_status
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.folder(cwd, name, status, parent_ignored)
|
function M.folder(absolute_path, name, status, parent_ignored)
|
||||||
local absolute_path = utils.path_join({cwd, name})
|
|
||||||
local handle = uv.fs_scandir(absolute_path)
|
local handle = uv.fs_scandir(absolute_path)
|
||||||
local has_children = handle and uv.fs_scandir_next(handle) ~= nil
|
local has_children = handle and uv.fs_scandir_next(handle) ~= nil
|
||||||
|
|
||||||
@@ -37,8 +36,7 @@ local function is_executable(absolute_path, ext)
|
|||||||
return uv.fs_access(absolute_path, 'X')
|
return uv.fs_access(absolute_path, 'X')
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.file(cwd, name, status, parent_ignored)
|
function M.file(absolute_path, name, status, parent_ignored)
|
||||||
local absolute_path = utils.path_join({cwd, name})
|
|
||||||
local ext = string.match(name, ".?[^.]+%.(.*)") or ""
|
local ext = string.match(name, ".?[^.]+%.(.*)") or ""
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -55,9 +53,8 @@ end
|
|||||||
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
|
-- links (for instance libr2.so in /usr/lib) and thus even with a C program realpath fails
|
||||||
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
|
-- when it has no real reason to. Maybe there is a reason, but errno is definitely wrong.
|
||||||
-- So we need to check for link_to ~= nil when adding new links to the main tree
|
-- So we need to check for link_to ~= nil when adding new links to the main tree
|
||||||
function M.link(cwd, name, status, parent_ignored)
|
function M.link(absolute_path, name, status, parent_ignored)
|
||||||
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
|
--- I dont know if this is needed, because in my understanding, there isnt hard links in windows, but just to be sure i changed it.
|
||||||
local absolute_path = utils.path_join({ cwd, name })
|
|
||||||
local link_to = uv.fs_realpath(absolute_path)
|
local link_to = uv.fs_realpath(absolute_path)
|
||||||
local stat = uv.fs_stat(absolute_path)
|
local stat = uv.fs_stat(absolute_path)
|
||||||
local open, nodes
|
local open, nodes
|
||||||
|
|||||||
@@ -100,11 +100,13 @@ function M.reload(nodes, cwd, parent_node, status)
|
|||||||
local prev = nil
|
local prev = nil
|
||||||
local change_prev
|
local change_prev
|
||||||
local new_nodes_added = false
|
local new_nodes_added = false
|
||||||
|
local parent_ignored = parent_node.git_status == '!!'
|
||||||
for _, e in ipairs(all) do
|
for _, e in ipairs(all) do
|
||||||
for _, name in ipairs(e.nodes) do
|
for _, name in ipairs(e.nodes) do
|
||||||
change_prev = true
|
change_prev = true
|
||||||
if not named_nodes[name] then
|
if not named_nodes[name] then
|
||||||
local n = e.fn(cwd, name, status)
|
local abs = utils.path_join({cwd, name})
|
||||||
|
local n = e.fn(abs, name, status, parent_ignored)
|
||||||
if e.check(n.link_to, n.absolute_path) then
|
if e.check(n.link_to, n.absolute_path) then
|
||||||
new_nodes_added = true
|
new_nodes_added = true
|
||||||
idx = 1
|
idx = 1
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
local uv = vim.loop
|
|
||||||
local utils = require'nvim-tree.utils'
|
local utils = require'nvim-tree.utils'
|
||||||
|
|
||||||
local M = {
|
local M = {
|
||||||
@@ -6,25 +5,6 @@ local M = {
|
|||||||
exclude_list = {},
|
exclude_list = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Returns true if there is either exactly 1 dir, or exactly 1 symlink dir. Otherwise, false.
|
|
||||||
-- @param cwd Absolute path to the parent directory
|
|
||||||
-- @param dirs List of dir names
|
|
||||||
-- @param files List of file names
|
|
||||||
-- @param links List of symlink names
|
|
||||||
function M.should_group(cwd, dirs, files, links)
|
|
||||||
if #dirs == 1 and #files == 0 and #links == 0 then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
if #dirs == 0 and #files == 0 and #links == 1 then
|
|
||||||
local absolute_path = utils.path_join({ cwd, links[1] })
|
|
||||||
local link_to = uv.fs_realpath(absolute_path)
|
|
||||||
return (link_to ~= nil) and uv.fs_stat(link_to).type == 'directory'
|
|
||||||
end
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.node_comparator(a, b)
|
function M.node_comparator(a, b)
|
||||||
if not (a and b) then
|
if not (a and b) then
|
||||||
return true
|
return true
|
||||||
|
|||||||
Reference in New Issue
Block a user