-
Notifications
You must be signed in to change notification settings - Fork 36
fix(ui): prevent duplicate windows by implementing buffer-to-window binding #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0708c35
fix(ui): prevent duplicate windows
disrupted 574c536
fix: prevent duplicate windows by implementing buffer-to-window binding
sudo-tee fccd818
feat(ui): add toggle between tab pages
sudo-tee ab6647a
fix(ui): prevent duplicate windows by cleaning up autocmds
sudo-tee d98ee70
fix(buf_fix_win): prevent duplicate BufWinEnter autocmds for the same…
sudo-tee 7f1938e
fix(tests): increase timer test wait timeout to prevent flakiness
sudo-tee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| --- Prevents a buffer from appearing in multiple windows (opposite of 'winfixbuf') | ||
| --- | ||
| --- This module solves the problem where buffers can appear in multiple windows | ||
| --- despite having 'winfixbuf' set. The 'winfixbuf' option prevents a window from | ||
| --- changing buffers, but doesn't prevent a buffer from appearing in multiple windows. | ||
| --- | ||
| local M = {} | ||
| local buff_to_win_map = {} | ||
| local global_autocmd_setup = false | ||
|
|
||
| local function close_duplicates(buf, get_win) | ||
| local intended = get_win() | ||
| if not intended or not vim.api.nvim_win_is_valid(intended) then | ||
| return | ||
| end | ||
|
|
||
| local wins = vim.fn.win_findbuf(buf) | ||
| if #wins > 1 then | ||
| vim.schedule(function() | ||
| for _, win in ipairs(wins) do | ||
| if win ~= intended and vim.api.nvim_win_is_valid(win) then | ||
| pcall(vim.api.nvim_win_close, win, true) | ||
| end | ||
| end | ||
| end) | ||
| end | ||
| end | ||
|
|
||
| local check_all_buffers = vim.schedule_wrap(function() | ||
| for buf, get_win in pairs(buff_to_win_map) do | ||
| if vim.api.nvim_buf_is_valid(buf) then | ||
| close_duplicates(buf, get_win) | ||
| else | ||
| buff_to_win_map[buf] = nil | ||
| end | ||
| end | ||
| end) | ||
|
|
||
| local function setup() | ||
| if global_autocmd_setup then | ||
| return | ||
| end | ||
| global_autocmd_setup = true | ||
|
|
||
| local augroup = vim.api.nvim_create_augroup('OpenCodeBufFixWin', { clear = false }) | ||
| vim.api.nvim_create_autocmd({ 'WinNew', 'VimResized' }, { | ||
| group = augroup, | ||
| callback = check_all_buffers, | ||
| }) | ||
sudo-tee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vim.api.nvim_create_autocmd('BufDelete', { | ||
| callback = function(args) | ||
| if args and args.buf then | ||
| buff_to_win_map[args.buf] = nil | ||
| end | ||
| end, | ||
| }) | ||
| end | ||
|
|
||
| --- Protect a buffer from appearing in multiple windows | ||
| ---@param buf integer Buffer number | ||
| ---@param get_intended_window fun(): integer? Function returning intended window ID | ||
| function M.fix_to_win(buf, get_intended_window) | ||
| setup() | ||
|
|
||
| buff_to_win_map[buf] = get_intended_window | ||
| local augroup = vim.api.nvim_create_augroup('OpenCodeBufFixWin_' .. buf, { clear = true }) | ||
| vim.api.nvim_create_autocmd('BufWinEnter', { | ||
| group = augroup, | ||
| buffer = buf, | ||
| callback = function() | ||
| close_duplicates(buf, get_intended_window) | ||
| end, | ||
| }) | ||
sudo-tee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| return M | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.