fix: add parent ref to nodes

also refactor movement go to parent
fixes #1148
This commit is contained in:
kiyan
2022-04-21 20:39:14 +02:00
parent 7f2ed4e043
commit 95a5c2d4bc
4 changed files with 26 additions and 36 deletions

View File

@@ -39,32 +39,19 @@ end
function M.parent_node(should_close) function M.parent_node(should_close)
return function(node) return function(node)
if node.name == ".." then local parent = node.parent
return
if not parent or parent.cwd then
return view.set_cursor { 1, 0 }
end end
should_close = should_close or false local _, line = utils.find_node(core.get_explorer().nodes, function(n)
local altered_tree = false return n.absolute_path == parent.absolute_path
end)
local iter = get_line_from_node(node, true) view.set_cursor { line + 1, 0 }
if node.open == true and should_close then if should_close then
node.open = false
altered_tree = true
else
local line, parent = iter(core.get_explorer().nodes, true)
if parent == nil then
line = 1
elseif should_close then
parent.open = false parent.open = false
altered_tree = true
end
if not view.is_root_folder_visible(core.get_cwd()) then
line = line - 1
end
view.set_cursor { line, 0 }
end
if altered_tree then
diagnostics.update() diagnostics.update()
renderer.draw() renderer.draw()
end end

View File

@@ -30,11 +30,11 @@ local function populate_children(handle, cwd, node, status)
and not nodes_by_path[abs] and not nodes_by_path[abs]
then then
if t == "directory" and uv.fs_access(abs, "R") then if t == "directory" and uv.fs_access(abs, "R") then
table.insert(node.nodes, builders.folder(abs, name, status, node_ignored)) table.insert(node.nodes, builders.folder(node, abs, name, status, node_ignored))
elseif t == "file" then elseif t == "file" then
table.insert(node.nodes, builders.file(abs, name, status, node_ignored)) table.insert(node.nodes, builders.file(node, abs, name, status, node_ignored))
elseif t == "link" then elseif t == "link" then
local link = builders.link(abs, name, status, node_ignored) local link = builders.link(node, abs, name, 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

View File

@@ -18,19 +18,20 @@ function M.get_git_status(parent_ignored, status, absolute_path)
return parent_ignored and "!!" or status.files and status.files[absolute_path] return parent_ignored and "!!" or status.files and status.files[absolute_path]
end end
function M.folder(absolute_path, name, status, parent_ignored) function M.folder(parent, absolute_path, name, status, parent_ignored)
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
return { return {
absolute_path = absolute_path, absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_dir_git_status(parent_ignored, status, absolute_path), git_status = M.get_dir_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children, has_children = has_children,
name = name, name = name,
nodes = {}, nodes = {},
open = false, open = false,
fs_stat = uv.fs_stat(absolute_path), parent = parent,
} }
end end
@@ -41,16 +42,17 @@ 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(absolute_path, name, status, parent_ignored) function M.file(parent, absolute_path, name, status, parent_ignored)
local ext = string.match(name, ".?[^.]+%.(.*)") or "" local ext = string.match(name, ".?[^.]+%.(.*)") or ""
return { return {
absolute_path = absolute_path, absolute_path = absolute_path,
executable = is_executable(absolute_path, ext), executable = is_executable(absolute_path, ext),
extension = ext, extension = ext,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_git_status(parent_ignored, status, absolute_path), git_status = M.get_git_status(parent_ignored, status, absolute_path),
name = name, name = name,
fs_stat = uv.fs_stat(absolute_path), parent = parent,
} }
end end
@@ -59,7 +61,7 @@ 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(absolute_path, name, status, parent_ignored) function M.link(parent, 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 link_to = uv.fs_realpath(absolute_path) local link_to = uv.fs_realpath(absolute_path)
local open, nodes, has_children local open, nodes, has_children
@@ -72,14 +74,15 @@ function M.link(absolute_path, name, status, parent_ignored)
return { return {
absolute_path = absolute_path, absolute_path = absolute_path,
fs_stat = uv.fs_stat(absolute_path),
git_status = M.get_git_status(parent_ignored, status, absolute_path), git_status = M.get_git_status(parent_ignored, status, absolute_path),
group_next = nil, -- If node is grouped, this points to the next child dir/link node group_next = nil, -- If node is grouped, this points to the next child dir/link node
has_children = has_children,
link_to = link_to, link_to = link_to,
name = name, name = name,
nodes = nodes, nodes = nodes,
open = open, open = open,
fs_stat = uv.fs_stat(absolute_path), parent = parent,
has_children = has_children,
} }
end end

View File

@@ -51,11 +51,11 @@ function M.reload(node, status)
child_names[abs] = true child_names[abs] = true
if not nodes_by_path[abs] then if not nodes_by_path[abs] then
if t == "directory" and uv.fs_access(abs, "R") then if t == "directory" and uv.fs_access(abs, "R") then
table.insert(node.nodes, builders.folder(abs, name, status, node_ignored)) table.insert(node.nodes, builders.folder(node, abs, name, status, node_ignored))
elseif t == "file" then elseif t == "file" then
table.insert(node.nodes, builders.file(abs, name, status, node_ignored)) table.insert(node.nodes, builders.file(node, abs, name, status, node_ignored))
elseif t == "link" then elseif t == "link" then
local link = builders.link(abs, name, status, node_ignored) local link = builders.link(node, abs, name, 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