Skip to content

Commit ca33ba7

Browse files
authored
feat: integration with a larger min_width should override the wildcard default (#15)
1 parent 48e6b3e commit ca33ba7

6 files changed

Lines changed: 103 additions & 38 deletions

File tree

README.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ return {
3636
{ filetype = "fugitive" },
3737
},
3838
right = {
39-
min_width = 46,
40-
{ filetype = "copilot-chat" },
39+
{ filetype = "*", min_width = 46 },
40+
{ filetype = "copilot-chat", min_width = 60 },
4141
{ filetype = "neotest-summary" },
4242
{ filetype = { "dapui_watches", "dapui_scopes", "dapui_stacks", "dapui_breakpoints" } },
4343
},
@@ -48,7 +48,7 @@ return {
4848
{ filetype = "noice" }, -- noice opens large notifications in a buffer
4949
},
5050
left = {
51-
min_width = 46,
51+
{ filetype = "*", min_width = 46 },
5252
{ filetype = "fugitiveblame" },
5353
{ filetype = "fyler" },
5454
{ filetype = "neotree" },
@@ -83,41 +83,34 @@ programs.nvf.settings.vim.lazy.plugins = {
8383
main = {
8484
width = 148;
8585
};
86+
# TIP: find a buffer's filetype with :lua print(vim.bo.filetype)
8687
top = [
8788
{ filetype = "fugitive"; }
8889
{ filetype = "git"; }
8990
{ filetype = "man"; }
9091
{ filetype = "help"; }
9192
{ filetype = "gitcommit"; }
9293
];
93-
right =
94-
lib.generators.mkLuaInline # lua
95-
''
96-
{
97-
min_width = 46,
98-
{ filetype = "copilot-chat" },
99-
{ filetype = "neotest-summary" },
100-
{ filetype = { "dapui_watches", "dapui_scopes", "dapui_stacks", "dapui_breakpoints" } },
101-
}
102-
'';
94+
right = [
95+
{ filetype = "*"; min_width = 46; }
96+
{ filetype = "copilot-chat"; min_width = 60; }
97+
{ filetype = "neotest-summary"; }
98+
{ filetype = [ "dapui_watches" "dapui_scopes" "dapui_stacks" "dapui_breakpoints" ]; }
99+
];
103100
bottom = [
104101
{ filetype = "dap-repl"; }
105102
{ filetype = "qf"; }
106103
{ filetype = "trouble"; }
107104
{ filetype = "noice"; } # noice opens large notifications in a buffer
108105
];
109-
left =
110-
lib.generators.mkLuaInline # lua
111-
''
112-
{
113-
min_width = 46,
114-
{ filetype = "fugitiveblame" },
115-
{ filetype = "fyler" },
116-
{ filetype = "neotree" },
117-
{ filetype = "dbui" },
118-
{ filetype = { "undotree", "diff" } },
119-
}
120-
'';
106+
left = [
107+
{ filetype = "*"; min_width = 46; }
108+
{ filetype = "fugitiveblame"; }
109+
{ filetype = "fyler"; }
110+
{ filetype = "neotree"; }
111+
{ filetype = "dbui"; }
112+
{ filetype = [ "undotree" "diff" ]; }
113+
];
121114
};
122115
};
123116
};

lua/zen/init.lua

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22

33
--- @class Integration
44
--- @field filetype Filetype
5+
--- @field min_width? number
56

67
--- @class Config
78
--- @field main? { width: number | fun(): number; }
89
--- @field top? Integration[]
9-
--- @field right? { min_width?: number; [number]: Integration[]}
10+
--- @field right? Integration[]
1011
--- @field bottom? Integration[]
11-
--- @field left? { min_width?: number; [number]: Integration[]}
12+
--- @field left? Integration[]
1213

1314
--- @class ConfigOptions
1415
--- @field main { width: number | fun(): number; }
1516
--- @field top Integration[]
16-
--- @field right { min_width: number; [number]: Integration[]}
17+
--- @field right Integration[]
1718
--- @field bottom Integration[]
18-
--- @field left { min_width: number; [number]: Integration[]}
19+
--- @field left Integration[]
1920

