Skip to content

Commit

Permalink
Minor breaking change: replace separators with callbacks
Browse files Browse the repository at this point in the history
I don't think anyone ever changed these settings.
Using callbacks opens up a much larger number of use cases.
Closes #64.
  • Loading branch information
Shatur committed Mar 29, 2023
1 parent e7a2cbf commit d1883f3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
29 changes: 27 additions & 2 deletions lua/session_manager/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local Path = require('plenary.path')
local Enum = require('plenary.enum')

local path_replacer = '__'
local colon_replacer = '++'

local config = {
AutoloadMode = Enum({
'Disabled',
Expand All @@ -9,10 +12,32 @@ local config = {
}),
}

--- Replaces symbols into separators and colons to transform filename into a session directory.
---@param filename string: Filename with expressions to replace.
---@return table: Session directory
local function session_filename_to_dir(filename)
-- Get session filename.
local dir = filename:sub(#tostring(config.sessions_dir) + 2)

dir = dir:gsub(colon_replacer, ':')
dir = dir:gsub(path_replacer, Path.path.sep)
return Path:new(dir)
end

--- Replaces separators and colons into special symbols to transform session directory into a filename.
---@param dir table?: Path to session directory. Defaults to the current working directory if `nil`.
---@return table: Session filename.
local function dir_to_session_filename(dir)
local filename = dir and dir.filename or vim.loop.cwd()
filename = filename:gsub(':', colon_replacer)
filename = filename:gsub(Path.path.sep, path_replacer)
return Path:new(config.sessions_dir):joinpath(filename)
end

config.defaults = {
sessions_dir = Path:new(vim.fn.stdpath('data'), 'sessions'),
path_replacer = '__',
colon_replacer = '++',
session_filename_to_dir = session_filename_to_dir,
dir_to_session_filename = dir_to_session_filename,
autoload_mode = config.AutoloadMode.LastSession,
autosave_last_session = true,
autosave_ignore_not_normal = true,
Expand Down
4 changes: 2 additions & 2 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ end

--- Loads a session for the current working directory.
function session_manager.load_current_dir_session(discard_current)
local session_name = utils.dir_to_session_filename(vim.loop.cwd())
local session_name = config.dir_to_session_filename(vim.loop.cwd())
if session_name:exists() then
utils.load_session(session_name.filename, discard_current)
end
end

--- Saves a session for the current working directory.
function session_manager.save_current_session() utils.save_session(utils.dir_to_session_filename().filename) end
function session_manager.save_current_session() utils.save_session(config.dir_to_session_filename().filename) end

--- Loads a session based on settings. Executed after starting the editor.
function session_manager.autoload_session()
Expand Down
28 changes: 3 additions & 25 deletions lua/session_manager/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function utils.get_last_session_filename()
local most_recent_filename = nil
local most_recent_timestamp = 0
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do
if utils.session_filename_to_dir(session_filename):is_dir() then
if config.session_filename_to_dir(session_filename):is_dir() then
local timestamp = vim.fn.getftime(session_filename)
if most_recent_timestamp < timestamp then
most_recent_timestamp = timestamp
Expand Down Expand Up @@ -114,7 +114,7 @@ end
function utils.get_sessions()
local sessions = {}
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do
local dir = utils.session_filename_to_dir(session_filename)
local dir = config.session_filename_to_dir(session_filename)
if dir:is_dir() then
table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir })
else
Expand All @@ -124,35 +124,13 @@ function utils.get_sessions()
table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end)

-- If the last session is the current one, then preselect the previous one.
if #sessions >= 2 and sessions[1].filename == utils.dir_to_session_filename().filename then
if #sessions >= 2 and sessions[1].filename == config.dir_to_session_filename().filename then
sessions[1], sessions[2] = sessions[2], sessions[1]
end

return sessions
end

--- Replaces symbols into separators and colons to transform filename into a session directory.
---@param filename string: Filename with expressions to replace.
---@return table: Session directory
function utils.session_filename_to_dir(filename)
-- Get session filename.
local dir = filename:sub(#tostring(config.sessions_dir) + 2)

dir = dir:gsub(config.colon_replacer, ':')
dir = dir:gsub(config.path_replacer, Path.path.sep)
return Path:new(dir)
end

--- Replaces separators and colons into special symbols to transform session directory into a filename.
---@param dir table?: Path to session directory. Defaults to the current working directory if `nil`.
---@return table: Session filename.
function utils.dir_to_session_filename(dir)
local filename = dir and dir.filename or vim.loop.cwd()
filename = filename:gsub(':', config.colon_replacer)
filename = filename:gsub(Path.path.sep, config.path_replacer)
return Path:new(config.sessions_dir):joinpath(filename)
end

---@param buffer number: buffer ID.
---@return boolean: `true` if this buffer could be restored later on loading.
function utils.is_restorable(buffer)
Expand Down

0 comments on commit d1883f3

Please sign in to comment.