Skip to content
Closed
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: 5 additions & 0 deletions lua/codediff/ui/explorer/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local tree_module = require("codediff.ui.explorer.tree")
local keymaps_module = require("codediff.ui.explorer.keymaps")
local refresh_module = require("codediff.ui.explorer.refresh")
local welcome = require("codediff.ui.welcome")
local view_helpers = require("codediff.ui.view.helpers")

local function should_show_welcome(explorer)
if not explorer or not explorer.git_root or explorer.dir1 or explorer.dir2 then
Expand Down Expand Up @@ -105,6 +106,9 @@ end
--- @param group string "staged" | "unstaged" | "conflicts"
--- @return boolean
local function already_showing(session, explorer, file_path, abs_path, group)
if not view_helpers.has_visible_panes(session) then
return false
end
local same_file = (session.modified and session.modified.absolute == abs_path) or (session.original and session.original.absolute == abs_path)
if not same_file then
return false
Expand Down Expand Up @@ -311,6 +315,7 @@ function M.create(status_result, git_root, tabpage, width, base_revision, target

local session = lifecycle.get_session(tabpage)
local showing_already = session
and view_helpers.has_visible_panes(session)
and session.original
and session.modified
and session.original.absolute == original.absolute
Expand Down
8 changes: 7 additions & 1 deletion lua/codediff/ui/history/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,13 @@ function M.create(commits, git_root, tabpage, width, opts)
-- Check if already displaying same file
local target_hash = base_revision or (commit_hash .. "^")
local session = lifecycle.get_session(tabpage)
if not opts.force and session and session.original_revision == target_hash and session.modified_revision == commit_hash then
if
not opts.force
and session
and require("codediff.ui.view.helpers").has_visible_panes(session)
and session.original_revision == target_hash
and session.modified_revision == commit_hash
then
if (session.modified and session.modified.relative == file_path) or (session.original and session.original.relative == file_path) then
return
end
Expand Down
62 changes: 49 additions & 13 deletions lua/codediff/ui/lifecycle/cleanup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ local function cleanup_diff(tabpage)
active_diffs[tabpage] = nil
end

-- Count windows in current tabpage that have diff markers
local function count_diff_windows()
-- Count windows in a tabpage that have diff markers
local function count_diff_windows(tabpage)
if not tabpage or not vim.api.nvim_tabpage_is_valid(tabpage) then
return 0
end

local count = 0
for i = 1, vim.fn.winnr("$") do
local win = vim.fn.win_getid(i)
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if vim.w[win].codediff_restore then
count = count + 1
end
Expand All @@ -124,6 +127,44 @@ local function should_cleanup(winid)
return vim.w[winid].codediff_restore and vim.api.nvim_win_is_valid(winid)
end

local function has_usable_panel(tabpage, diff)
if not diff.panel or (diff.panel.name ~= "explorer" and diff.panel.name ~= "history") then
return false
end

local panel = diff.panel.view
if
not panel
or panel.is_hidden
or type(panel.on_file_select) ~= "function"
or not panel.winid
or not vim.api.nvim_win_is_valid(panel.winid)
or not panel.bufnr
or not vim.api.nvim_buf_is_valid(panel.bufnr)
then
return false
end

return vim.api.nvim_win_get_tabpage(panel.winid) == tabpage and vim.api.nvim_win_get_buf(panel.winid) == panel.bufnr
end

local function cleanup_threshold(tabpage, diff)
local has_result = diff.result_win and vim.api.nvim_win_is_valid(diff.result_win)

-- A visible panel is still an entry point into a side-by-side session. Keep
-- its state even with no diff panes so the next selection can rebuild them.
if diff.layout ~= "inline" and not has_result and has_usable_panel(tabpage, diff) then
return -1
end

local is_single_window = diff.single_pane == true or diff.layout == "inline"
if is_single_window then
return 0
end

return 1
end

-- Setup autocmds for automatic cleanup
function M.setup_autocmds()
-- When a window is closed, check if we should cleanup the diff
Expand All @@ -141,11 +182,8 @@ function M.setup_autocmds()
local active_diffs = session.get_active_diffs()
for tabpage, diff in pairs(active_diffs) do
if diff.original_win == closed_win or diff.modified_win == closed_win then
-- single_pane/inline mode: we expect only 1 diff window
local is_single_window = diff.single_pane == true or diff.layout == "inline"
local diff_win_count = count_diff_windows()
local threshold = is_single_window and 0 or 1
if diff_win_count <= threshold then
local diff_win_count = count_diff_windows(tabpage)
if diff_win_count <= cleanup_threshold(tabpage, diff) then
cleanup_diff(tabpage)
end
break
Expand Down Expand Up @@ -194,10 +232,8 @@ function M.setup_autocmds()
end
local diff = session.get_active_diffs()[tabpage]
if diff then
local diff_win_count = count_diff_windows()
local is_single_window = diff.single_pane == true or diff.layout == "inline"
local threshold = is_single_window and 0 or 1
if diff_win_count <= threshold then
local diff_win_count = count_diff_windows(tabpage)
if diff_win_count <= cleanup_threshold(tabpage, diff) then
cleanup_diff(tabpage)
end
end
Expand Down
50 changes: 49 additions & 1 deletion lua/codediff/ui/view/helpers.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
-- Buffer preparation helpers for diff view
-- Buffer and window helpers for diff views
local M = {}

local virtual_file = require("codediff.core.virtual_file")
local path = require("codediff.core.path")

--- True when every pane needed by the current view is still open.
---@param session table
---@return boolean
function M.has_visible_panes(session)
local original = session.original_win and vim.api.nvim_win_is_valid(session.original_win) or false
local modified = session.modified_win and vim.api.nvim_win_is_valid(session.modified_win) or false
if session.single_pane or session.layout == "inline" then
return original or modified
end
return original and modified
end

--- Open the first content pane when only an explorer or history panel remains.
---@param tabpage number
---@param session table
---@return number? win
function M.open_pane_from_panel(tabpage, session)
local panel = session.panel
local view = panel and panel.view
local win = view and view.winid
if not win or view.is_hidden or not vim.api.nvim_win_is_valid(win) or vim.api.nvim_win_get_tabpage(win) ~= tabpage or vim.api.nvim_win_get_buf(win) ~= view.bufnr then
return nil
end

local config = require("codediff.config")
local panel_config = config.options[panel.name] or {}
local position = panel_config.position or (panel.name == "history" and "bottom" or "left")
vim.api.nvim_set_current_win(win)
local scratch = vim.api.nvim_create_buf(false, true)
vim.bo[scratch].buftype = "nofile"
vim.bo[scratch].bufhidden = "wipe"

local ok, content_win = pcall(vim.api.nvim_open_win, scratch, true, {
split = position == "bottom" and "above" or "right",
win = -1,
})
if not ok then
pcall(vim.api.nvim_buf_delete, scratch, { force = true })
return nil
end

vim.w[content_win].codediff_restore = 1
vim.wo[content_win].cursorline = true
vim.wo[content_win].wrap = false
vim.wo[content_win].list = false
return content_win
end

--- True when the panel opens before any file is chosen, so the panes start
--- empty and the panel fills them on first selection.
--- @param session_config SessionConfig
Expand Down
4 changes: 4 additions & 0 deletions lua/codediff/ui/view/side_by_side/single_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ local function show_single_file(tabpage, opts)
close_win = nil
end

if not keep_win or not vim.api.nvim_win_is_valid(keep_win) then
keep_win = helpers.open_pane_from_panel(tabpage, session)
end

-- Load the file into the kept window BEFORE closing the other one. Virtual
-- buffers (from load_virtual_file) carry `bufhidden = "wipe"` so they get
-- wiped as soon as they have no window; closing close_win first would leave
Expand Down
25 changes: 17 additions & 8 deletions lua/codediff/ui/view/side_by_side/update.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,17 @@ function M.update(tabpage, session_config, auto_scroll_to_first_hunk)
if not old_original_buf or not old_modified_buf then
return false
end
if not original_win and not modified_win then
return false
local original_win_valid = original_win and vim.api.nvim_win_is_valid(original_win)
local modified_win_valid = modified_win and vim.api.nvim_win_is_valid(modified_win)

-- Rebuild the first pane from the panel when both diff windows are gone.
if not original_win_valid and not modified_win_valid then
original_win = helpers.open_pane_from_panel(tabpage, session)
if not original_win then
return false
end
original_win_valid = true
session.original_win = original_win
end

-- Disable auto-refresh temporarily
Expand All @@ -155,19 +164,19 @@ function M.update(tabpage, session_config, auto_scroll_to_first_hunk)
lifecycle.set_result(tabpage, nil, nil)
end

-- Restore second window if returning from single-pane mode
if session.single_pane then
-- Restore the second pane after a single-file view or a manual close.
if session.single_pane or not original_win_valid or not modified_win_valid then
local split_cmd = config.options.diff.original_position == "right" and "leftabove vsplit" or "rightbelow vsplit"

if not original_win or not vim.api.nvim_win_is_valid(original_win) then
-- Original was closed (untracked file) — recreate it to the left of modified
if not original_win_valid then
-- Recreate original on the configured side of modified.
vim.api.nvim_set_current_win(modified_win)
vim.cmd(config.options.diff.original_position == "right" and "rightbelow vsplit" or "leftabove vsplit")
original_win = vim.api.nvim_get_current_win()
vim.w[original_win].codediff_restore = 1
session.original_win = original_win
elseif not modified_win or not vim.api.nvim_win_is_valid(modified_win) then
-- Modified was closed (deleted file) — recreate it to the right of original
elseif not modified_win_valid then
-- Recreate modified on the configured side of original.
vim.api.nvim_set_current_win(original_win)
vim.cmd(split_cmd)
modified_win = vim.api.nvim_get_current_win()
Expand Down
Loading
Loading