Skip to content
Merged
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
70 changes: 70 additions & 0 deletions lua/codediff/ui/explorer/refresh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,70 @@ function M.setup_auto_refresh(explorer, tabpage)
return cleanup
end

-- Collect collapsed state from tree (groups and directories that user manually collapsed)
local function collect_collapsed_state(tree)
local collapsed = {}

local function collect_from_node(node)
if not node.data then
return
end
local node_type = node.data.type
if node_type == "group" or node_type == "directory" then
-- Use path for directories, name for groups as unique key
local key = node.data.path or node.data.name
if key and not node:is_expanded() then
collapsed[key] = true
end
-- Recurse into children
if node:has_children() then
for _, child_id in ipairs(node:get_child_ids()) do
local child = tree:get_node(child_id)
if child then
collect_from_node(child)
end
end
end
end
end

local root_nodes = tree:get_nodes()
for _, node in ipairs(root_nodes) do
collect_from_node(node)
end

return collapsed
end

-- Restore collapsed state after tree rebuild
local function restore_collapsed_state(tree, collapsed, root_nodes)
local function restore_node(node)
if not node.data then
return
end
local node_type = node.data.type
if node_type == "group" or node_type == "directory" then
local key = node.data.path or node.data.name
if key and collapsed[key] then
node:collapse()
end
-- Recurse into children
if node:has_children() then
for _, child_id in ipairs(node:get_child_ids()) do
local child = tree:get_node(child_id)
if child then
restore_node(child)
end
end
end
end
end

for _, node in ipairs(root_nodes) do
restore_node(node)
end
end

-- Refresh explorer with updated git status
function M.refresh(explorer)
local git = require("codediff.core.git")
Expand All @@ -144,6 +208,9 @@ function M.refresh(explorer)
local current_node = explorer.tree:get_node()
local current_path = current_node and current_node.data and current_node.data.path

-- Collect collapsed state before async operation
local collapsed_state = collect_collapsed_state(explorer.tree)

local function process_result(err, status_result)
vim.schedule(function()
if err then
Expand Down Expand Up @@ -182,6 +249,9 @@ function M.refresh(explorer)
end
end

-- Restore user's collapsed state (must be after expand_all_dirs)
restore_collapsed_state(explorer.tree, collapsed_state, root_nodes)

explorer.tree:render()

-- Update status result for file selection logic
Expand Down