Skip to content

Commit efcd924

Browse files
sand4rtCopilot
andauthored
fix: close side buffers on last main quit (#1)
* test: improve coverage * fix: add ConfigOptions type with optional fields for setup() user input Agent-Logs-Url: https://github.com/sand4rt/zen.nvim/sessions/ec375353-ede5-4108-8ceb-0ba933b9a808 Co-authored-by: sand4rt <17591696+sand4rt@users.noreply.github.com> * fix: remove trailing semicolons and invalid [number]? from LuaDoc annotations Agent-Logs-Url: https://github.com/sand4rt/zen.nvim/sessions/ec375353-ede5-4108-8ceb-0ba933b9a808 Co-authored-by: sand4rt <17591696+sand4rt@users.noreply.github.com> * refact --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 1d4fbf4 commit efcd924

File tree

8 files changed

+136
-26
lines changed

8 files changed

+136
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DEPS_DIR := $(CURDIR)/deps
44

55
# Download mini.nvim if not present, then run all tests
66
test: deps
7-
nvim --headless -u tests/minimal_init.lua -c "lua MiniTest.run()" 2>&1
7+
nvim --headless -u tests/scripts/minimal_init.lua -c "lua MiniTest.run()"
88

99
# Clone mini.nvim into deps/
1010
deps:

lua/zen/init.lua

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
--- @field filetype Filetype
55

66
--- @class Config
7-
--- @field main { width: number };
8-
--- @field top Integration[];
9-
--- @field right { min_width: number; [number]: Integration[]};
10-
--- @field bottom Integration[];
11-
--- @field left { min_width: number; [number]: Integration[]};
12-
13-
--- @type Config
7+
--- @field main? { width?: number }
8+
--- @field top? Integration[]
9+
--- @field right? { min_width?: number; [number]: Integration[]}
10+
--- @field bottom? Integration[]
11+
--- @field left? { min_width?: number; [number]: Integration[]}
12+
13+
--- @class ConfigOptions
14+
--- @field main { width: number }
15+
--- @field top Integration[]
16+
--- @field right { min_width: number; [number]: Integration[]}
17+
--- @field bottom Integration[]
18+
--- @field left { min_width: number; [number]: Integration[]}
19+
20+
--- @type ConfigOptions
1421
local opts = {
1522
main = { width = 148 },
1623
top = {},
@@ -223,13 +230,13 @@ local function close(filetype)
223230
end
224231
end
225232

226-
---@param options Config
233+
---@param options? Config
227234
local function setup(options)
228235
-- Default splitting will cause your main splits to jump when opening an integration.
229236
-- To prevent this, set `splitkeep` to either `screen` or `topline`.
230237
vim.opt.splitkeep = "screen"
231238

232-
---@type Config
239+
---@type ConfigOptions
233240
opts = vim.tbl_extend("force", opts, options or {})
234241

235242
vim.api.nvim_create_autocmd("CursorMoved", {
@@ -292,15 +299,30 @@ local function setup(options)
292299
if is_popup_window(vim.api.nvim_get_current_win()) then
293300
return
294301
end
295-
if vim.tbl_count(get_editable_files()) == 1 and not is_buff_integration(args.buf) then
296-
close_side_buffer("left")
297-
close_side_buffer("right")
302+
if is_buff_integration(args.buf) then
303+
return
304+
end
298305

299-
for _, position in ipairs({ "top", "right", "bottom", "left" }) do
300-
for _, integration in pairs(opts[position]) do
301-
if type(integration) == "table" then
302-
close(integration.filetype)
303-
end
306+
local editable_count = 0
307+
for _, win in ipairs(vim.api.nvim_list_wins()) do
308+
if not is_popup_window(win) then
309+
local buf = vim.api.nvim_win_get_buf(win)
310+
if not is_buff_integration(buf) then
311+
editable_count = editable_count + 1
312+
end
313+
end
314+
end
315+
if editable_count ~= 1 then
316+
return
317+
end
318+
319+
close_side_buffer("left")
320+
close_side_buffer("right")
321+
322+
for _, position in ipairs({ "top", "right", "bottom", "left" }) do
323+
for _, integration in pairs(opts[position]) do
324+
if type(integration) == "table" then
325+
close(integration.filetype)
304326
end
305327
end
306328
end

tests/scripts/helpers.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
local Helpers = {}
2+
3+
Helpers.expect = vim.deepcopy(MiniTest.expect)
4+
5+
local function error_message(str, pattern)
6+
return string.format("Pattern: %s\nObserved string: %s", vim.inspect(pattern), str)
7+
end
8+
9+
Helpers.expect.layout = MiniTest.new_expectation("window layout matches", function(child, expected)
10+
local layout = child.lua_func(function()
11+
local function enrich(node)
12+
if node[1] == "leaf" then
13+
local win = node[2]
14+
local buf = vim.api.nvim_win_get_buf(win)
15+
return {
16+
type = "leaf",
17+
filetype = vim.bo[buf].filetype,
18+
buftype = vim.bo[buf].buftype,
19+
width = vim.api.nvim_win_get_width(win),
20+
height = vim.api.nvim_win_get_height(win),
21+
}
22+
end
23+
local result = {}
24+
for i, n in ipairs(node[2]) do
25+
result[i] = enrich(n)
26+
end
27+
return { type = node[1], children = result }
28+
end
29+
return enrich(vim.fn.winlayout())
30+
end)
31+
return Helpers.expect.equality(layout, expected)
32+
end, error_message)
33+
34+
return Helpers

tests/scripts/init_with_zen.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vim.cmd([[let &rtp.=','.getcwd()]])
2+
vim.cmd("set rtp+=deps/mini.nvim")
3+
4+
vim.o.columns = 240
5+
vim.o.lines = 52
6+
7+
require("mini.test").setup()
8+
require("zen").setup()

tests/test_closing.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local Helpers = dofile("tests/scripts/helpers.lua")
2+
local child = MiniTest.new_child_neovim()
3+
4+
local T = MiniTest.new_set({
5+
hooks = {
6+
pre_case = function()
7+
child.restart({ "-u", "tests/scripts/init_with_zen.lua" })
8+
end,
9+
post_once = child.stop,
10+
},
11+
})
12+
13+
T["close side buffers on last main quit"] = function()
14+
Helpers.expect.layout(child, {
15+
type = "row",
16+
children = {
17+
{ type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 50 },
18+
{ type = "leaf", filetype = "", buftype = "", width = 146, height = 50 },
19+
{ type = "leaf", filetype = "zen-right", buftype = "nofile", width = 46, height = 50 },
20+
},
21+
})
22+
23+
pcall(child.cmd, "q")
24+
25+
local STOPPED = 0
26+
local exit_code = vim.fn.jobwait({ child.job.id }, 1000)[1]
27+
MiniTest.expect.equality(exit_code, STOPPED)
28+
end
29+
30+
return T

tests/test_opening.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local Helpers = dofile("tests/scripts/helpers.lua")
2+
local child = MiniTest.new_child_neovim()
3+
4+
local T = MiniTest.new_set({
5+
hooks = {
6+
pre_case = function()
7+
child.restart({ "-u", "tests/scripts/init_with_zen.lua" })
8+
end,
9+
post_once = child.stop,
10+
},
11+
})
12+
13+
T["open side buffers on startup"] = function()
14+
Helpers.expect.layout(child, {
15+
type = "row",
16+
children = {
17+
{ type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 50 },
18+
{ type = "leaf", filetype = "", buftype = "", width = 146, height = 50 },
19+
{ type = "leaf", filetype = "zen-right", buftype = "nofile", width = 46, height = 50 },
20+
},
21+
})
22+
end
23+
24+
return T

tests/test_setup.lua

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)