Skip to content

Commit

Permalink
Fix popup resize mechanism.
Browse files Browse the repository at this point in the history
The WinClosed event is triggered right before the window being closed is
removed from the window layout. Hence, it is not helpful in resizing
popups. Tying popup resizes to the WinResize event works and makes more
sense.
  • Loading branch information
carbon-steel committed Dec 3, 2023
1 parent 4170b11 commit fdddf01
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lua/detour/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ local function popup(bufnr)
group = augroup_id,
callback = function ()
for _, x in ipairs(vim.v.event.windows) do
if util.contains_element(covered_windows, x) then
if util.contains_element(vim.api.nvim_tabpage_list_wins(tab_id), x) then
local new_window_opts = construct_window_opts(covered_windows, tab_id)
resize_popup(popup_id, new_window_opts)
break
Expand Down
34 changes: 34 additions & 0 deletions spec/detour_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ local function switch_to_window(window_id)
end
end

local function overlap(win_a, win_b)
local top_a, bottom_a, left_a, right_a = util.get_window_coordinates(win_a)
local top_b, bottom_b, left_b, right_b = util.get_window_coordinates(win_b)
print(top_a, bottom_a, left_a, right_a)
print(top_b, bottom_b, left_b, right_b)

if math.max(left_a, left_b) > math.min(right_a, right_b) then
return false
end

if math.max(top_a, top_b) > math.min(bottom_a, bottom_b) then
return false
end

return true
end

describe("detour", function ()
before_each(function ()
vim.cmd[[
Expand Down Expand Up @@ -104,4 +121,21 @@ describe("detour", function ()
assert.True(util.contains_element(windows, split_popup))
assert.same(#windows, 2)
end)

it("react to a coverable window closing", function ()
pending("WinResized doesn't seem to work when running nvim as a command.")
vim.cmd.wincmd('v')
local coverable_window = vim.api.nvim_get_current_win()
detour.Detour()
local popup = vim.api.nvim_get_current_win()
switch_to_window(coverable_window)
vim.cmd.wincmd('s')
local uncoverable_win = vim.api.nvim_get_current_win()
switch_to_window(coverable_window)
print(util.get_window_coordinates(uncoverable_win))
vim.cmd.close()
print(util.get_window_coordinates(uncoverable_win))

assert.False(overlap(popup, uncoverable_win))
end)
end)

0 comments on commit fdddf01

Please sign in to comment.