@@ -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
1515local 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 )
1920end
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
2930end
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 ()
4448end
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
4953local function allow_dir (dir )
5054 local allowed_dirs = config .options .allowed_dirs
@@ -57,7 +61,7 @@ local function allow_dir(dir)
5761end
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
6266local function ignore_dir (dir )
6367 local ignored_dirs = config .options .ignored_dirs
@@ -70,9 +74,8 @@ local function ignore_dir(dir)
7074end
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)
8386end
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
8891function 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
130125end
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
135130local function get_current (dir )
136131 local name = dir :gsub (utils .get_dir_pattern (), " %%" )
@@ -140,10 +135,11 @@ local function get_current(dir)
140135end
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
145140function 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
148144end
149145
@@ -152,25 +148,26 @@ end
152148--- @return nil
153149function 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
164160end
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
170166function 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
202199--- Automatically load the session for the current dir
203200--- @return nil
204201function 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
242240function 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)
265263end
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
270268function 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
287286--- @return nil
288287function 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