Skip to content

Commit d98ee70

Browse files
committed
fix(buf_fix_win): prevent duplicate BufWinEnter autocmds for the same buffer
1 parent ab6647a commit d98ee70

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

lua/opencode/ui/buf_fix_win.lua

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ local function close_duplicates(buf, get_win)
1515
end
1616

1717
local wins = vim.fn.win_findbuf(buf)
18-
if #wins <= 1 then
19-
return
20-
end
21-
22-
for _, win in ipairs(wins) do
23-
if win ~= intended and vim.api.nvim_win_is_valid(win) then
24-
vim.schedule(function()
25-
pcall(vim.api.nvim_win_close, win, true)
26-
end)
27-
end
18+
if #wins > 1 then
19+
vim.schedule(function()
20+
for _, win in ipairs(wins) do
21+
if win ~= intended and vim.api.nvim_win_is_valid(win) then
22+
pcall(vim.api.nvim_win_close, win, true)
23+
end
24+
end
25+
end)
2826
end
2927
end
3028

lua/opencode/ui/input_window.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function M.create_buf()
6565

6666
local buffixwin = require('opencode.ui.buf_fix_win')
6767
buffixwin.fix_to_win(input_buf, function()
68-
local state = require('opencode.state')
6968
return state.windows and state.windows.input_win
7069
end)
7170

lua/opencode/ui/output_window.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function M.create_buf()
1111

1212
local buffixwin = require('opencode.ui.buf_fix_win')
1313
buffixwin.fix_to_win(output_buf, function()
14-
local state = require('opencode.state')
1514
return state.windows and state.windows.output_win
1615
end)
1716

tests/unit/buf_fix_win_spec.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,25 @@ describe('buf_fix_win module', function()
358358
assert.equal(initial_buf2 + 1, final_buf2)
359359
end)
360360

361+
it('should not accumulate BufWinEnter autocmds when fix_to_win is called multiple times for same buffer', function()
362+
local buf = create_test_buffer()
363+
local win = create_test_window(buf)
364+
365+
local initial = #vim.api.nvim_get_autocmds({ event = 'BufWinEnter', buffer = buf })
366+
367+
fresh_buf_fix_win.fix_to_win(buf, function()
368+
return win
369+
end)
370+
local after_first = #vim.api.nvim_get_autocmds({ event = 'BufWinEnter', buffer = buf })
371+
fresh_buf_fix_win.fix_to_win(buf, function()
372+
return win
373+
end)
374+
local after_second = #vim.api.nvim_get_autocmds({ event = 'BufWinEnter', buffer = buf })
375+
376+
assert.equal(initial + 1, after_first)
377+
assert.equal(initial + 1, after_second)
378+
end)
379+
361380
it('should setup global VimResized autocmd when fix_to_win is first called', function()
362381
local buf = create_test_buffer()
363382
local win = create_test_window(buf)

0 commit comments

Comments
 (0)