Skip to content

Commit

Permalink
feat(api): Add method to jump to previous window
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjones2014 committed Mar 4, 2024
1 parent 07fa44c commit 5c18899
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lua/smart-splits/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local lazy = require('smart-splits.lazy')
local config = lazy.require_on_index('smart-splits.config') --[[@as SmartSplitsConfig]]
local mux = lazy.require_on_exported_call('smart-splits.mux') --[[@as SmartSplitsMultiplexer]]
local utils = require('smart-splits.utils')
local mux_utils = require('smart-splits.mux.utils')
local types = require('smart-splits.types')
local Direction = types.Direction
local AtEdgeBehavior = types.AtEdgeBehavior
Expand Down Expand Up @@ -489,4 +490,11 @@ end, {
Direction.down,
})

function M.move_cursor_previous_win()
local win = mux_utils.get_previous_win()
if win then
vim.api.nvim_set_current_win(win)
end
end

return M
2 changes: 2 additions & 0 deletions lua/smart-splits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ end, {
Direction.down,
})

M.move_cursor_previous_win = lazy.require_on_exported_call('smart-splits.api').move_cursor_previous_win

M.start_resize_mode = function()
require('smart-splits.resize-mode').start_resize_mode()
end
Expand Down
16 changes: 16 additions & 0 deletions lua/smart-splits/mux/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,27 @@ function M.are_we_gui()
return current_ui ~= nil and not current_ui.stdin_tty and not current_ui.stdout_tty
end

local prev_win = nil

---Return the buf ID of the previous buffer, if there is one
---@return number|nil
function M.get_previous_win()
return prev_win
end

---Initialization for mux capabilities.
---If selected mux has an `on_init` or `on_exit`,
---call `on_init` and set up autocmds to call `on_init` on `VimResume`
---and `on_exit` on `VimSuspend` and `VimLeavePre`.
function M.startup()
-- buffer tracking for "previous buffer"
vim.api.nvim_create_autocmd('WinLeave', {
callback = function()
prev_win = tonumber(vim.api.nvim_get_current_win())
end,
})

-- multiplexer startup/shutdown events
local mux = require('smart-splits.mux').get()
if not mux then
return
Expand Down

0 comments on commit 5c18899

Please sign in to comment.