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.main
parent
d7d8fcbbb4
commit
97bc6260fb
|
|
@ -248,7 +248,7 @@ require("persisted").setup({
|
|||
|
||||
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
|
||||
|
||||
> **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.
|
||||
> **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.
|
||||
|
||||
### Following current working directory
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ Install the plugin with your preferred package manager:
|
|||
>vim
|
||||
" Vim Script
|
||||
Plug 'olimorris/persisted.nvim'
|
||||
|
||||
|
||||
lua << EOF
|
||||
require("persisted").setup {
|
||||
-- your configuration comes here
|
||||
|
|
@ -270,7 +270,7 @@ Autoloading can be further controlled for certain directories by specifying
|
|||
|
||||
**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
|
||||
`nvim some_file.rb` will not result in autoloading but `nvim
|
||||
some/existing/path` or `nvim .` will.
|
||||
|
||||
FOLLOWING CURRENT WORKING DIRECTORY ~
|
||||
|
|
@ -379,14 +379,14 @@ session, saving the current session before clearing all of the open buffers:
|
|||
|
||||
>lua
|
||||
local group = vim.api.nvim_create_augroup("PersistedHooks", {})
|
||||
|
||||
|
||||
vim.api.nvim_create_autocmd({ "User" }, {
|
||||
pattern = "PersistedTelescopeLoadPre",
|
||||
group = group,
|
||||
callback = function(session)
|
||||
-- Save the currently loaded session using a global variable
|
||||
require("persisted").save({ session = vim.g.persisted_loaded_session })
|
||||
|
||||
|
||||
-- Delete all of the open buffers
|
||||
vim.api.nvim_input("<ESC>:%bd!<CR>")
|
||||
end,
|
||||
|
|
|
|||
|
|
@ -19,32 +19,38 @@ local function escape_pattern(str, pattern, replace, n)
|
|||
return string.gsub(str, pattern, replace, n)
|
||||
end
|
||||
|
||||
---Gets the directory from the file/path argument passed to Neovim if there's
|
||||
---exactly one and it resolves to a valid directory
|
||||
---@return string|nil
|
||||
local function args_path()
|
||||
if vim.fn.argc() ~= 1 then
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Use expand() to resolve '~' and use fs_realpath to resolve both '.' and
|
||||
-- relative paths passed as arguments. Note that argv() will only ever return
|
||||
-- paths/files passed as arguments and does not include other
|
||||
-- parameters/arguments. fs_realpath() returns nil if the path doesn't exist.
|
||||
-- Use isdirectory to validate it's a directory and not a file.
|
||||
local dir = vim.loop.fs_realpath(vim.fn.expand(vim.fn.argv(0)))
|
||||
if dir ~= nil and vim.fn.isdirectory(dir) ~= 0 then
|
||||
return dir
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
---Check any arguments passed to Neovim and verify if they're a directory
|
||||
---@return boolean
|
||||
local function args_check()
|
||||
if vim.fn.argc() == 1 then
|
||||
return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0
|
||||
end
|
||||
|
||||
return vim.fn.argc() == 0
|
||||
-- Args are valid if a single directory was resolved or if no args were used.
|
||||
return args_path() ~= nil or vim.fn.argc() == 0
|
||||
end
|
||||
|
||||
---Get the directory to be used for the session
|
||||
---@return string
|
||||
local function session_dir()
|
||||
if vim.fn.argc() == 1 then
|
||||
local dir = vim.fn.expand(vim.fn.argv(0))
|
||||
|
||||
if dir == "." then
|
||||
return vim.fn.getcwd()
|
||||
end
|
||||
|
||||
if vim.fn.isdirectory(dir) ~= 0 then
|
||||
return dir
|
||||
end
|
||||
end
|
||||
|
||||
return vim.fn.getcwd()
|
||||
-- Use specified directory from arguments or the working directory otherwise.
|
||||
return args_path() or vim.fn.getcwd()
|
||||
end
|
||||
|
||||
---Does the current working directory allow for the auto-saving and loading?
|
||||
|
|
@ -92,12 +98,12 @@ function M.get_branch(dir)
|
|||
dir = dir or session_dir()
|
||||
|
||||
if config.options.use_git_branch then
|
||||
vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null")
|
||||
vim.fn.system("git -C \"" .. dir .. "\" rev-parse 2>/dev/null")
|
||||
|
||||
local git_enabled = (vim.v.shell_error == 0)
|
||||
|
||||
if git_enabled then
|
||||
local git_branch = vim.fn.systemlist("git -C " .. dir .. " rev-parse --abbrev-ref HEAD 2>/dev/null")
|
||||
local git_branch = vim.fn.systemlist("git -C \"" .. dir .. "\" rev-parse --abbrev-ref HEAD 2>/dev/null")
|
||||
|
||||
if vim.v.shell_error == 0 then
|
||||
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
|
||||
|
|
|
|||
Loading…
Reference in New Issue