-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout_spec.lua
More file actions
322 lines (272 loc) · 9.24 KB
/
layout_spec.lua
File metadata and controls
322 lines (272 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
local config = require("peekstack.config")
local layout = require("peekstack.core.layout")
local stack = require("peekstack.core.stack")
local helpers = require("tests.helpers")
describe("layout.compute", function()
local original_columns
local original_lines
local original_cmdheight
before_each(function()
original_columns = vim.o.columns
original_lines = vim.o.lines
original_cmdheight = vim.o.cmdheight
vim.o.columns = 120
vim.o.lines = 40
vim.o.cmdheight = 1
end)
after_each(function()
vim.o.columns = original_columns
vim.o.lines = original_lines
vim.o.cmdheight = original_cmdheight
config.setup({})
end)
it("shrinks and offsets for stack style", function()
config.setup({
ui = {
layout = {
style = "stack",
offset = { row = 1, col = 2 },
shrink = { w = 4, h = 2 },
min_size = { w = 20, h = 10 },
max_ratio = 1,
zindex_base = 50,
},
},
})
local first = layout.compute(1)
local second = layout.compute(2)
assert.is_true(second.width < first.width)
assert.is_true(second.height < first.height)
assert.is_true(second.row >= first.row)
assert.is_true(second.col >= first.col)
end)
it("cascades without shrinking for cascade style", function()
config.setup({
ui = {
layout = {
style = "cascade",
offset = { row = 1, col = 2 },
shrink = { w = 4, h = 2 },
min_size = { w = 20, h = 10 },
max_ratio = 1,
zindex_base = 50,
},
},
})
local first = layout.compute(1)
local second = layout.compute(2)
assert.equals(first.width, second.width)
assert.equals(first.height, second.height)
assert.equals(first.row + 1, second.row)
assert.equals(first.col + 2, second.col)
end)
it("keeps position and size for single style", function()
config.setup({
ui = {
layout = {
style = "single",
offset = { row = 1, col = 2 },
shrink = { w = 4, h = 2 },
min_size = { w = 20, h = 10 },
max_ratio = 1,
zindex_base = 50,
},
},
})
local first = layout.compute(1)
local second = layout.compute(2)
assert.equals(first.width, second.width)
assert.equals(first.height, second.height)
assert.equals(first.row, second.row)
assert.equals(first.col, second.col)
end)
it("clamps to min_size on very small screen", function()
vim.o.columns = 30
vim.o.lines = 10
config.setup({
ui = {
layout = {
style = "stack",
offset = { row = 1, col = 4 },
shrink = { w = 4, h = 2 },
min_size = { w = 60, h = 12 },
max_ratio = 0.65,
zindex_base = 50,
},
},
})
local result = layout.compute(1)
assert.equals(30, result.width) -- clamped to columns (< min_size.w)
assert.equals(9, result.height) -- clamped to lines (< min_size.h)
assert.is_true(result.row >= 0)
assert.is_true(result.col >= 0)
end)
it("produces non-negative row and col for all indices", function()
vim.o.columns = 40
vim.o.lines = 15
config.setup({
ui = {
layout = {
style = "stack",
offset = { row = 1, col = 4 },
shrink = { w = 4, h = 2 },
min_size = { w = 20, h = 6 },
max_ratio = 0.65,
zindex_base = 50,
},
},
})
for idx = 1, 5 do
local result = layout.compute(idx)
assert.is_true(result.width > 0, "width should be positive at index " .. idx)
assert.is_true(result.height > 0, "height should be positive at index " .. idx)
assert.is_true(result.row >= 0, "row should be non-negative at index " .. idx)
assert.is_true(result.col >= 0, "col should be non-negative at index " .. idx)
end
end)
end)
describe("layout.reflow", function()
before_each(function()
stack._reset()
config.setup({})
end)
after_each(function()
local s = stack.current_stack()
for i = #s.popups, 1, -1 do
stack.close(s.popups[i].id)
end
stack._reset()
end)
it("does not error on empty stack", function()
local s = stack.current_stack()
assert.equals(0, #s.popups)
assert.has_no.errors(function()
layout.reflow(s)
end)
end)
it("recalculates popup dimensions after screen size change", function()
local original_columns = vim.o.columns
local original_lines = vim.o.lines
local loc = helpers.make_location()
local m1 = stack.push(loc)
assert.is_not_nil(m1)
local cfg_before = vim.api.nvim_win_get_config(m1.winid)
-- Simulate screen resize
vim.o.columns = math.floor(original_columns / 2)
vim.o.lines = math.floor(original_lines / 2)
local s = stack.current_stack()
layout.reflow(s)
local cfg_after = vim.api.nvim_win_get_config(m1.winid)
assert.is_true(cfg_after.width <= cfg_before.width)
vim.o.columns = original_columns
vim.o.lines = original_lines
end)
end)
describe("layout.update_focus_zindex", function()
before_each(function()
stack._reset()
config.setup({})
end)
after_each(function()
-- close all popups
local s = stack.current_stack()
for i = #s.popups, 1, -1 do
stack.close(s.popups[i].id)
end
stack._reset()
end)
it("raises focused popup above all others", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
local m2 = stack.push(loc)
local m3 = stack.push(loc)
assert.is_not_nil(m1)
assert.is_not_nil(m2)
assert.is_not_nil(m3)
local s = stack.current_stack()
local base = config.get().ui.layout.zindex_base
-- Before: focused popup (m3) is raised above natural order
local cfg1 = vim.api.nvim_win_get_config(m1.winid)
local cfg2 = vim.api.nvim_win_get_config(m2.winid)
local cfg3 = vim.api.nvim_win_get_config(m3.winid)
assert.equals(base, cfg1.zindex)
assert.equals(base + 1, cfg2.zindex)
assert.equals(base + 3, cfg3.zindex)
-- Focus m1 (the bottom popup)
layout.update_focus_zindex(s, m1.winid)
-- After: m1 should be at the top (base + 3), others at natural
cfg1 = vim.api.nvim_win_get_config(m1.winid)
cfg2 = vim.api.nvim_win_get_config(m2.winid)
cfg3 = vim.api.nvim_win_get_config(m3.winid)
assert.equals(base + 3, cfg1.zindex) -- focused → top
assert.equals(base + 1, cfg2.zindex) -- natural
assert.equals(base + 2, cfg3.zindex) -- natural
end)
it("restores natural zindex when a different popup is focused", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
local m2 = stack.push(loc)
assert.is_not_nil(m1)
assert.is_not_nil(m2)
local s = stack.current_stack()
local base = config.get().ui.layout.zindex_base
-- Focus m1
layout.update_focus_zindex(s, m1.winid)
local cfg1 = vim.api.nvim_win_get_config(m1.winid)
assert.equals(base + 2, cfg1.zindex)
-- Now focus m2
layout.update_focus_zindex(s, m2.winid)
cfg1 = vim.api.nvim_win_get_config(m1.winid)
local cfg2 = vim.api.nvim_win_get_config(m2.winid)
assert.equals(base, cfg1.zindex) -- restored to natural
assert.equals(base + 2, cfg2.zindex) -- now focused → top
end)
it("keeps focused popup above others after reflow", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
local m2 = stack.push(loc)
assert.is_not_nil(m1)
assert.is_not_nil(m2)
stack.focus_by_id(m1.id)
stack.reflow_all()
local base = config.get().ui.layout.zindex_base
local cfg1 = vim.api.nvim_win_get_config(m1.winid)
local cfg2 = vim.api.nvim_win_get_config(m2.winid)
assert.equals(base + 2, cfg1.zindex)
assert.equals(base + 1, cfg2.zindex)
end)
it("sets focused popup winhighlight to PeekstackPopupBorderFocused", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
local m2 = stack.push(loc)
assert.is_not_nil(m1)
assert.is_not_nil(m2)
local s = stack.current_stack()
layout.update_focus_zindex(s, m2.winid)
local wh2 = vim.wo[m2.winid].winhighlight
assert.is_true(wh2:find("PeekstackPopupBorderFocused", 1, true) ~= nil)
local wh1 = vim.wo[m1.winid].winhighlight
assert.is_true(wh1:find("PeekstackPopupBorder", 1, true) ~= nil)
assert.is_nil(wh1:find("PeekstackPopupBorderFocused", 1, true))
end)
it("swaps winhighlight when focus changes", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
local m2 = stack.push(loc)
assert.is_not_nil(m1)
assert.is_not_nil(m2)
local s = stack.current_stack()
layout.update_focus_zindex(s, m2.winid)
assert.is_true(vim.wo[m2.winid].winhighlight:find("PeekstackPopupBorderFocused", 1, true) ~= nil)
layout.update_focus_zindex(s, m1.winid)
assert.is_true(vim.wo[m1.winid].winhighlight:find("PeekstackPopupBorderFocused", 1, true) ~= nil)
assert.is_nil(vim.wo[m2.winid].winhighlight:find("PeekstackPopupBorderFocused", 1, true))
end)
it("sets initial winhighlight as focused on new popup", function()
local loc = helpers.make_location()
local m1 = stack.push(loc)
assert.is_not_nil(m1)
local wh = vim.wo[m1.winid].winhighlight
assert.is_true(wh:find("PeekstackPopupBorderFocused", 1, true) ~= nil)
end)
end)