Skip to content

fix: restore proper window layout after diff operations #61

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions lua/claudecode/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ local logger = require("claudecode.logger")
local active_diffs = {}
local autocmd_group

--- Restore window layout by delegating to terminal module
-- @param target_window number|nil The remaining main editor window
local function restore_window_layout()
local ok, terminal = pcall(require, "claudecode.terminal")
if ok and terminal.restore_window_layout then
terminal.restore_window_layout()
end
end

--- Get or create the autocmd group
local function get_autocmd_group()
if not autocmd_group then
Expand Down Expand Up @@ -316,13 +325,15 @@ function M._resolve_diff_as_saved(tab_name, buffer_id)
final_content = final_content .. "\n"
end

-- Close diff windows (unified behavior)
-- Close diff windows (unified behavior) and restore layout
if diff_data.new_window and vim.api.nvim_win_is_valid(diff_data.new_window) then
vim.api.nvim_win_close(diff_data.new_window, true)
end
if diff_data.target_window and vim.api.nvim_win_is_valid(diff_data.target_window) then
vim.api.nvim_set_current_win(diff_data.target_window)
vim.cmd("diffoff")
-- Restore proper window layout after closing diff
restore_window_layout()
end

-- Create MCP-compliant response
Expand Down Expand Up @@ -615,11 +626,13 @@ function M._cleanup_diff_state(tab_name, reason)
pcall(vim.api.nvim_win_close, diff_data.new_window, true)
end

-- Turn off diff mode in target window if it still exists
-- Turn off diff mode in target window if it still exists and restore layout
if diff_data.target_window and vim.api.nvim_win_is_valid(diff_data.target_window) then
vim.api.nvim_win_call(diff_data.target_window, function()
vim.cmd("diffoff")
end)
-- Restore proper window layout after cleanup
restore_window_layout()
end

-- Remove from active diffs
Expand Down Expand Up @@ -913,13 +926,15 @@ function M.deny_current_diff()
return
end

-- Close windows and clean up (same logic as the original keymap)
-- Close windows and clean up with layout restoration
if new_win and vim.api.nvim_win_is_valid(new_win) then
vim.api.nvim_win_close(new_win, true)
end
if target_window and vim.api.nvim_win_is_valid(target_window) then
vim.api.nvim_set_current_win(target_window)
vim.cmd("diffoff")
-- Restore proper window layout after rejecting diff
restore_window_layout()
end

M._resolve_diff_as_rejected(tab_name)
Expand Down
25 changes: 25 additions & 0 deletions lua/claudecode/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,29 @@ function M._get_managed_terminal_for_test()
return nil
end

--- Restore window layout after closing diff windows
-- This ensures that Claude's terminal maintains its configured size ratio
-- @param target_window number|nil The remaining main editor window
function M.restore_window_layout()
local logger = require("claudecode.logger")

-- Get active terminal buffer number
local terminal_bufnr = M.get_active_terminal_bufnr()
if not terminal_bufnr then
return
end

-- Check if terminal is visible and get its window
if is_terminal_visible(terminal_bufnr) then
local bufinfo = vim.fn.getbufinfo(terminal_bufnr)
if bufinfo and #bufinfo > 0 and #bufinfo[1].windows > 0 then
local terminal_win = bufinfo[1].windows[1]
local total_width = vim.o.columns
local terminal_width = math.floor(total_width * config.split_width_percentage)
vim.api.nvim_win_set_width(terminal_win, terminal_width)
logger.debug("terminal", "Restored window layout with terminal width:", terminal_width)
end
end
end

return M