Skip to content

Commit 97bc626

Browse files
authored
fix: properly resolve directories (#105)
* fix: properly resolve directories The `vim.loop.fs_realpath()` function can be used to properly resolve both the `.` shortcut as well as relative paths. This corrects scenarios when opening nvim to a subdirectory of the working directory. With this change doing so will properly resolve the session file to the directory that was specified on the command line. ```console $ pwd /Users/username/.config $ ls fish iterm2 nvim $ nvim nvim ``` This will result in using the path `/Users/username/.config/nvim` for the session. The behavior will be the same as running plain `nvim` from within the `/Users/username/.config/nvim` directory itself. This change also quotes the path passed to `git` to properly handle paths containing spaces. This change also corrects some typos in the readme. * fix: isdirectory is not necessary The fs_realpath function will return nil if the path doesn't exist. * fix: typos in comments * fix: isdirectory is still necessary Oops, the isdirectory check is necessary to confirm it's a directory and not a file.
1 parent d7d8fcb commit 97bc626

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ require("persisted").setup({
248248

249249
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
250250

251-
> **Note**: Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not not result in autoloading but `nvim some/existing/path` or `nvim .` will.
251+
> **Note**: Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will.
252252
253253
### Following current working directory
254254

doc/persisted.nvim.txt

+4-4
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
@@ -270,7 +270,7 @@ Autoloading can be further controlled for certain directories by specifying
270270

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

276276
FOLLOWING CURRENT WORKING DIRECTORY ~
@@ -379,14 +379,14 @@ session, saving the current session before clearing all of the open buffers:
379379

380380
>lua
381381
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
382-
382+
383383
vim.api.nvim_create_autocmd({ "User" }, {
384384
pattern = "PersistedTelescopeLoadPre",
385385
group = group,
386386
callback = function(session)
387387
-- Save the currently loaded session using a global variable
388388
require("persisted").save({ session = vim.g.persisted_loaded_session })
389-
389+
390390
-- Delete all of the open buffers
391391
vim.api.nvim_input("<ESC>:%bd!<CR>")
392392
end,

lua/persisted/init.lua

+26-20
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,38 @@ local function escape_pattern(str, pattern, replace, n)
1919
return string.gsub(str, pattern, replace, n)
2020
end
2121

22+
---Gets the directory from the file/path argument passed to Neovim if there's
23+
---exactly one and it resolves to a valid directory
24+
---@return string|nil
25+
local function args_path()
26+
if vim.fn.argc() ~= 1 then
27+
return nil
28+
end
29+
30+
-- Use expand() to resolve '~' and use fs_realpath to resolve both '.' and
31+
-- relative paths passed as arguments. Note that argv() will only ever return
32+
-- paths/files passed as arguments and does not include other
33+
-- parameters/arguments. fs_realpath() returns nil if the path doesn't exist.
34+
-- Use isdirectory to validate it's a directory and not a file.
35+
local dir = vim.loop.fs_realpath(vim.fn.expand(vim.fn.argv(0)))
36+
if dir ~= nil and vim.fn.isdirectory(dir) ~= 0 then
37+
return dir
38+
end
39+
return nil
40+
end
41+
2242
---Check any arguments passed to Neovim and verify if they're a directory
2343
---@return boolean
2444
local function args_check()
25-
if vim.fn.argc() == 1 then
26-
return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0
27-
end
28-
29-
return vim.fn.argc() == 0
45+
-- Args are valid if a single directory was resolved or if no args were used.
46+
return args_path() ~= nil or vim.fn.argc() == 0
3047
end
3148

3249
---Get the directory to be used for the session
3350
---@return string
3451
local function session_dir()
35-
if vim.fn.argc() == 1 then
36-
local dir = vim.fn.expand(vim.fn.argv(0))
37-
38-
if dir == "." then
39-
return vim.fn.getcwd()
40-
end
41-
42-
if vim.fn.isdirectory(dir) ~= 0 then
43-
return dir
44-
end
45-
end
46-
47-
return vim.fn.getcwd()
52+
-- Use specified directory from arguments or the working directory otherwise.
53+
return args_path() or vim.fn.getcwd()
4854
end
4955

5056
---Does the current working directory allow for the auto-saving and loading?
@@ -92,12 +98,12 @@ function M.get_branch(dir)
9298
dir = dir or session_dir()
9399

94100
if config.options.use_git_branch then
95-
vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null")
101+
vim.fn.system("git -C \"" .. dir .. "\" rev-parse 2>/dev/null")
96102

97103
local git_enabled = (vim.v.shell_error == 0)
98104

99105
if git_enabled then
100-
local git_branch = vim.fn.systemlist("git -C " .. dir .. " rev-parse --abbrev-ref HEAD 2>/dev/null")
106+
local git_branch = vim.fn.systemlist("git -C \"" .. dir .. "\" rev-parse --abbrev-ref HEAD 2>/dev/null")
101107

102108
if vim.v.shell_error == 0 then
103109
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")

0 commit comments

Comments
 (0)