From 98b4a9c977c7f0737b2955ab6ff1ded0c010769d Mon Sep 17 00:00:00 2001 From: kiyan Date: Sun, 13 Feb 2022 12:03:08 +0100 Subject: [PATCH] fix: reload git status of existing nodes --- lua/nvim-tree/actions/reloaders.lua | 5 +++-- lua/nvim-tree/explorer/node-builders.lua | 12 ++++++++---- lua/nvim-tree/explorer/reload.lua | 23 +++++++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree/actions/reloaders.lua b/lua/nvim-tree/actions/reloaders.lua index 9c29d67569c..a92585430df 100644 --- a/lua/nvim-tree/actions/reloaders.lua +++ b/lua/nvim-tree/actions/reloaders.lua @@ -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) diff --git a/lua/nvim-tree/explorer/node-builders.lua b/lua/nvim-tree/explorer/node-builders.lua index a89f9877a5d..ea731bbf5f0 100644 --- a/lua/nvim-tree/explorer/node-builders.lua +++ b/lua/nvim-tree/explorer/node-builders.lua @@ -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 @@ -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, @@ -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 @@ -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, diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index a765fb2970c..8ee9ecdf1ac 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -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 @@ -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