Skip to content

Commit

Permalink
feat: invert opts for api functions
Browse files Browse the repository at this point in the history
Rather than noautosave and nosave, use autosave and save. This requires
a util function to override any defaults, but leads to a cleaner api.
  • Loading branch information
natecraddock committed Jan 30, 2022
1 parent 6e9392d commit 206662d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lua/sessions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ M.write_session_file = function()
end

-- start autosaving changes to the session file
local start_autosave = function(path, opts)
opts = opts or {}

local start_autosave = function()
-- save future changes
local events = vim.fn.join(config.events, ",")
vim.cmd(string.format([[
Expand All @@ -73,7 +71,9 @@ end

-- stop autosaving changes to the session file
M.stop_autosave = function(opts)
opts = opts or {}
opts = util.merge({
save = true,
}, opts)

if not session_file_path then return end
vim.cmd[[
Expand All @@ -82,7 +82,7 @@ M.stop_autosave = function(opts)
]]

-- save before stopping
if not opts.nosave then
if opts.save then
M.write_session_file()
end

Expand All @@ -91,7 +91,9 @@ end

-- save or overwrite a session file to the given path
M.save = function(path, opts)
opts = opts or {}
opts = util.merge({
autosave = true,
}, opts)

path = get_session_path(path)
if not path then
Expand All @@ -102,13 +104,16 @@ M.save = function(path, opts)
session_file_path = path
M.write_session_file()

if opts.noautosave then return end
start_autosave(path)
if not opts.autosave then return end
start_autosave()
end

-- load a session file from the given path
M.load = function(path, opts)
opts = opts or {}
opts = util.merge({
autosave = true,
silent = false,
}, opts)

path = get_session_path(path)
if not path then
Expand All @@ -121,8 +126,8 @@ M.load = function(path, opts)
session_file_path = path
vim.cmd(string.format("silent! source %s", path))

if opts.noautosave then return end
start_autosave(path)
if not opts.autosave then return end
start_autosave()
end

local subcommands = { "save", "load", "start", "stop" }
Expand Down Expand Up @@ -165,28 +170,27 @@ M.parse_args = function(subcommand, bang, path)

if subcommand == "save" then
if bang then
M.save(path, { noautosave = true })
M.save(path, { autosave = false })
else
M.save(path)
end
elseif subcommand == "load" then
if bang then
M.load(path, { noautosave = true })
M.load(path, { autosave = false })
else
M.load(path)
end
elseif subcommand == "stop" then
if bang then
M.stop_autosave({ nosave = true })
M.stop_autosave({ save = false })
else
M.stop_autosave()
end
end
end

M.setup = function(opts)
opts = opts or {}
config = vim.tbl_deep_extend("force", {}, config, opts)
config = util.merge(config, opts)

-- register commands
vim.cmd[[
Expand Down
9 changes: 9 additions & 0 deletions lua/sessions/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ M.slice = function(tbl, s, e)
return { unpack(tbl, s, e) }
end

---@param default table
---@param override table
---@return table
---merges override into default, overriding anything in default
M.merge = function(default, override)
override = override or {}
return vim.tbl_deep_extend("force", {}, default, override)
end

return M

0 comments on commit 206662d

Please sign in to comment.