Skip to content

Commit 5c18899

Browse files
committed
feat(api): Add method to jump to previous window
1 parent 07fa44c commit 5c18899

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lua/smart-splits/api.lua

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local lazy = require('smart-splits.lazy')
22
local config = lazy.require_on_index('smart-splits.config') --[[@as SmartSplitsConfig]]
33
local mux = lazy.require_on_exported_call('smart-splits.mux') --[[@as SmartSplitsMultiplexer]]
44
local utils = require('smart-splits.utils')
5+
local mux_utils = require('smart-splits.mux.utils')
56
local types = require('smart-splits.types')
67
local Direction = types.Direction
78
local AtEdgeBehavior = types.AtEdgeBehavior
@@ -489,4 +490,11 @@ end, {
489490
Direction.down,
490491
})
491492

493+
function M.move_cursor_previous_win()
494+
local win = mux_utils.get_previous_win()
495+
if win then
496+
vim.api.nvim_set_current_win(win)
497+
end
498+
end
499+
492500
return M

lua/smart-splits/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ end, {
2121
Direction.down,
2222
})
2323

24+
M.move_cursor_previous_win = lazy.require_on_exported_call('smart-splits.api').move_cursor_previous_win
25+
2426
M.start_resize_mode = function()
2527
require('smart-splits.resize-mode').start_resize_mode()
2628
end

lua/smart-splits/mux/utils.lua

+16
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,27 @@ function M.are_we_gui()
3535
return current_ui ~= nil and not current_ui.stdin_tty and not current_ui.stdout_tty
3636
end
3737

38+
local prev_win = nil
39+
40+
---Return the buf ID of the previous buffer, if there is one
41+
---@return number|nil
42+
function M.get_previous_win()
43+
return prev_win
44+
end
45+
3846
---Initialization for mux capabilities.
3947
---If selected mux has an `on_init` or `on_exit`,
4048
---call `on_init` and set up autocmds to call `on_init` on `VimResume`
4149
---and `on_exit` on `VimSuspend` and `VimLeavePre`.
4250
function M.startup()
51+
-- buffer tracking for "previous buffer"
52+
vim.api.nvim_create_autocmd('WinLeave', {
53+
callback = function()
54+
prev_win = tonumber(vim.api.nvim_get_current_win())
55+
end,
56+
})
57+
58+
-- multiplexer startup/shutdown events
4359
local mux = require('smart-splits.mux').get()
4460
if not mux then
4561
return

0 commit comments

Comments
 (0)