Skip to content

Commit

Permalink
feat: save/load current win and cursor positions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 9, 2022
1 parent af2b62e commit 1b6268f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
39 changes: 32 additions & 7 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ end
---@return string
local function get_session_file(name)
local files = require("resession.files")
return files.get_stdpath_filename("data", "session", string.format("%s.json", name))
return files.get_stdpath_filename(
"data",
"session",
string.format("%s.json", name:gsub(files.sep, "_"))
)
end

---@return string|nil
Expand Down Expand Up @@ -77,12 +81,18 @@ M.delete = function(name)
end

---@class resession.SaveOpts
---@field detach nil|boolean
---@field detach nil|boolean Immediately detach from the saved session
---@field notify nil|boolean Notify on success

---@param name? string
---@param opts? resession.SaveOpts
M.save = function(name, opts)
opts = opts or {}
opts = vim.tbl_extend("keep", opts or {}, {
notify = true,
})
if not name then
name = current_session
end
if not name then
vim.ui.input({ prompt = "Session name" }, function(selected)
if selected then
Expand Down Expand Up @@ -128,6 +138,9 @@ M.save = function(name, opts)
if not opts.detach then
current_session = name
end
if opts.notify then
vim.notify(string.format("Saved session %s", name))
end
end

local function close_everything()
Expand All @@ -141,12 +154,15 @@ local function close_everything()
end

---@class resession.LoadOpts
---@field detach nil|boolean
---@field detach nil|boolean Detach from session after loading
---@field reset nil|boolean Close everthing before loading the session (default true)

---@param name? string
---@param opts? resession.LoadOpts
M.load = function(name, opts)
opts = opts or {}
opts = vim.tbl_extend("keep", opts or {}, {
reset = true,
})
local files = require("resession.files")
local layout = require("resession.layout")
if not name then
Expand All @@ -168,7 +184,11 @@ M.load = function(name, opts)
vim.notify(string.format("Could not find session %s", name), vim.log.levels.ERROR)
return
end
close_everything()
if opts.reset then
close_everything()
else
vim.cmd("tabnew")
end
vim.cmd(string.format("cd %s", data.cwd))
for _, buf in ipairs(data.buffers) do
local bufnr = vim.fn.bufadd(buf.name)
Expand All @@ -180,15 +200,20 @@ M.load = function(name, opts)
end
end

local curwin
for i, tab in ipairs(data.tabs) do
if i > 1 then
vim.cmd("tabnew")
end
if tab.cwd then
vim.cmd(string.format("tcd %s", tab.cwd))
end
layout.set_winlayout(tab.wins)
local win = layout.set_winlayout(tab.wins)
if win then
curwin = win
end
end
vim.api.nvim_set_current_win(curwin.winid)
if not opts.detach then
current_session = name
end
Expand Down
16 changes: 15 additions & 1 deletion lua/resession/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ M.get_win_info = function(tabnr, winid)
end
local win = {
bufname = vim.api.nvim_buf_get_name(bufnr),
current = vim.api.nvim_get_current_win() == winid,
cursor = vim.api.nvim_win_get_cursor(winid),
}
local winnr = vim.api.nvim_win_get_number(winid)
if vim.fn.haslocaldir(winnr, tabnr) == 1 then
Expand Down Expand Up @@ -52,13 +54,21 @@ end

M.set_winlayout = function(layout)
local type = layout[1]
local ret
if type == "leaf" then
local win = layout[2]
local bufnr = vim.fn.bufadd(win.bufname)
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_win_set_cursor(0, win.cursor)
if win.cwd then
vim.cmd(string.format("lcd %s", win.cwd))
end
if win.current then
ret = {
winid = vim.api.nvim_get_current_win(),
cursor = win.cursor,
}
end
else
for i, v in ipairs(layout[2]) do
if i > 1 then
Expand All @@ -68,9 +78,13 @@ M.set_winlayout = function(layout)
vim.cmd("split")
end
end
M.set_winlayout(v)
local result = M.set_winlayout(v)
if result then
ret = result
end
end
end
return ret
end

return M

0 comments on commit 1b6268f

Please sign in to comment.