Skip to content

Commit 548b43a

Browse files
committed
refactor: function names and comment blocks
1 parent 7944b9a commit 548b43a

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

doc/persisted.nvim.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Install the plugin with your preferred package manager:
6060
>vim
6161
" Vim Script
6262
Plug 'olimorris/persisted.nvim'
63-
63+
6464
lua << EOF
6565
require("persisted").setup {
6666
-- your configuration comes here
@@ -275,8 +275,8 @@ Autoloading can be further controlled for certain directories by specifying
275275

276276
**Note**Autoloading will not occur if the plugin is lazy loaded or a user opens
277277
Neovim with arguments other than a single directory argument. For example:
278-
`nvim some_file.rb` will not result in autoloading but
279-
`nvim some/existing/path` or `nvim .` will.
278+
`nvim some_file.rb` will not not result in autoloading but `nvim
279+
some/existing/path` or `nvim .` will.
280280

281281
FOLLOWING CURRENT WORKING DIRECTORY ~
282282

@@ -380,7 +380,7 @@ autocmd can be created to hook into the `PersistedSavePre` event:
380380

381381
>lua
382382
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
383-
383+
384384
vim.api.nvim_create_autocmd({ "User" }, {
385385
pattern = "PersistedSavePre",
386386
group = group,
@@ -397,14 +397,14 @@ made available to the callbacks:
397397

398398
>lua
399399
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
400-
400+
401401
vim.api.nvim_create_autocmd({ "User" }, {
402402
pattern = "PersistedTelescopeLoadPre",
403403
group = group,
404404
callback = function(session)
405405
-- Save the currently loaded session
406406
require("persisted").save({ session = vim.g.persisted_loaded_session })
407-
407+
408408
-- Delete all of the open buffers
409409
vim.api.nvim_input("<ESC>:%bd!<CR>")
410410
end,

lua/persisted/init.lua

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,44 @@ local e = vim.fn.fnameescape
1111
---@param replace string
1212
---@param n? integer
1313
---@return string
14-
---@return integer count
14+
---@return integer
1515
local function escape_pattern(str, pattern, replace, n)
1616
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
1717
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
18+
1819
return string.gsub(str, pattern, replace, n)
1920
end
2021

21-
---Checks if vim was started with either zero arguments or a single argument
22-
---which is a drectory.
22+
---Check any arguments passed to Neovim and verify if they're a directory
2323
---@return boolean
24-
local function is_neovim_start_ok()
24+
local function args_check()
2525
if vim.fn.argc() == 1 then
2626
return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0
2727
end
28+
2829
return vim.fn.argc() == 0
2930
end
3031

31-
---Gets the directory to be used for sessions.
32+
---Get the directory to be used for the session
3233
---@return string
33-
local function get_start_dir()
34+
local function session_dir()
3435
if vim.fn.argc() == 1 then
3536
local dir = vim.fn.expand(vim.fn.argv(0))
36-
if dir == '.' then
37-
return vim.fn.getcwd()
37+
38+
if dir == "." then
39+
return vim.fn.getcwd()
3840
end
41+
3942
if vim.fn.isdirectory(dir) ~= 0 then
4043
return dir
4144
end
4245
end
46+
4347
return vim.fn.getcwd()
4448
end
4549

4650
---Does the current working directory allow for the auto-saving and loading?
47-
---@param dir string
51+
---@param dir string Directory to be used for the session
4852
---@return boolean
4953
local function allow_dir(dir)
5054
local allowed_dirs = config.options.allowed_dirs
@@ -57,7 +61,7 @@ local function allow_dir(dir)
5761
end
5862

5963
---Is the current working directory ignored for auto-saving and loading?
60-
---@param dir string
64+
---@param dir string Directory to be used for the session
6165
---@return boolean
6266
local function ignore_dir(dir)
6367
local ignored_dirs = config.options.ignored_dirs
@@ -70,9 +74,8 @@ local function ignore_dir(dir)
7074
end
7175

7276
---Get the session that was saved last
73-
---@param dir string The directory whose last session to load.
7477
---@return string
75-
local function get_last(dir)
78+
local function get_last()
7679
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)
7780

7881
table.sort(sessions, function(a, b)
@@ -83,10 +86,11 @@ local function get_last(dir)
8386
end
8487

8588
---Get the current Git branch
86-
---@param dir? string The directory to inspect for a Git branch. If not set then the current working directory is used.
89+
---@param dir? string Directory to be used for the session
8790
---@return string|nil
8891
function M.get_branch(dir)
89-
dir = dir or get_start_dir()
92+
dir = dir or session_dir()
93+
9094
if config.options.use_git_branch then
9195
vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null")
9296

@@ -97,10 +101,7 @@ function M.get_branch(dir)
97101

98102
if vim.v.shell_error == 0 then
99103
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
100-
local branch_session = config.options.save_dir
101-
.. dir:gsub(utils.get_dir_pattern(), "%%")
102-
.. branch
103-
.. ".vim"
104+
local branch_session = config.options.save_dir .. dir:gsub(utils.get_dir_pattern(), "%%") .. branch .. ".vim"
104105

105106
-- Try to load the session for the current branch
106107
if vim.fn.filereadable(branch_session) ~= 0 then
@@ -120,17 +121,11 @@ function M.get_branch(dir)
120121
end
121122
end
122123
end
123-
124-
-- -- INFO: This allows users who have `@@main` in their session name to load
125-
-- -- repositories that are not git enabled
126-
-- if config.options.use_old_branching then
127-
-- return config.options.branch_separator .. config.options.default_branch
128-
-- end
129124
end
130125
end
131126

132127
---Get the current session for the current working directory and git branch
133-
---@param dir string The directory whose session file to get.
128+
---@param dir string Directory to be used for the session
134129
---@return string
135130
local function get_current(dir)
136131
local name = dir:gsub(utils.get_dir_pattern(), "%%")
@@ -140,10 +135,11 @@ local function get_current(dir)
140135
end
141136

142137
---Determine if a session for the current wording directory, exists
143-
---@param dir? string The directory whose associated session to check if exists. If not set, current working directory is used.
138+
---@param dir? string Directory to be used for the session
144139
---@return boolean
145140
function M.session_exists(dir)
146-
dir = dir or get_start_dir()
141+
dir = dir or session_dir()
142+
147143
return vim.fn.filereadable(get_current(dir)) ~= 0
148144
end
149145

@@ -152,25 +148,26 @@ end
152148
---@return nil
153149
function M.setup(opts)
154150
config.setup(opts)
151+
local dir = session_dir()
155152

156-
local dir = get_start_dir()
157153
if
158154
config.options.autosave
159155
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
160-
and is_neovim_start_ok()
156+
and args_check()
161157
then
162158
M.start()
163159
end
164160
end
165161

166162
---Load a session
167163
---@param opt? table
168-
---@param dir? string
164+
---@param dir string Directory to be used for the session
169165
---@return nil
170166
function M.load(opt, dir)
171167
opt = opt or {}
172-
dir = dir or get_start_dir()
173-
local session = opt.session or (opt.last and get_last(dir) or get_current(dir))
168+
dir = dir or session_dir()
169+
170+
local session = opt.session or (opt.last and get_last() or get_current(dir))
174171

175172
local session_exists = vim.fn.filereadable(session) ~= 0
176173

@@ -202,8 +199,9 @@ end
202199
---Automatically load the session for the current dir
203200
---@return nil
204201
function M.autoload()
205-
local dir = get_start_dir()
206-
if config.options.autoload and is_neovim_start_ok() then
202+
local dir = session_dir()
203+
204+
if config.options.autoload and args_check() then
207205
if allow_dir(dir) and not ignore_dir(dir) then
208206
M.load({}, dir)
209207
end
@@ -237,11 +235,11 @@ end
237235

238236
---Save the session
239237
---@param opt? table
240-
---@param dir? string
238+
---@param dir? string Directory to be used for the session
241239
---@return nil
242240
function M.save(opt, dir)
243241
opt = opt or {}
244-
dir = dir or get_start_dir()
242+
dir = dir or session_dir()
245243

246244
if not opt.session then
247245
-- Do not save the session if the user has manually stopped it...unless it's forced
@@ -265,11 +263,12 @@ function M.save(opt, dir)
265263
end
266264

267265
---Delete the current session
268-
---@param dir? string The directory whose associated session to delete. If not set, the current working directory is used.
266+
---@param dir? string Directory to be used for the session
269267
---@return nil
270268
function M.delete(dir)
271-
dir = dir or get_start_dir()
269+
dir = dir or session_dir()
272270
local session = get_current(dir)
271+
273272
if session and vim.loop.fs_stat(session) ~= 0 then
274273
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedDeletePre", data = { name = session } })
275274

@@ -287,7 +286,7 @@ end
287286
---@return nil
288287
function M.toggle(dir)
289288
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedToggled" })
290-
dir = dir or get_start_dir()
289+
dir = dir or session_dir()
291290

292291
if vim.g.persisting == nil then
293292
return M.load({}, dir)

0 commit comments

Comments
 (0)