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 1 commit 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
14 changes: 14 additions & 0 deletions lua/claudecode/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ local function is_buffer_dirty(file_path)
return is_dirty, nil
end

---Restore window layout by delegating to terminal module
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

---Setup the diff module
---@param user_config table? The configuration passed from init.lua
function M.setup(user_config)
Expand Down Expand Up @@ -369,6 +377,8 @@ function M._resolve_diff_as_saved(tab_name, buffer_id)
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 @@ -678,6 +688,8 @@ function M._cleanup_diff_state(tab_name, reason)
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 @@ -1016,6 +1028,8 @@ function M.deny_current_diff()
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 @@ -423,4 +423,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
function M.restore_window_layout()
local logger = require("claudecode.logger")
local effective_config = build_config({})

-- 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 * effective_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