Open scienceplease opened 1 month ago
A silly workaround I have for the time being is just iterating through all the windows on the tab page before and after to detect which have been swapped and then restoring their respective cursor positions.
local function snapshot()
-- Save buffer cursor information for each window in the active tab page.
local state = { }
local tabpagenr = vim.api.nvim_get_current_tabpage()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(tabpagenr)) do
state[winid] = {
bufnr = vim.api.nvim_win_get_buf(winid),
cursor = vim.api.nvim_win_get_cursor(winid)
}
end
return state
end
local function restore_swapped_buffer_cursors(snapshot)
-- Using snapshot information, detect which buffers have been swapped and restore their cursor positions.
local now = snapshot()
local swapped_winids = { }
for winid, buffer in pairs(now) do
if buffer.bufnr ~= snapshot[winid].bufnr then
-- Buffer displayed in window has been swapped.
swapped_winids[#swapped_winids + 1] = winid
end
end
if #swapped_winids ~= 0 then
assert(#swapped_winids == 2, "Number of swapped windows must be 2.")
vim.api.nvim_win_set_cursor(swapped_winids[1], snapshot[swapped_winids[2]].cursor)
vim.api.nvim_win_set_cursor(swapped_winids[2], snapshot[swapped_winids[1]].cursor)
else
-- Do nothing. Swap was between windows displaying the same buffer which cannot be detected by this approach.
end
end
local function swap(direction)
return function()
local saved = snapshot()
smart_splits['swap_buf_' .. direction]({ move_cursor = true })
restore_swapped_buffer_cursors(saved)
end
end
vim.keymap.set('n', '<leader><leader>h', swap('left'))
vim.keymap.set('n', '<leader><leader>j', swap('down'))
vim.keymap.set('n', '<leader><leader>k', swap('up'))
vim.keymap.set('n', '<leader><leader>l', swap('right'))
Should be easy enough. We can have the swap_buffer
function save and restore the cursor position, and the alternate window can have it's position restored via a one-time autocmd.
Similar Issues
Description
When swapping buffers between windows where
{ move_cursor = true }
it would be nice if the cursor position in each respective buffer is preserved/restored after the swap. As an example, I can restore the cursor position in the active buffer after the swap via the following.Though with this approach I cannot restore the cursor in the buffer that I am swapping with because getting direction-based window ids is internal to the plugin. An alternative to making this behavior the default would be to expose API functions that allow user code to query for window ids that are left/down/up/right to a given window id. The latter would also enable users to implement custom functionality and not require changes to the plugin itself.