2021
local default_width = 148
2122
--- @type ConfigOptions
2223
local opts = {
2324
main = { width = default_width },
2425
top = {},
25-
right = { min_width = 46 },
26+
right = { { filetype = "*", min_width = 46 } },
2627
bottom = {},
27-
left = { min_width = 46 },
28+
left = { { filetype = "*", min_width = 46 } },
2829
}
2930
local state = {
3031
[vim.api.nvim_get_current_tabpage()] = { left = nil, right = nil },
@@ -69,13 +70,31 @@ end
6970
---@return boolean
7071
local function is_filetype(target, filetype)
7172
if type(filetype) == "string" then
72-
return filetype == target
73+
return filetype ~= "*" and filetype == target
7374
elseif type(filetype) == "table" then
7475
return vim.tbl_contains(filetype, target)
7576
end
7677
return false
7778
end
7879

80+
---@param position "left" | "right"
81+
---@param filetype string
82+
---@return number
83+
local function get_min_width(position, filetype)
84+
local wildcard_width = 0
85+
for _, integration in ipairs(opts[position]) do
86+
if type(integration) == "table" then
87+
if integration.min_width and integration.filetype ~= "*" and is_filetype(filetype, integration.filetype) then
88+
return integration.min_width
89+
end
90+
if integration.filetype == "*" and integration.min_width then
91+
wildcard_width = integration.min_width
92+
end
93+
end
94+
end
95+
return wildcard_width
96+
end
97+
7998
---@return number
8099
local function get_side_buffer(position)
81100
local current_tabpage = vim.api.nvim_get_current_tabpage()
@@ -262,8 +281,8 @@ local function setup(options)
262281
for _, position in ipairs({ "right", "left" }) do
263282
for _, integration in pairs(opts[position]) do
264283
---@diagnostic disable-next-line: undefined-field
265-
if type(integration) == "table" and integration.filetype == filetype then
266-
local new_width = math.max(opts[position].min_width, math.floor((vim.o.columns - get_main_width()) / 2))
284+
if type(integration) == "table" and is_filetype(filetype, integration.filetype) then
285+
local new_width = math.max(get_min_width(position, filetype), math.floor((vim.o.columns - get_main_width()) / 2))
267286
vim.api.nvim_win_set_width(buf_info[1].windows[1], new_width)
268287
return
269288
end
@@ -460,6 +479,19 @@ local function setup(options)
460479
end
461480
end
462481
end
482+
483+
if position == "left" or position == "right" then
484+
local min_width = get_min_width(position, filetype)
485+
if min_width > 0 then
486+
local win = vim.fn.bufwinid(args.buf)
487+
if win ~= -1 then
488+
local current_width = vim.api.nvim_win_get_width(win)
489+
if current_width < min_width then
490+
vim.api.nvim_win_set_width(win, min_width)
491+
end
492+
end
493+
end
494+
end
463495
end
464496
end
465497
end

tests/scripts/init_with_zen.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require("fyler").setup({})
1010
require("neotest").setup({ adapters = {} })
1111
require("dapui").setup({})
1212
require("CopilotChat").setup({})
13+
1314
require("zen").setup({
1415
top = {
1516
{ filetype = "fugitive" },
@@ -20,12 +21,12 @@ require("zen").setup({
2021
{ filetype = "qf" },
2122
},
2223
left = {
23-
min_width = 46,
24+
{ filetype = "*", min_width = 46 },
2425
{ filetype = "fyler" },
2526
{ filetype = "dbui" },
2627
},
2728
right = {
28-
min_width = 46,
29+
{ filetype = "*", min_width = 46 },
2930
{ filetype = "neotest-summary" },
3031
{ filetype = "copilot-chat" },
3132
{ filetype = { "dapui_watches", "dapui_scopes", "dapui_stacks", "dapui_breakpoints" } },
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
vim.cmd([[let &rtp.=','.getcwd()]])
2+
vim.opt.packpath:prepend("deps")
3+
4+
vim.o.columns = 240
5+
vim.o.lines = 52
6+
7+
require("mini.test").setup()
8+
require("CopilotChat").setup()
9+
10+
require("zen").setup({
11+
right = {
12+
{ filetype = "*", min_width = 46 },
13+
{ filetype = "copilot-chat", min_width = 60 },
14+
},
15+
})

tests/scripts/init_with_zen_small.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ require("zen").setup({
1616
{ filetype = "trouble" },
1717
},
1818
left = {
19-
min_width = 46,
19+
{ filetype = "*", min_width = 46 },
2020
{ filetype = "fyler" },
2121
},
2222
right = {
23-
min_width = 46,
23+
{ filetype = "*", min_width = 46 },
2424
{ filetype = "neotest-summary" },
2525
},
2626
})

tests/test_integrations.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,28 @@ T["right integration"]["opening an integration with table filetype"] = function(
257257
})
258258
end
259259

260+
local min_width_child = MiniTest.new_child_neovim()
261+
262+
T["min_width"] = MiniTest.new_set({
263+
hooks = {
264+
pre_case = function()
265+
min_width_child.restart({ "-u", "tests/scripts/init_with_zen_min_width.lua" })
266+
end,
267+
post_once = min_width_child.stop,
268+
},
269+
})
270+
271+
T["min_width"]["integration with a larger min_width should override the wildcard default"] = function()
272+
min_width_child.lua([[vim.o.splitright = true; require("CopilotChat").open()]])
273+
274+
Helpers.expect.layout(min_width_child, {
275+
type = "row",
276+
children = {
277+
{ type = "leaf", filetype = "zen-left", buftype = "nofile", width = 46, height = 50 },
278+
{ type = "leaf", filetype = "", buftype = "", width = 132, height = 50 },
279+
{ type = "leaf", filetype = "copilot-chat", buftype = "nofile", width = 60, height = 50 },
280+
},
281+
})
282+
end
283+
260284
return T

0 commit comments

Comments
 (0)