Skip to content

fix: reload git status of existing nodes #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lua/nvim-tree/actions/reloaders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ local explorer_module = require'nvim-tree.explorer'
local M = {}

local function refresh_nodes(node, projects)
local project_root = git.get_project_root(node.absolute_path or node.cwd)
explorer_module.reload(node, node.absolute_path or node.cwd, projects[project_root] or {})
local cwd = node.absolute_path or node.cwd
local project_root = git.get_project_root(cwd)
explorer_module.reload(node, cwd, projects[project_root] or {})
for _, _node in ipairs(node.nodes) do
if _node.nodes and _node.open then
refresh_nodes(_node, projects)
Expand Down
12 changes: 8 additions & 4 deletions lua/nvim-tree/explorer/node-builders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local M = {
is_windows = vim.fn.has('win32') == 1
}

local function get_dir_git_status(parent_ignored, status, absolute_path)
function M.get_dir_git_status(parent_ignored, status, absolute_path)
if parent_ignored then
return '!!'
end
Expand All @@ -14,13 +14,17 @@ local function get_dir_git_status(parent_ignored, status, absolute_path)
return dir_status or file_status
end

function M.get_git_status(parent_ignored, status, absolute_path)
return parent_ignored and '!!' or status.files and status.files[absolute_path]
end

function M.folder(absolute_path, name, status, parent_ignored)
local handle = uv.fs_scandir(absolute_path)
local has_children = handle and uv.fs_scandir_next(handle) ~= nil

return {
absolute_path = absolute_path,
git_status = 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
has_children = has_children,
name = name,
Expand All @@ -43,7 +47,7 @@ function M.file(absolute_path, name, status, parent_ignored)
absolute_path = absolute_path,
executable = is_executable(absolute_path, ext),
extension = ext,
git_status = parent_ignored and '!!' or status.files and status.files[absolute_path],
git_status = M.get_git_status(parent_ignored, status, absolute_path),
name = name,
}
end
Expand All @@ -70,7 +74,7 @@ function M.link(absolute_path, name, status, parent_ignored)

return {
absolute_path = absolute_path,
git_status = parent_ignored and '!!' or status.files and status.files[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
last_modified = last_modified,
link_to = link_to,
Expand Down
23 changes: 19 additions & 4 deletions lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ local function key_by(nodes, key)
return v
end

local function update_status(nodes_by_path, node_ignored, status)
return function(node)
if nodes_by_path[node.absolute_path] then
if node.nodes then
node.git_status = builders.get_dir_git_status(node_ignored, status, node.absolute_path)
else
node.git_status = builders.get_git_status(node_ignored, status, node.absolute_path)
end
end
return node
end
end

function M.reload(node, cwd, status)
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
Expand Down Expand Up @@ -54,10 +67,12 @@ function M.reload(node, cwd, status)
end
end

node.nodes = vim.tbl_filter(
function(n) return child_names[n.absolute_path] end,
node.nodes
)
node.nodes = vim.tbl_map(
update_status(nodes_by_path, node_ignored, status),
vim.tbl_filter(
function(n) return child_names[n.absolute_path] end,
node.nodes
))

local is_root = node.cwd ~= nil
if vim.g.nvim_tree_group_empty == 1 and not is_root and #(node.nodes) == 1 then
Expand Down