From 8c3577614d1fac04af9fa4695156141eb9372a34 Mon Sep 17 00:00:00 2001 From: user <17591696+sand4rt@users.noreply.github.com> Date: Sat, 18 Jul 2026 22:26:20 +0200 Subject: [PATCH] fix: open integration when the window is small --- lua/zen/init.lua | 35 ++++++++++-------- tests/scripts/init_with_zen_small.lua | 18 ++------- tests/test_integrations.lua | 53 +++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 32 deletions(-) diff --git a/lua/zen/init.lua b/lua/zen/init.lua index 03dc5ab..c4734e0 100644 --- a/lua/zen/init.lua +++ b/lua/zen/init.lua @@ -3,7 +3,7 @@ --- @class Integration --- @field filetype Filetype --- @field min_width? number ---- @field replace? boolean +--- @field replace? boolean --- @class Config --- @field main? { width: number | fun(): number; } @@ -281,22 +281,24 @@ local function reposition_stack(position) heights[win] = vim.api.nvim_win_get_height(win) end - if position == "top" then - vim.api.nvim_win_set_config(windows[1], { split = "above", win = -1 }) - for i = 2, #windows do - vim.api.nvim_win_set_config(windows[i], { split = "below", win = windows[i - 1] }) - end - else - vim.api.nvim_win_set_config(windows[#windows], { split = "below", win = -1 }) - for i = #windows - 1, 1, -1 do - vim.api.nvim_win_set_config(windows[i], { split = "above", win = windows[i + 1] }) + pcall(function() + if position == "top" then + vim.api.nvim_win_set_config(windows[1], { split = "above", win = -1 }) + for i = 2, #windows do + vim.api.nvim_win_set_config(windows[i], { split = "below", win = windows[i - 1] }) + end + else + vim.api.nvim_win_set_config(windows[#windows], { split = "below", win = -1 }) + for i = #windows - 1, 1, -1 do + vim.api.nvim_win_set_config(windows[i], { split = "above", win = windows[i + 1] }) + end end - end - for _, win in ipairs(windows) do - vim.api.nvim_win_set_height(win, heights[win]) - vim.api.nvim_win_set_width(win, vim.o.columns) - end + for _, win in ipairs(windows) do + vim.api.nvim_win_set_height(win, heights[win]) + vim.api.nvim_win_set_width(win, vim.o.columns) + end + end) end ---@param position "top" | "bottom" @@ -369,6 +371,9 @@ local function setup(options) vim.api.nvim_create_autocmd("CursorMoved", { -- TODO: use pattern for better perf callback = function(args) + if vim.o.columns <= get_main_width() then + return + end if is_buff_integration(args.buf) then local buf_info = vim.fn.getbufinfo(args.buf) diff --git a/tests/scripts/init_with_zen_small.lua b/tests/scripts/init_with_zen_small.lua index 54d6665..b56fbb9 100644 --- a/tests/scripts/init_with_zen_small.lua +++ b/tests/scripts/init_with_zen_small.lua @@ -9,18 +9,8 @@ require("trouble").setup({ open_no_results = true }) require("fyler").setup({}) require("neotest").setup({ adapters = {} }) require("zen").setup({ - top = { - { filetype = "fugitive" }, - }, - bottom = { - { filetype = "trouble" }, - }, - left = { - { filetype = "*", min_width = 46 }, - { filetype = "fyler_finder" }, - }, - right = { - { filetype = "*", min_width = 46 }, - { filetype = "neotest-summary" }, - }, + top = { { filetype = "fugitive" } }, + bottom = { { filetype = "trouble" } }, + left = { { filetype = "fyler_finder" } }, + right = { { filetype = "neotest-summary" } }, }) diff --git a/tests/test_integrations.lua b/tests/test_integrations.lua index 0382943..072a4cc 100644 --- a/tests/test_integrations.lua +++ b/tests/test_integrations.lua @@ -36,7 +36,6 @@ T["left integration"]["opening closes zen side buffer, closing reopens it"] = fu }) end - T["left integration"]["opening an integration should close the existing integration on the same side"] = function() child.cmd("Fyler kind=split_left_most") @@ -61,6 +60,20 @@ T["left integration"]["opening an integration should close the existing integrat }) end +T["left integration"]["opening an integration on a small window"] = function() + child.restart({ "-u", "tests/scripts/init_with_zen_small.lua" }) + + child.cmd("Fyler kind=split_left_most") + + Helpers.expect.layout(child, { + type = "row", + children = { + { type = "leaf", filetype = "fyler_finder", buftype = "acwrite", width = 35, height = 50 }, + { type = "leaf", filetype = "", buftype = "", width = 104, height = 50 }, + }, + }) +end + T["top integration"] = MiniTest.new_set({}) T["top integration"]["opening"] = function() @@ -118,8 +131,7 @@ T["top integration"]["opening an integration should close the existing integrati }) end - -for _, case in ipairs({ { name = "above", }, { name = "below", } }) do +for _, case in ipairs({ { name = "above" }, { name = "below" } }) do T["top integration"]["stacks " .. case.name .. " when configured " .. case.name .. " with replace=false"] = function() if case.name == "below" then child.restart({ "-u", "tests/scripts/init_with_zen_top_order.lua" }) @@ -202,6 +214,41 @@ for _, case in ipairs({ { name = "above", }, { name = "below", } }) do end end +T["top integration"]["closing a git commit keeps the top and bottom stacks intact"] = function() + child.lua([[ + local tmpdir = vim.fn.tempname() + vim.fn.mkdir(tmpdir, "p") + vim.fn.system({ "git", "init", tmpdir }) + vim.fn.system({ "git", "-C", tmpdir, "config", "user.name", "Test" }) + vim.fn.system({ "git", "-C", tmpdir, "config", "user.email", "test@test.com" }) + vim.fn.system({ "git", "-C", tmpdir, "commit", "--allow-empty", "-m", "initial" }) + vim.fn.writefile({ "hello" }, tmpdir .. "/file") + vim.cmd("edit " .. tmpdir .. "/file") + ]]) + + child.cmd("Git") + child.cmd("Trouble diagnostics") + child.cmd("wincmd t") + child.cmd("Git commit --allow-empty") + child.cmd("q") + + Helpers.expect.layout(child, { + type = "col", + children = { + { type = "leaf", filetype = "fugitive", buftype = "nowrite", width = 240, height = 25 }, + { + type = "row", + children = { + { type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 13 }, + { type = "leaf", filetype = "", buftype = "", width = 146, height = 13 }, + { type = "leaf", filetype = "zen-right", buftype = "nofile", width = 46, height = 13 }, + }, + }, + { type = "leaf", filetype = "trouble", buftype = "nofile", width = 240, height = 10 }, + }, + }) +end + T["bottom integration"] = MiniTest.new_set({}) T["bottom integration"]["opening"] = function()